3 # A simple benchmark for Blue Sky that will read and/or write a collection of
4 # files with a specified working set size, and measure the response time to do
7 import json, math, os, random, sys, threading, time
11 def percentile(rank, elements):
12 """Return the value at a fraction rank from the beginning of the list."""
14 # Convert a fraction [0.0, 1.0] to a fractional index in elements.
15 rank = rank * (len(elements) - 1)
17 # Round the index value to the nearest integer values, then interpolate.
18 prev = int(math.floor(rank))
19 next = int(math.ceil(rank))
22 return (1.0 - frac) * elements[prev] + frac * elements[next]
25 """Performs a mix of file system operations and records the performance."""
27 PARAMS = ['duration', 'write_fraction', 'wss_count', 'tot_count',
28 'filesize', 'target_ops']
32 self.duration = 1800.0 # Seconds for which to run
33 self.write_fraction = 0.5 # Fraction of operations which are writes
34 self.wss_count = 2048 # Files in the working set
35 self.tot_count = 2048 # Total number of files created
36 self.filesize = 32 * 1024 # Size of files to work with
37 self.target_ops = 40 # Target operations/second/thread
42 params[p] = getattr(self, p)
46 for i in range(self.tot_count):
47 filename = "file-%d" % (i,)
48 fp = open(filename, 'w')
49 fp.write('\0' * self.filesize)
53 stop_time = time.time() + self.duration
57 if time1 >= stop_time: break
58 info = self._operation()
60 self.stats.append((time1, time2 - time1, info))
62 delay = time1 + (1.0 / self.target_ops) - time2
63 if delay > 0: time.sleep(delay)
66 """Run a single file system test (i.e., read or write a file)."""
68 filename = "file-%d" % (random.randrange(self.wss_count),)
70 if random.uniform(0.0, 1.0) < self.write_fraction:
71 fp = open(filename, 'w')
72 fp.write('\0' * self.filesize)
74 return ('write', filename)
76 fp = open(filename, 'r')
79 return ('read', filename)
81 def print_distribution_stats(stats):
83 print " Count:", len(stats)
84 if len(stats) == 0: return
85 print " Average:", sum(stats) / len(stats)
86 for (s, p) in [("Min", 0.0), ("Med", 0.5), ("90%", 0.9),
87 ("95%", 0.95), ("Max", 1.0)]:
88 print " %s: %s" % (s, percentile(p, stats))
91 duration = max(x[0] for x in stats) - min(x[0] for x in stats)
92 latencies = [x[1] for x in stats]
94 print "Experiment duration:", duration
96 print_distribution_stats([x[1] for x in stats if x[2][0] == 'read'])
98 print_distribution_stats([x[1] for x in stats if x[2][0] == 'write'])
100 fp = open('/tmp/results.json', 'a')
102 def run(filecount, writefrac, filesize):
105 for i in range(THREADS):
107 w.write_fraction = writefrac
108 w.wss_count = w.tot_count = filecount
109 w.filesize = filesize
111 t = threading.Thread(target=w.run)
116 print json.dumps(workers[0].get_params(), indent=2)
126 fp.write(json.dumps(workers[0].get_params(), indent=2) + "\n\n")
127 fp.write(json.dumps(results, indent=2))
131 if __name__ == '__main__':
132 for filesize in [32, 256, 2048]: # KiB
133 for totsize in [256, 512, 1024]: # MiB
134 filecount = totsize * 1024 / filesize
135 for writefrac in [0.0, 0.5]:
136 run(filecount, writefrac, filesize * 1024)