Work on a simple workload generator for benchmarking.
[bluesky.git] / microbench / workload.py
1 #!/usr/bin/python
2 #
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
5 # so.
6
7 import json, os, random, sys, threading, time
8
9 THREADS = 1
10
11 class WorkerThread:
12     """Performs a mix of file system operations and records the performance."""
13
14     def __init__(self):
15         self.stats = []
16         self.duration = 10.0        # Seconds for which to run
17         self.write_fraction = 0.3   # Fraction of operations which are writes
18         self.wss_count = 16         # Files in the working set
19         self.tot_count = 32         # Total number of files created
20         self.filesize = 32 * 1024   # Size of files to work with
21         self.target_ops = 2         # Target operations/second/thread
22
23     def setup(self):
24         for i in range(self.tot_count):
25             filename = "file-%d" % (i,)
26             fp = open(filename, 'w')
27             fp.write('\0' * self.filesize)
28             fp.close()
29
30     def run(self):
31         stop_time = time.time() + self.duration
32
33         while True:
34             time1 = time.time()
35             if time1 >= stop_time: break
36             info = self._operation()
37             time2 = time.time()
38             self.stats.append((time1, time2 - time1, info))
39             print self.stats[-1]
40             delay = time1 + (1.0 / self.target_ops) - time2
41             if delay > 0: time.sleep(delay)
42
43     def _operation(self):
44         """Run a single file system test (i.e., read or write a file)."""
45
46         filename = "file-%d" % (random.randrange(self.wss_count),)
47
48         if random.uniform(0.0, 1.0) < self.write_fraction:
49             fp = open(filename, 'w')
50             fp.write('\0' * self.filesize)
51             fp.close()
52             return ('write', filename)
53         else:
54             fp = open(filename, 'r')
55             fp.read()
56             fp.close()
57             return ('read', filename)
58
59 def run_stats(stats):
60     duration = max(x[0] for x in stats) - min(x[0] for x in stats)
61     latencies = [x[1] for x in stats]
62     latencies.sort()
63     print "Experiment duration:", duration
64     print "Operation count:", len(stats)
65     print "Latencies:", latencies
66     print "Average latency:", sum(latencies) / len(latencies)
67
68 if __name__ == '__main__':
69     workers = []
70     threads = []
71     for i in range(THREADS):
72         w = WorkerThread()
73         if i == 0: w.setup()
74         t = threading.Thread(target=w.run)
75         threads.append(t)
76         workers.append(w)
77         t.start()
78     for t in threads:
79         t.join()
80
81     results = []
82     for w in workers:
83         results += w.stats
84     results.sort()
85
86     print json.dumps(results, indent=2)
87     run_stats(results)