summaryrefslogtreecommitdiff
path: root/t/utils.py
blob: 6a168bc5da8532b147c6b037084a817ecb8b551b (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
from aergia.aergia import Aergia
import unittest


class AergiaUnitTestCase(unittest.TestCase):

    interval = 0.01
    Aergia = Aergia(interval)

    def setUp(self):
        self.Aergia.clear()

    def assert_reasonable_delay(self, func_name, time_expected, samples):
        '''Compares the results reported by Aergia for FUNC_NAME
        with time_expected.'''
        samples_expected = self.aergia_expected_samples(time_expected)
        samples_actual = self.aergia_extract_values_by_func(samples, func_name)
        self.assert_roughly_equal(samples_actual, samples_expected)

    def assert_roughly_equal(self, v1, v2):
        '''Throws an exception if values V1 and V2 are not within 2.5
        samples of each other.'''
        a = abs(v1 - v2)
        self.assertTrue(a <= 2.5, f'{v1} (expected) not roughly {v2} (actual)')

    def aergia_expected_samples(self, total_seconds):
        '''Given TOTAL_SECONDS, returns the expected number of samples, using
        the sampling interval.'''
        return (total_seconds / self.interval)

    def aergia_extract_values_by_func(self, samples, func_name):
        '''Returns the total number of samples for function name, given an
        Aergia SAMPLES object.'''
        return sum(
            value for key, value in samples.items()
            if key.func == func_name
        )

    def yappi_extract_values_by_func(self, stats, func_name):
        '''Returns the total number of samples for function name, given a
        yappi stats object.'''
        for s in stats:
            if s.name == func_name:
                return s.ttot