-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
137 lines (108 loc) · 3.54 KB
/
tests.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
#!/usr/bin/python -u
# -*- encoding: utf-8 -*-
import random
import sys
import os
from diskdict import DiskDict
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
def test_basics():
print "Starting basis test, cache_size=2:"
print " ",
d = DiskDict(cache_size=2)
d[1] = 1
d[2] = 4
d[3] = 9
d[4] = 16
d[5] = 25
assert( d == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25} )
print ".",
assert( len(d) == 5 )
print ".",
d[10] = 100
d[20] = 400
d[30] = 900
d[40] = 1600
d[50] = 2500
assert( d == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 40: 1600, 10: 100, 50: 2500, 20: 400, 30: 900} )
print ".",
assert( len(d) == 10 )
print ".",
d[1] = -1
d[2] = -4
d[3] = -9
d[4] = -16
d[5] = -25
assert( d == {1: -1, 2: -4, 3: -9, 4: -16, 5: -25, 40: 1600, 10: 100, 50: 2500, 20: 400, 30: 900} )
print ".",
assert( len(d) == 10 )
print ".",
del d[1]
assert( len(d) == 9 )
print ".",
assert( d.counters == {'mem_hits': 6, 'misses': 0, 'cache_file_size': 63, 'set_ops': 15, 'get_ops': 25, 'mem_items': 1, 'disk_items': 8, 'disk_hits': 19, 'len': 9, 'del_ops': 1} )
print ".",
print
print " Counters: " + str(d.counters)
print "all rests passed."
def speed_test_w(n,s):
print " ",
c=0
p = min(100000,int(n/10))
for i in range(n):
if c % p == 0:
print str(int(round(float(c) / n * 100))) + '%',
d['a'+str(i)] = ( i*i, s )
c += 1
print 'done',
def speed_test_r(n,s):
print " ",
c=0
p = min(100000,int(n/10))
for i in range(n):
if c % p == 0:
print str(int(round(float(c) / n * 100))) + '%',
x = d['a'+str(i)]
c += 1
print 'done',
def speed_test_rnd(n,s):
print " ",
r = range(n)
random.shuffle(r)
c=0
p = min(100000,int(n/10))
for i in r:
if c % p == 0:
print str(int(round(float(c) / n * 100))) + '%',
x = d['a'+str(i)]
c += 1
print 'done',
if __name__ == '__main__':
test_basics()
print
import timeit
n = 100000
print "Starting speed test with {} 1k strings, cache_size=0:".format( str(n) )
s = "'" + 'a' * 1024 + "'"
d = DiskDict(cache_size=0)
t = int( n/timeit.timeit('speed_test_w({},{})'.format(n,s), setup='from __main__ import speed_test_w,speed_test_r,speed_test_rnd', number=1) )
print " >> Write : n={}, s={}/{}, ops/s={}".format( n, type(s), len(s), t )
t = int( n/timeit.timeit('speed_test_r({},{})'.format(n,s), setup='from __main__ import speed_test_w,speed_test_r,speed_test_rnd', number=1) )
print " >> Read SEQ: n={}, s={}/{}, ops/s={}".format( n, type(s), len(s), t )
t = int( n/timeit.timeit('speed_test_rnd({},{})'.format(n,s), setup='from __main__ import speed_test_w,speed_test_r,speed_test_rnd', number=1) )
print " >> Read RND: n={}, s={}/{}, ops/s={}".format( n, type(s), len(s), t )
print " Counters: " + str(d.counters)
print "done."
print
n = 100000
print "Starting speed test with {} 100 item lists, cache_size=0:".format( str(n) )
s = range(100)
d = DiskDict(cache_size=0)
t = int( n/timeit.timeit('speed_test_w({},{})'.format(n,s), setup='from __main__ import speed_test_w,speed_test_r,speed_test_rnd', number=1) )
print " >> Write : n={}, s={}/{}, ops/s={}".format( n, type(s), len(s), t )
t = int( n/timeit.timeit('speed_test_r({},{})'.format(n,s), setup='from __main__ import speed_test_w,speed_test_r,speed_test_rnd', number=1) )
print " >> Read SEQ: n={}, s={}/{}, ops/s={}".format( n, type(s), len(s), t )
t = int( n/timeit.timeit('speed_test_rnd({},{})'.format(n,s), setup='from __main__ import speed_test_w,speed_test_r,speed_test_rnd', number=1) )
print " >> Read RND: n={}, s={}/{}, ops/s={}".format( n, type(s), len(s), t )
print " Counters: " + str(d.counters)
print "done."
print