Completed
Pull Request — master (#93)
by
unknown
29s
created

test_extra_fields()   B

Complexity

Conditions 3

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 31
rs 8.8571
c 1
b 0
f 0
1
from pytest import mark
2
3
from pytest_benchmark.stats import Stats
4
5
6 View Code Duplication
def test_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
    stats = Stats()
8
    for i in 4., 36., 45., 50., 75.:
9
        stats.update(i)
10
    assert stats.mean == 42.
11
    assert stats.min == 4.
12
    assert stats.max == 75.
13
    assert stats.stddev == 25.700194551792794
14
    assert stats.rounds == 5
15
    assert stats.total == 210.
16
    assert stats.ops == 0.023809523809523808
17
18
19 View Code Duplication
def test_2():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
    stats = Stats()
21
    stats.update(17.)
22
    stats.update(19.)
23
    stats.update(24.)
24
    assert stats.mean == 20.
25
    assert stats.min == 17.
26
    assert stats.max == 24.
27
    assert stats.stddev == 3.605551275463989
28
    assert stats.rounds == 3
29
    assert stats.total == 60.
30
    assert stats.ops == 0.05
31
32
33
def test_single_item():
34
    stats = Stats()
35
    stats.update(1)
36
    assert stats.mean == 1
37
    assert stats.median == 1
38
    assert stats.iqr_outliers == 0
39
    assert stats.stddev_outliers == 0
40
    assert stats.min == 1
41
    assert stats.max == 1
42
    assert stats.stddev == 0
43
    assert stats.iqr == 0
44
    assert stats.rounds == 1
45
    assert stats.total == 1
46
    assert stats.ld15iqr == 1
47
    assert stats.hd15iqr == 1
48
    assert stats.ops == 1
49
50
51
@mark.parametrize('length', range(1, 10))
52
def test_length(length):
53
    stats = Stats()
54
    for i in range(length):
55
        stats.update(1)
56
57
    assert stats.as_dict()
58
59
60
def test_iqr():
61
    stats = Stats()
62
    for i in 6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49:
63
        stats.update(i)
64
    assert stats.iqr == 22.5  # https://en.wikipedia.org/wiki/Quartile#Example_1
65
66
    stats = Stats()
67
    for i in 7, 15, 36, 39, 40, 41:
68
        stats.update(i)
69
    assert stats.iqr == 25.0  # https://en.wikipedia.org/wiki/Quartile#Example_2
70
71
    stats = Stats()
72
    for i in 1, 2, 3, 4, 5, 6, 7, 8, 9:
73
        stats.update(i)
74
    assert stats.iqr == 4.5  # http://www.phusewiki.org/docs/2012/PRESENTATIONS/SP/SP06%20.pdf - method 1
75
76
    stats = Stats()
77
    for i in 1, 2, 3, 4, 5, 6, 7, 8:
78
        stats.update(i)
79
    assert stats.iqr == 4.0  # http://www.lexjansen.com/nesug/nesug07/po/po08.pdf - method 1
80
81
    stats = Stats()
82
    for i in 1, 2, 1, 123, 4, 1234, 1, 234, 12, 34, 12, 3, 2, 34, 23:
83
        stats.update(i)
84
    assert stats.iqr == 32.0
85
86
    stats = Stats()
87
    for i in 1, 2, 3, 10, 10.1234, 11, 12, 13., 10.1115, 11.1115, 12.1115, 13.5, 10.75, 11.75, 13.12175, 13.1175, 20, \
88
             50, 52:
89
        stats.update(i)
90
    assert stats.stddev == 13.518730097622106
91
    assert stats.iqr == 3.006212500000002  # close enough: http://www.wessa.net/rwasp_variability.wasp
92
93
    stats = Stats()
94
    for i in [
95
        11.2, 11.8, 13.2, 12.9, 12.1, 13.5, 14.8, 14.8, 13.6, 11.9, 10.4, 11.8, 11.5, 12.6, 14.1, 13.5, 12.5, 14.9,
96
        17.0, 17.0, 15.8, 13.3, 11.4, 14.0, 14.5, 15.0, 17.8, 16.3, 17.2, 17.8, 19.9, 19.9, 18.4, 16.2, 14.6, 16.6,
97
        17.1, 18.0, 19.3, 18.1, 18.3, 21.8, 23.0, 24.2, 20.9, 19.1, 17.2, 19.4, 19.6, 19.6, 23.6, 23.5, 22.9, 24.3,
98
        26.4, 27.2, 23.7, 21.1, 18.0, 20.1, 20.4, 18.8, 23.5, 22.7, 23.4, 26.4, 30.2, 29.3, 25.9, 22.9, 20.3, 22.9,
99
        24.2, 23.3, 26.7, 26.9, 27.0, 31.5, 36.4, 34.7, 31.2, 27.4, 23.7, 27.8, 28.4, 27.7, 31.7, 31.3, 31.8, 37.4,
100
        41.3, 40.5, 35.5, 30.6, 27.1, 30.6, 31.5, 30.1, 35.6, 34.8, 35.5, 42.2, 46.5, 46.7, 40.4, 34.7, 30.5, 33.6,
101
        34.0, 31.8, 36.2, 34.8, 36.3, 43.5, 49.1, 50.5, 40.4, 35.9, 31.0, 33.7, 36.0, 34.2, 40.6, 39.6, 42.0, 47.2,
102
        54.8, 55.9, 46.3, 40.7, 36.2, 40.5, 41.7, 39.1, 41.9, 46.1, 47.2, 53.5, 62.2, 60.6, 50.8, 46.1, 39.0, 43.2,
103
    ]:
104
        stats.update(i)
105
    assert stats.iqr == 18.1  # close enough: http://www.wessa.net/rwasp_variability.wasp
106
107
108
def test_ops():
109
    stats = Stats()
110
    stats.update(0)
111
    assert stats.mean == 0
112
    assert stats.ops == 0
113
114
115
def test_percentile():
116
    stats = Stats()
117
118
    # Taken from http://onlinestatbook.com/2/introduction/percentiles.html
119
    for i in [4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10]:
120
        stats.update(i)
121
122
    assert stats.percentile(0.0) == stats.min
123
    assert stats.percentile(1.0) == stats.max
124
    assert stats.percentile(0.5) == stats.median
125
    assert stats.percentile(0.25) == 5.0
126
    assert stats.percentile(0.85) == 9.849999999999998 # approx(9.85)
127
128
    assert hasattr(stats, '_percentile_cache')
129
    assert 0.85 in stats._percentile_cache
130
    assert stats._percentile_cache[0.85] == 9.849999999999998 # approx(9.85)
131
132
133
def test_percentile2():
134
    stats = Stats()
135
136
    # Taken from http://www.itl.nist.gov/div898/handbook/prc/section2/prc262.htm
137
    for i in [95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.0610, 95.1591, 95.1195, 95.1065, 95.0925, 95.1990, 95.1682]:
138
        stats.update(i)
139
140
    assert stats.p0 == stats.min
141
    assert stats.p100 == stats.max
142
    assert stats.p50 == stats.median
143
    assert stats.p90 == 95.19807 # approx(95.1981)
144
145
146
def test_extra_fields():
147
    # Test that percentiles are included in .as_dict() results
148
    expected = {
149
        'p85': 9.849999999999998,
150
        'p25': 5.0,
151
        "min": 4,
152
        "max": 10,
153
        "mean": 7,
154
        "stddev": 2.0261449005179113,
155
        "rounds": 20,
156
        "median": 7.0,
157
        "iqr": 4.0,
158
        "q1": 5.0,
159
        "q3": 9.0,
160
        "iqr_outliers": 0,
161
        "stddev_outliers": 5,
162
        "outliers": '5;0',
163
        "ld15iqr": 4,
164
        "hd15iqr": 10,
165
        "ops": 0.14285714285714285,
166
        "total": 140,
167
    }
168
169
    stats = Stats()
170
171
    for i in [4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10]:
172
        stats.update(i)
173
174
    result = stats.as_dict(extra_fields=('p25', 'p85'))
175
176
    assert result == expected
177