forked from graphite-project/whisper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_whisper.py
executable file
·251 lines (197 loc) · 8.15 KB
/
test_whisper.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
import os
import time
import random
import struct
try:
import unittest2 as unittest
except ImportError:
import unittest
import whisper
class TestWhisper(unittest.TestCase):
"""
Testing functions for whisper.
"""
db = "db.wsp"
@classmethod
def setUpClass(cls):
cls._removedb()
@classmethod
def _removedb(cls):
"""Remove the whisper database file"""
try:
if os.path.exists(cls.db):
os.unlink(cls.db)
except (IOError, OSError):
pass
def test_validate_archive_list(self):
"""blank archive config"""
with self.assertRaises(whisper.InvalidConfiguration):
whisper.validateArchiveList([])
def test_duplicate(self):
"""Checking duplicates"""
whisper.validateArchiveList([(1, 60), (60, 60)])
with self.assertRaises(whisper.InvalidConfiguration):
whisper.validateArchiveList([(1, 60), (60, 60), (1, 60)])
def test_even_precision_division(self):
"""even precision division"""
whisper.validateArchiveList([(60, 60), (6, 60)])
with self.assertRaises(whisper.InvalidConfiguration):
whisper.validateArchiveList([(60, 60), (7, 60)])
def test_timespan_coverage(self):
"""timespan coverage"""
whisper.validateArchiveList([(1, 60), (60, 60)])
with self.assertRaises(whisper.InvalidConfiguration):
whisper.validateArchiveList([(1, 60), (10, 1)])
def test_number_of_points(self):
"""number of points"""
whisper.validateArchiveList([(1, 60), (60, 60)])
with self.assertRaises(whisper.InvalidConfiguration):
whisper.validateArchiveList([(1, 30), (60, 60)])
def test_aggregate(self):
"""aggregate functions"""
# min of 1-4
self.assertEqual(whisper.aggregate('min', [1, 2, 3, 4]), 1)
# max of 1-4
self.assertEqual(whisper.aggregate('max', [1, 2, 3, 4]), 4)
# last element in the known values
self.assertEqual(whisper.aggregate('last', [3, 2, 5, 4]), 4)
# sum ALL THE VALUES!
self.assertEqual(whisper.aggregate('sum', [10, 2, 3, 4]), 19)
# average of the list elements
self.assertEqual(whisper.aggregate('average', [1, 2, 3, 4]), 2.5)
with self.assertRaises(whisper.InvalidAggregationMethod):
whisper.aggregate('derp', [12, 2, 3123, 1])
def test_create(self):
"""Create a db and use info() to validate"""
retention = [(1, 60), (60, 60)]
# check if invalid configuration fails successfully
with self.assertRaises(whisper.InvalidConfiguration):
whisper.create(self.db, [])
# create a new db with a valid configuration
whisper.create(self.db, retention)
# attempt to create another db in the same file, this should fail
with self.assertRaises(whisper.InvalidConfiguration):
whisper.create(self.db, 0)
info = whisper.info(self.db)
# check header information
self.assertEqual(info['maxRetention'],
max([a[0] * a[1] for a in retention]))
self.assertEqual(info['aggregationMethod'], 'average')
self.assertEqual(info['xFilesFactor'], 0.5)
# check archive information
self.assertEqual(len(info['archives']), len(retention))
self.assertEqual(info['archives'][0]['points'], retention[0][1])
self.assertEqual(info['archives'][0]['secondsPerPoint'],
retention[0][0])
self.assertEqual(info['archives'][0]['retention'],
retention[0][0] * retention[0][1])
self.assertEqual(info['archives'][1]['retention'],
retention[1][0] * retention[1][1])
# remove database
self._removedb()
def test_merge(self):
"""test merging two databases"""
testdb = "test-%s" % self.db
self._removedb()
try:
os.unlink(testdb)
except Exception:
pass
# Create 2 whisper databases and merge one into the other
self._update()
self._update(testdb)
whisper.merge(self.db, testdb)
self._removedb()
try:
os.unlink(testdb)
except Exception:
pass
def test_fetch(self):
"""fetch info from database """
# check a db that doesnt exist
with self.assertRaises(Exception):
whisper.fetch("this_db_does_not_exist", 0)
# SECOND MINUTE HOUR DAY
retention = [(1, 60), (60, 60), (3600, 24), (86400, 365)]
whisper.create(self.db, retention)
# check a db with an invalid time range
with self.assertRaises(whisper.InvalidTimeInterval):
whisper.fetch(self.db, time.time(), time.time()-6000)
fetch = whisper.fetch(self.db, 0)
# check time range
self.assertEqual(fetch[0][1] - fetch[0][0],
retention[-1][0] * retention[-1][1])
# check number of points
self.assertEqual(len(fetch[1]), retention[-1][1])
# check step size
self.assertEqual(fetch[0][2], retention[-1][0])
self._removedb()
def _update(self, wsp=None, schema=None):
wsp = wsp or self.db
schema = schema or [(1, 20)]
num_data_points = 20
whisper.create(wsp, schema)
# create sample data
tn = time.time() - num_data_points
data = []
for i in range(num_data_points):
data.append((tn + 1 + i, random.random() * 10))
# test single update
whisper.update(wsp, data[0][1], data[0][0])
# test multi update
whisper.update_many(wsp, data[1:])
return data
def test_update_single_archive(self):
"""Update with a single leveled archive"""
retention_schema = [(1, 20)]
data = self._update(schema=retention_schema)
# fetch the data
fetch = whisper.fetch(self.db, 0) # all data
fetch_data = fetch[1]
for i, (timestamp, value) in enumerate(data):
# is value in the fetched data?
self.assertEqual(value, fetch_data[i])
# check TimestampNotCovered
with self.assertRaises(whisper.TimestampNotCovered):
# in the future
whisper.update(self.db, 1.337, time.time() + 1)
with self.assertRaises(whisper.TimestampNotCovered):
# before the past
whisper.update(self.db, 1.337,
time.time() - retention_schema[0][1] - 1)
self._removedb()
def test_setAggregation(self):
"""Create a db, change aggregation, xFilesFactor, then use info() to validate"""
retention = [(1, 60), (60, 60)]
# create a new db with a valid configuration
whisper.create(self.db, retention)
#set setting every AggregationMethod available
for ag in whisper.aggregationMethods:
for xff in 0.0,0.2,0.4,0.7,0.75,1.0:
#original xFilesFactor
info0 = whisper.info(self.db)
#optional xFilesFactor not passed
whisper.setAggregationMethod(self.db, ag)
#original value should not change
info1 = whisper.info(self.db)
self.assertEqual(info0['xFilesFactor'],info1['xFilesFactor'])
#the selected aggregation method should have applied
self.assertEqual(ag,info1['aggregationMethod'])
#optional xFilesFactor used
whisper.setAggregationMethod(self.db, ag, xff)
#new info should match what we just set it to
info2 = whisper.info(self.db)
#packing and unpacking because
#AssertionError: 0.20000000298023224 != 0.2
target_xff = struct.unpack("!f", struct.pack("!f",xff))[0]
self.assertEqual(info2['xFilesFactor'], target_xff)
#same aggregationMethod asssertion again, but double-checking since
#we are playing with packed values and seek()
self.assertEqual(ag,info2['aggregationMethod'])
self._removedb()
@classmethod
def tearDownClass(cls):
cls._removedb()
if __name__ == '__main__':
unittest.main()