diff options
| author | drebs <drebs@leap.se> | 2017-04-25 17:57:25 +0200 | 
|---|---|---|
| committer | drebs <drebs@leap.se> | 2017-04-27 10:02:20 +0200 | 
| commit | c08083e9dfd38f93eb67d234e85b47efa211045c (patch) | |
| tree | 7c2a9f1316c6622bb2882c796f0adb0caee59938 /testing/tests/benchmarks | |
| parent | 48dd87ab8ad077e5913c0cddd5bfb9badd9dcc35 (diff) | |
[test] measure cpu percentage during benchmark
Diffstat (limited to 'testing/tests/benchmarks')
| -rw-r--r-- | testing/tests/benchmarks/conftest.py | 23 | ||||
| -rw-r--r-- | testing/tests/benchmarks/test_cpu.py | 36 | 
2 files changed, 59 insertions, 0 deletions
| diff --git a/testing/tests/benchmarks/conftest.py b/testing/tests/benchmarks/conftest.py index bec5d7ab..543f06b8 100644 --- a/testing/tests/benchmarks/conftest.py +++ b/testing/tests/benchmarks/conftest.py @@ -1,4 +1,6 @@  import base64 +import os +import psutil  import pytest  import random @@ -73,3 +75,24 @@ def txbenchmark_with_setup(benchmark):                                        rounds=4, warmup_rounds=1)          return threads.deferToThread(bench)      return blockOnThreadWithSetup + + +# +# resource monitoring +# + +@pytest.fixture +def monitored_benchmark(benchmark, request): + +    def _monitored_benchmark(fun, *args, **kwargs): +        process = psutil.Process(os.getpid()) +        process.cpu_percent() +        benchmark.pedantic( +            fun, args=args, kwargs=kwargs, +            rounds=1, iterations=1, warmup_rounds=0) +        percent = process.cpu_percent() +        # store value in benchmark session, so json output can be updated +        bs = request.config._benchmarksession +        bs.benchmarks[0].stats.cpu_percent = percent + +    return _monitored_benchmark diff --git a/testing/tests/benchmarks/test_cpu.py b/testing/tests/benchmarks/test_cpu.py new file mode 100644 index 00000000..fba6a6d4 --- /dev/null +++ b/testing/tests/benchmarks/test_cpu.py @@ -0,0 +1,36 @@ +import pytest +import random +import time + +from decimal import Decimal + + +def bellardBig(n): +    # http://en.wikipedia.org/wiki/Bellard%27s_formula +    pi = Decimal(0) +    k = 0 +    while k < n: +        pi += (Decimal(-1) ** k / (1024 ** k)) * ( +            Decimal(256) / (10 * k + 1) + +            Decimal(1) / (10 * k + 9) - +            Decimal(64) / (10 * k + 3) - +            Decimal(32) / (4 * k + 1) - +            Decimal(4) / (10 * k + 5) - +            Decimal(4) / (10 * k + 7) - +            Decimal(1) / (4 * k + 3)) +        k += 1 +    pi = pi * 1 / (2 ** 6) +    return pi + + +def test_long_operation(monitored_benchmark): + +    def _long_operation(): +        sleep = [random.uniform(0.5, 1.5) for _ in xrange(3)] +        while sleep: +            t = sleep.pop() +            time.sleep(t) +            bellardBig(int((10 ** 3) * t)) + +    results = monitored_benchmark(_long_operation) +    print results | 
