forked from grantjenks/python-diskcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
115 lines (85 loc) · 2.63 KB
/
utils.py
File metadata and controls
115 lines (85 loc) · 2.63 KB
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
from __future__ import print_function
import os
import subprocess as sp
def percentile(sequence, percent):
if not sequence:
return None
values = sorted(sequence)
if percent == 0:
return values[0]
pos = int(len(values) * percent) - 1
return values[pos]
def secs(value):
units = ['s ', 'ms', 'us', 'ns']
pos = 0
if value is None:
return ' 0.000ns'
elif value == 0:
return ' 0.000ns'
else:
for unit in units:
if value > 1:
return '%7.3f' % value + unit
else:
value *= 1000
def run(*args):
"Run command, print output, and return output."
print('utils$', *args)
result = sp.check_output(args)
print(result)
return result.strip()
def mount_ramdisk(size, path):
"Mount RAM disk at `path` with `size` in bytes."
sectors = size / 512
os.makedirs(path)
dev_path = run('hdid', '-nomount', 'ram://%d' % sectors)
run('newfs_hfs', '-v', 'RAMdisk', dev_path)
run('mount', '-o', 'noatime', '-t', 'hfs', dev_path, path)
return dev_path
def unmount_ramdisk(dev_path, path):
"Unmount RAM disk with `dev_path` and `path`."
run('umount', path)
run('diskutil', 'eject', dev_path)
run('rm', '-r', path)
def retry(sql, query):
pause = 0.001
error = sqlite3.OperationalError
for _ in range(int(LIMITS[u'timeout'] / pause)):
try:
sql(query).fetchone()
except sqlite3.OperationalError as exc:
error = exc
time.sleep(pause)
else:
break
else:
raise error
del error
def display(name, timings):
cols = ('Action', 'Count', 'Miss', 'Median', 'P90', 'P99', 'Max', 'Total')
template = ' '.join(['%9s'] * len(cols))
print()
print(' '.join(['=' * 9] * len(cols)))
print('Timings for %s' % name)
print('-'.join(['-' * 9] * len(cols)))
print(template % cols)
print(' '.join(['=' * 9] * len(cols)))
len_total = sum_total = 0
for action in ['get', 'set', 'delete']:
values = timings[action]
len_total += len(values)
sum_total += sum(values)
print(template % (
action,
len(values),
len(timings.get(action + '-miss', [])),
secs(percentile(values, 0.5)),
secs(percentile(values, 0.9)),
secs(percentile(values, 0.99)),
secs(percentile(values, 1.0)),
secs(sum(values)),
))
totals = ('Total', len_total, '', '', '', '', '', secs(sum_total))
print(template % totals)
print(' '.join(['=' * 9] * len(cols)))
print()