This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
benchmark.py
118 lines (105 loc) · 3.02 KB
/
benchmark.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
118
#!python
# this program is intended to randomly generate a set of commands
#
# Author: Kuei (Jack) Sun
# Email: kuei.sun@utoronto.ca
import sys
import time
import random
import string
WRITE_MAX_LENGTH = 320
FILE_MAX_LENGTH = 8
MAX_NUM_ENTRIES = 16
ALNUM_CHARS = string.ascii_letters + string.digits
actions_in_curdir = 0
curr_num_entries = 0
def get_rand_name():
length = random.randint(3, FILE_MAX_LENGTH-1)
name = [random.choice(string.ascii_letters)]
for n in xrange(length):
name.append(random.choice(ALNUM_CHARS))
return "".join(name)
def chdir(f, curdir):
global actions_in_curdir
dirent = random.choice(curdir['dir'])
if dirent[1] != curdir:
if actions_in_curdir > 0 or dirent[0] != "..":
f.write("cd %s\n"%dirent[0])
actions_in_curdir = -1
return dirent[1]
return curdir
def mkdir(f, curdir):
global curr_num_entries
if curr_num_entries < MAX_NUM_ENTRIES:
name = get_rand_name()
newdir = { 'file':[], 'dir':[("..", curdir)] }
curdir['dir'].append((name, newdir))
f.write("mkdir %s\n"%name)
curr_num_entries = curr_num_entries + 1
return curdir
def touch(f, curdir):
global curr_num_entries
if curr_num_entries < MAX_NUM_ENTRIES:
name = get_rand_name()
if name not in curdir['file']:
curdir['file'].append(name)
f.write("touch %s\n"%name)
curr_num_entries = curr_num_entries + 1
return curdir
def write(f, curdir):
if len(curdir['file']) > 0:
which = random.choice(curdir['file'])
length = random.randint(1, WRITE_MAX_LENGTH)
data = []
for n in xrange(length):
data.append(random.choice(ALNUM_CHARS))
f.write("write %s %s\n"%("".join(data), which))
return curdir
WHATEVER = 0
FILE = 1
DIRECTORY = 2
def remove(f, curdir, what=WHATEVER):
global curr_num_entries
if what == 0:
what = random.randint(FILE, DIRECTORY)
if what == FILE:
if len(curdir['file']) > 0:
which = random.choice(curdir['file'])
curdir['file'].remove(which)
f.write("rm %s\n"%which)
curr_num_entries = curr_num_entries - 1
else:
curdir = remove(f, curdir, DIRECTORY)
else:
if len(curdir['dir']) <= 1:
if len(curdir['file']) > 0:
curdir = remove(f, curdir, FILE)
else:
name = ".."
while name == "..":
dirent = random.choice(curdir['dir'])
name = dirent[0]
if len(dirent[1]['dir']) <= 1 and \
len(dirent[1]['file']) == 0:
curdir['dir'].remove(dirent)
f.write("rm %s\n"%dirent[0])
curr_num_entries = curr_num_entries - 1
else:
curdir = chdir(f, curdir)
curdir = remove(f, curdir)
return curdir
CMDS = [ chdir, touch, mkdir, write, remove, remove ]
if __name__ == "__main__":
root = { 'file':[], 'dir':[] }
root['dir'].append(("..", root))
pwd = root
num_cmds = 300000
if len(sys.argv) == 2:
num_cmds = int(sys.argv[1])
print "Generating %d commands"%num_cmds
f = open("script.txt", "wb")
for n in xrange(num_cmds):
pwd = random.choice(CMDS)(f, pwd)
actions_in_curdir = actions_in_curdir + 1
f.write("checkfs\n")
f.close()