Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / microbench / cleanbench.py
1 #!/usr/bin/python
2 #
3 # A workload generator for the cleaner benchmarks.  This will randomly write to
4 # a collection of files at a fixed rate, to dirty data in the file system.
5 #
6 # DO NOT RUN FROM A DIRECTORY WITH FILES YOU CARE ABOUT--it will overwrite
7 # files in and under the current directory.
8
9 import os, random, sys, time
10
11 def write_file(path, size=1024**2):
12     buf = 'A' * size
13     f = open(path, 'w')
14     f.write(buf)
15     f.close()
16
17 def modify_files(files, rate=1.0, fraction=0.25):
18     files = random.sample(files, int(round(len(files) * fraction)))
19     print "Modifying", len(files), "files"
20
21     last_time = time.time()
22     latencies = []
23     for f in files:
24         now = time.time()
25         next_time = last_time + (1.0/rate)
26         time.sleep(max(0.0, next_time - now))
27         print "Writing", f
28         write_file(f)
29         last_time = next_time
30     print "Done"
31
32 if __name__ == '__main__':
33     print "Modifying files in", os.getcwd()
34     time.sleep(15)
35     all_files = []
36     for (path, dirs, files) in iter(os.walk(".")):
37         for f in files:
38             all_files.append(os.path.join(path, f))
39     print len(all_files), "files total"
40     modify_files(all_files, rate=1e6)