Skip to content

Commit a27cb63

Browse files
committed
Refactor this to something simpler.
1 parent d2fc1b9 commit a27cb63

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

src/pytest_benchmark/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .session import PerformanceRegression
1616
from .timers import default_timer
1717
from .utils import NameWrapper
18-
from .utils import format_dict
18+
from .utils import consistent_dumps
1919
from .utils import get_commit_info
2020
from .utils import get_current_time
2121
from .utils import get_tag
@@ -295,8 +295,8 @@ def pytest_addhooks(pluginmanager):
295295

296296

297297
def pytest_benchmark_compare_machine_info(config, benchmarksession, machine_info, compared_benchmark):
298-
machine_info = format_dict(machine_info)
299-
compared_machine_info = format_dict(compared_benchmark['machine_info'])
298+
machine_info = consistent_dumps(machine_info)
299+
compared_machine_info = consistent_dumps(compared_benchmark['machine_info'])
300300

301301
if compared_machine_info != machine_info:
302302
benchmarksession.logger.warning(

src/pytest_benchmark/utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,16 +535,15 @@ def clonefunc(f):
535535
return f2
536536

537537

538-
def format_dict(obj):
539-
value = ', '.join(f'{k}: {json.dumps(v)}' for k, v in sorted(obj.items()))
540-
return f'{value}'
541-
542-
543538
class SafeJSONEncoder(json.JSONEncoder):
544539
def default(self, o):
545540
return f'UNSERIALIZABLE[{o!r}]'
546541

547542

543+
def consistent_dumps(value):
544+
return json.dumps(value, sort_keys=True)
545+
546+
548547
def safe_dumps(obj, **kwargs):
549548
return json.dumps(obj, cls=SafeJSONEncoder, **kwargs)
550549

tests/test_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def test_compare_1(sess, LineMatcher):
348348
print(output.getvalue())
349349
LineMatcher(output.getvalue().splitlines()).fnmatch_lines(
350350
[
351-
'Benchmark machine_info is different. Current: {foo: "bar"} VS saved: {machine: "x86_64", node: "minibox", processor: "x86_64", python_compiler: "GCC 4.6.3", python_implementation: "CPython", python_version: "2.7.3", release: "3.13.0-55-generic", system: "Linux"} (location: tests*test_storage).',
351+
'Benchmark machine_info is different. Current: {"foo": "bar"} VS saved: {"machine": "x86_64", "node": "minibox", "processor": "x86_64", "python_compiler": "GCC 4.6.3", "python_implementation": "CPython", "python_version": "2.7.3", "release": "3.13.0-55-generic", "system": "Linux"} (location: tests/test_storage).',
352352
'Comparing against benchmarks from: 0001_b87b9aae14ff14a7887a6bbaa9731b9a8760555d_20150814_190343_uncommitted' '-changes.json',
353353
'',
354354
'*------------------------------------------------------------------------ benchmark: 2 tests -----------------------------------------------------------------------*',
@@ -381,7 +381,7 @@ def test_compare_2(sess, LineMatcher):
381381
print(output.getvalue())
382382
LineMatcher(output.getvalue().splitlines()).fnmatch_lines(
383383
[
384-
'Benchmark machine_info is different. Current: {foo: "bar"} VS saved: {machine: "x86_64", node: "minibox", processor: "x86_64", python_compiler: "GCC 4.6.3", python_implementation: "CPython", python_version: "2.7.3", release: "3.13.0-55-generic", system: "Linux"} (location: tests*test_storage).',
384+
'Benchmark machine_info is different. Current: {"foo": "bar"} VS saved: {"machine": "x86_64", "node": "minibox", "processor": "x86_64", "python_compiler": "GCC 4.6.3", "python_implementation": "CPython", "python_version": "2.7.3", "release": "3.13.0-55-generic", "system": "Linux"} (location: tests/test_storage).',
385385
'Comparing against benchmarks from: 0002_b87b9aae14ff14a7887a6bbaa9731b9a8760555d_20150814_190348_uncommitted-changes.json',
386386
'',
387387
'*------------------------------------------------------------------------ benchmark: 2 tests -----------------------------------------------------------------------*',

0 commit comments

Comments
 (0)