summaryrefslogtreecommitdiff
path: root/testing/tests/benchmarks/test_resources.py
blob: 173edbd16a03d8daf7379650060821e81ec5bd4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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


@pytest.mark.skip(reason='not a real use case, used only for instrumentation')
def test_cpu_intensive(monitored_benchmark):

    def _cpu_intensive():
        sleep = [random.uniform(0.5, 1.5) for _ in xrange(3)]
        while sleep:
            t = sleep.pop()
            time.sleep(t)
            bellardBig(int((10 ** 3) * t))

    monitored_benchmark(_cpu_intensive)


@pytest.mark.skip(reason='not a real use case, used only for instrumentation')
def test_memory_intensive(monitored_benchmark):

    def _memory_intensive():
        sleep = [random.uniform(0.5, 1.5) for _ in xrange(3)]
        bigdata = ""
        while sleep:
            t = sleep.pop()
            bigdata += "b" * 10 * int(10E6)
            time.sleep(t)

    monitored_benchmark(_memory_intensive)