Skip to content

Commit

Permalink
Merge a14db84 into 62afaeb
Browse files Browse the repository at this point in the history
  • Loading branch information
asford committed Jun 3, 2018
2 parents 62afaeb + a14db84 commit 3cd3c57
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/pytest_benchmark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .table import TableResults
from .utils import NAME_FORMATTERS
from .utils import SecondsDecimal
from .utils import cached_property
from .utils import first_or_value
from .utils import get_machine_id
from .utils import load_storage
Expand Down Expand Up @@ -93,8 +92,7 @@ def __init__(self, config):

self.histogram = first_or_value(config.getoption("benchmark_histogram"), False)

@cached_property
def machine_info(self):
def get_machine_info(self):
obj = self.config.hook.pytest_benchmark_generate_machine_info(config=self.config)
self.config.hook.pytest_benchmark_update_machine_info(
config=self.config,
Expand Down Expand Up @@ -136,6 +134,7 @@ def handle_saving(self):
if not self.benchmarks:
self.logger.warn("BENCHMARK-U2", "Not saving anything, no benchmarks have been run!")
return
machine_info = self.get_machine_info()
commit_info = self.config.hook.pytest_benchmark_generate_commit_info(config=self.config)
self.config.hook.pytest_benchmark_update_commit_info(config=self.config, commit_info=commit_info)

Expand All @@ -144,7 +143,7 @@ def handle_saving(self):
config=self.config,
benchmarks=self.benchmarks,
include_data=True,
machine_info=self.machine_info,
machine_info=machine_info,
commit_info=commit_info,
)
self.config.hook.pytest_benchmark_update_json(
Expand All @@ -159,7 +158,7 @@ def handle_saving(self):
config=self.config,
benchmarks=self.benchmarks,
include_data=self.save_data,
machine_info=self.machine_info,
machine_info=machine_info,
commit_info=commit_info,
)
self.config.hook.pytest_benchmark_update_json(
Expand Down Expand Up @@ -187,11 +186,12 @@ def handle_loading(self):
code = "BENCHMARK-C1"
self.logger.warn(code, msg, fslocation=self.storage.location)

machine_info = self.get_machine_info()
for path, compared_benchmark in compared_benchmarks:
self.config.hook.pytest_benchmark_compare_machine_info(
config=self.config,
benchmarksession=self,
machine_info=self.machine_info,
machine_info=machine_info,
compared_benchmark=compared_benchmark,
)
compared_mapping[path] = dict(
Expand Down
53 changes: 53 additions & 0 deletions tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,59 @@ def test_extra(benchmark):
assert bench_info['extra_info'] == {'foo': 'bar'}



def test_update_machine_info_hook_detection(testdir):
"""Tests detection and execution and update_machine_info_hooks.
Verifies that machine info hooks are detected and executed in nested
`conftest.py`s.
"""

record_path_conftest = '''
import os
def pytest_benchmark_update_machine_info(config, machine_info):
machine_info["conftest_path"] = (
machine_info.get("conftest_path", []) + [os.path.relpath(__file__)]
)
'''

simple_test = '''
def test_simple(benchmark):
@benchmark
def resuilt():
1+1
'''

testdir.makepyfile(**{
"conftest" : record_path_conftest,
"test_module/conftest" : record_path_conftest,
"test_module/tests/conftest" : record_path_conftest,
"test_module/tests/simple_test.py" : simple_test,
})

def run_verify_pytest(*args):
testdir.runpytest(
'--benchmark-json=benchmark.json',
'--benchmark-max-time=0.0000001',
*args
)

benchmark_json = json.loads(testdir.tmpdir.join('benchmark.json').read())
machine_info = benchmark_json["machine_info"]

assert sorted(machine_info["conftest_path"]) == sorted([
"conftest.py",
"test_module/conftest.py",
"test_module/tests/conftest.py",
])

run_verify_pytest("test_module/tests")
run_verify_pytest("test_module")
run_verify_pytest(".")


def test_histogram(testdir):
test = testdir.makepyfile(SIMPLE_TEST)
result = testdir.runpytest('--doctest-modules', '--benchmark-histogram=foobar',
Expand Down

0 comments on commit 3cd3c57

Please sign in to comment.