Include elapsed time in write benchmark
[bluesky.git] / microbench / writebench.py
1 #!/usr/bin/python
2 #
3 # A small synthetic write benchmark for BlueSky.  We want to see what happens
4 # when there are write bursts that fill up the disk cache faster than it can be
5 # drained to the cloud.  This tool simply writes out 1 MB files to a directory,
6 # recording the time needed to write each one.  The rate at which files are
7 # written out can be specified in files (/megabytes) per second.
8
9 import os, sys, time
10
11 DIRSIZE=100
12
13 def write_file(path, n, size=1024**2):
14     buf = 'A' * size
15     try: os.mkdir('%s/%d' % (path, n // DIRSIZE))
16     except: pass
17     t1 = time.time()
18     f = open('%s/%d/%d' % (path, n // DIRSIZE, n), 'w')
19     f.write(buf)
20     f.close()
21     t2 = time.time()
22     return t2 - t1
23
24 def run_writebench(path, rate):
25     count = 0
26     start_time = time.time()
27     last_time = start_time
28     latencies = []
29     while last_time < start_time + 120:
30         now = time.time()
31         next_time = start_time + (1.0/rate) * count
32         time.sleep(max(0.0, next_time - now))
33         last_time = time.time()
34         latencies.append(write_file(path, count))
35         print "%s\t%s" % (latencies[-1], time.time() - start_time)
36         count += 1
37     end_time = time.time()
38     duration = end_time - start_time
39     print "# %s MB/s (%d MB/%s seconds)" % (count / duration, count, duration)
40     return latencies
41
42 if __name__ == '__main__':
43     rate = float(sys.argv[1])
44     latencies = run_writebench('.', rate)