-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_utils.py
117 lines (98 loc) · 3.57 KB
/
benchmark_utils.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
# Timing and other utilities for micro-benchmarking.
from array import array
import shutil
from subprocess import call
import time
import sys
import dynamo_fs
import os
import random
import math
# Collects timing samples during benchmarking.
class BenchmarkTimer:
def __init__(self):
self.samples = []
def begin(self):
self.beginTime = time.time()
def end(self):
endTime = time.time()
delta = endTime - self.beginTime
self.samples.append(delta)
def mean(self):
return sum(self.samples) / len(self.samples)
def stddev(self):
mean = self.mean()
variance = 0.0
for s in self.samples:
variance += (s - mean) ** 2.0
return math.sqrt(variance)
# Creates a clean, empty DynamoFS object.
def emptyFs(backend, rootFilename):
# Clean up any leftovers from the last test.
backend.nuke()
try:
os.unlink(rootFilename)
except:
pass
return dynamo_fs.DynamoFS(backend, rootFilename)
# Creates an n-deep nesting of directories, starting at the given root.
# The path to the deepest directory is returned.
def makeDepth(fs, root, n):
cwd = root
for _ in range(0, n):
# Make up a random 5-character name for this directory.
name = randomDirName()
fs.mkdir(cwd, name)
cwd = dynamo_fs.concatPath(cwd, name)
return cwd
def randomArray(length, rangeMin = 32, rangeMax = 126):
ar = array('B')
for _ in range(0, length):
ar.append(random.randint(rangeMin, rangeMax))
return ar
# Generates a random string of printable ASCII. Optionally, a sub-range of
# printable ASCII may be specified using min and max.
def randomString(length, rangeMin = 32, rangeMax = 126):
text = ''
for _ in range(0, length):
text += chr(random.randint(rangeMin, rangeMax))
return text
# Generates a random string of lower-case letters.
def randomAlphaString(length):
return randomString(length, ord('a'), ord('z'))
# Creates a normally-sized random directory name.
def randomDirName():
return randomAlphaString(7)
# Generates a random string of printable ASCII. All the characters will
# be the same randomly chosen character.
def semirandomString(length, rangeMin = 32, rangeMax = 126):
return chr(random.randint(rangeMin, rangeMax)) * length
def semirandomArray(length, rangeMin = 32, rangeMax = 126):
return array('B', [random.randint(rangeMin, rangeMax)] * length)
# Randomly mutates one cell in a numeric array.
def randomArrayMutate(arr, rangeMin = 32, rangeMax = 126):
index = random.randint(0, len(arr) - 1)
value = random.randint(rangeMin, rangeMax)
while arr[index] == value:
index = random.randint(0, len(arr) - 1)
value = random.randint(rangeMin, rangeMax)
arr[index] = value
# Takes in a list of tuples (or a list of lists) and prints it out to stdout
# in a CSV format.
def printCsv(data):
for line in data:
print ','.join(map(str, line))
couldNotDrop = False # indicates whether drop has failed yet; this way
# we don't print more than one warning
# Clears the filesystem cache. Only works on Linux.
# Based on http://www.linuxquestions.org/questions/linux-kernel-70/how-to-disable-filesystem-cache-627012/
def clearFSCache():
global couldNotDrop
try:
call(['sync'])
with open('/proc/sys/vm/drop_caches', 'w') as f:
f.write("3")
except IOError, e:
if not couldNotDrop:
print >> sys.stderr, "Warning: couldn't drop caches."
couldNotDrop = True