2 this is a load-generating client program. It does all of its work through a
3 given tahoe node (specified by URL), and performs random reads and writes
6 Run this in a directory with the following files:
7 server-URLs : a list of tahoe node URLs (one per line). Each operation
8 will use a randomly-selected server.
9 root.cap: (string) the top-level directory rwcap to use
10 delay: (float) seconds to delay between operations
11 operation-mix: "R/W": two ints, relative frequency of read and write ops
14 Set argv[1] to a per-client stats-NN.out file. This will will be updated with
15 running totals of bytes-per-second and operations-per-second. The stats from
16 multiple clients can be totalled together and averaged over time to compute
17 the traffic being accepted by the grid.
19 Each time a 'read' operation is performed, the client will begin at the root
20 and randomly choose a child. If the child is a directory, the client will
21 recurse. If the child is a file, the client will read the contents of the
24 Each time a 'write' operation is performed, the client will generate a target
25 filename (a random string). 90% of the time, the file will be written into
26 the same directory that was used last time (starting at the root). 10% of the
27 time, a new directory is created by assembling 1 to 5 pathnames chosen at
28 random. The client then writes a certain number of zero bytes to this file.
29 The filesize is determined with something like a power-law distribution, with
30 a mean of 10kB and a max of 100MB, so filesize=min(int(1.0/random(.0002)),1e8)
35 import os, sys, httplib, binascii
36 import urllib, simplejson, random, time, urlparse
38 if sys.argv[1] == "--stats":
39 statsfiles = sys.argv[2:]
40 # gather stats every 10 seconds, do a moving-window average of the last
49 for line in open(sf, "r").readlines():
50 name, value = line.split(":")
51 value = int(value.strip())
54 stats[name] += float(value)
57 delta = dict( [ (n,stats[n]-last_stats[n])
60 for name in sorted(delta.keys()):
61 avg = float(delta[name]) / float(DELAY)
62 print "%20s: %0.2f per second" % (name, avg)
64 while len(totals) > MAXSAMPLES:
69 print "MOVING WINDOW AVERAGE:"
70 for name in sorted(delta.keys()):
71 avg = sum([ s[name] for s in totals]) / (DELAY*len(totals))
72 print "%20s %0.2f per second" % (name, avg)
79 stats_out = sys.argv[1]
82 for url in open("server-URLs", "r").readlines():
85 server_urls.append(url)
86 root = open("root.cap", "r").read().strip()
87 delay = float(open("delay", "r").read().strip())
88 readfreq, writefreq = (
89 [int(x) for x in open("operation-mix", "r").read().strip().split("/")])
97 directories_written = 0
99 def listdir(nodeurl, root, remote_pathname):
100 if nodeurl[-1] != "/":
102 url = nodeurl + "uri/%s/" % urllib.quote(root)
104 url += urllib.quote(remote_pathname)
106 data = urllib.urlopen(url).read()
108 parsed = simplejson.loads(data)
111 print "DATA was", data
114 assert nodetype == "dirnode"
115 global directories_read
116 directories_read += 1
117 children = dict( [(unicode(name),value)
119 in d["children"].iteritems()] )
123 def choose_random_descendant(server_url, root, pathname=""):
124 children = listdir(server_url, root, pathname)
125 name = random.choice(children.keys())
126 child = children[name]
128 new_pathname = pathname + "/" + name
131 if child[0] == "filenode":
133 return choose_random_descendant(server_url, root, new_pathname)
135 def read_and_discard(nodeurl, root, pathname):
136 if nodeurl[-1] != "/":
138 url = nodeurl + "uri/%s/" % urllib.quote(root)
140 url += urllib.quote(pathname)
141 f = urllib.urlopen(url)
142 global bytes_downloaded
147 bytes_downloaded += len(data)
151 "dreamland/disengaging/hucksters",
152 "dreamland/disengaging/klondikes",
153 "dreamland/disengaging/neatly",
154 "dreamland/cottages/richmond",
155 "dreamland/cottages/perhaps",
156 "dreamland/cottages/spies",
157 "dreamland/finder/diversion",
158 "dreamland/finder/cigarette",
159 "dreamland/finder/album",
160 "hazing/licences/comedian",
161 "hazing/licences/goat",
162 "hazing/licences/shopkeeper",
163 "hazing/regiment/frigate",
164 "hazing/regiment/quackery",
165 "hazing/regiment/centerpiece",
166 "hazing/disassociate/mob",
167 "hazing/disassociate/nihilistic",
168 "hazing/disassociate/bilbo",
171 def create_random_directory():
172 d = random.choice(directories)
173 pieces = d.split("/")
174 numsegs = random.randint(1, len(pieces))
175 return "/".join(pieces[0:numsegs])
177 def generate_filename():
178 fn = binascii.hexlify(os.urandom(4))
183 size = random.expovariate(1.0 / mean)
184 return int(min(size, 100e6))
186 # copied from twisted/web/client.py
187 def parse_url(url, defaultPort=None):
189 parsed = urlparse.urlparse(url)
191 path = urlparse.urlunparse(('','')+parsed[2:])
192 if defaultPort is None:
193 if scheme == 'https':
197 host, port = parsed[1], defaultPort
199 host, port = host.split(':')
203 return scheme, host, port, path
205 def generate_and_put(nodeurl, root, remote_filename, size):
206 if nodeurl[-1] != "/":
208 url = nodeurl + "uri/%s/" % urllib.quote(root)
209 url += urllib.quote(remote_filename)
211 scheme, host, port, path = parse_url(url)
213 c = httplib.HTTPConnection(host, port)
214 elif scheme == "https":
215 c = httplib.HTTPSConnection(host, port)
217 raise ValueError("unknown scheme '%s', need http or https" % scheme)
218 c.putrequest("PUT", path)
219 c.putheader("Hostname", host)
220 c.putheader("User-Agent", "tahoe-check-load")
221 c.putheader("Connection", "close")
222 c.putheader("Content-Length", "%d" % size)
224 global bytes_uploaded
226 chunksize = min(size, 4096)
228 c.send("\x00" * chunksize)
229 bytes_uploaded += chunksize
230 return c.getresponse()
233 current_writedir = ""
237 if random.uniform(0, readfreq+writefreq) < readfreq:
242 server = random.choice(server_urls)
244 pathname = choose_random_descendant(server, root)
245 print " reading", pathname
246 read_and_discard(server, root, pathname)
247 files_downloaded += 1
249 if random.uniform(0, 100) < 10:
250 current_writedir = create_random_directory()
251 filename = generate_filename()
253 pathname = current_writedir + "/" + filename
256 print " writing", pathname
259 generate_and_put(server, root, pathname, size)
262 f = open(stats_out+".tmp", "w")
263 f.write("files-uploaded: %d\n" % files_uploaded)
264 f.write("files-downloaded: %d\n" % files_downloaded)
265 f.write("bytes-uploaded: %d\n" % bytes_uploaded)
266 f.write("bytes-downloaded: %d\n" % bytes_downloaded)
267 f.write("directories-read: %d\n" % directories_read)
268 f.write("directories-written: %d\n" % directories_written)
270 os.rename(stats_out+".tmp", stats_out)