From 3f762bb742f3b94dfb51019d95118befb2275287 Mon Sep 17 00:00:00 2001 From: Noah Negin-Ulster Date: Wed, 30 Nov 2022 17:16:40 -0500 Subject: [PATCH] fix: group snapshot writes by extension class --- CONTRIBUTING.md | 24 ++++++++++++++++++++++++ src/syrupy/session.py | 5 +++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dc9136bb5..eae37ba1f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -91,6 +91,30 @@ Fill in the relevant sections, clearly linking the issue the change is attemping `debugpy` is installed in local development. A VSCode launch config is provided. Run `inv test -v -d` to enable the debugger (`-d` for debug). It'll then wait for you to attach your VSCode debugging client. +#### Debugging Performance Issues + +You can run `inv benchmark` to run the full benchmark suite. Alternatively, write a test file, e.g.: + +```py +# test_performance.py +import pytest +import os + +SIZE = int(os.environ.get("SIZE", 1000)) + +@pytest.mark.parametrize("x", range(SIZE)) +def test_performance(x, snapshot): + assert x == snapshot +``` + +and then run: + +```sh +SIZE=1000 python -m cProfile -s cumtime -m pytest test_performance.py --snapshot-update -s > profile.log +``` + +See the cProfile docs for metric sorting options. + ## Styleguides ### Commit Messages diff --git a/src/syrupy/session.py b/src/syrupy/session.py index 9f5caf544..51065627b 100644 --- a/src/syrupy/session.py +++ b/src/syrupy/session.py @@ -58,9 +58,10 @@ def queue_snapshot_write( data: "SerializedData", index: "SnapshotIndex", ) -> None: - queue = self._queued_snapshot_writes.get(extension, []) + key = hash(extension.__class__) + queue = self._queued_snapshot_writes.get(key, []) queue.append((data, index)) - self._queued_snapshot_writes[extension] = queue + self._queued_snapshot_writes[key] = queue def flush_snapshot_write_queue(self) -> None: for extension, queued_write in self._queued_snapshot_writes.items():