-
Notifications
You must be signed in to change notification settings - Fork 13
/
run.py
executable file
·143 lines (114 loc) · 4.93 KB
/
run.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
from subprocess import call
from sys import argv
import os
import subprocess
import workerpool
import multiprocessing
import argparse
######################################################################
######################################################################
######################################################################
parser = argparse.ArgumentParser(description='Simulation runner')
parser.add_argument('scenarios', metavar='scenario', type=str, nargs='*',
help='Scenario to run')
parser.add_argument('-l', '--list', dest="list", action='store_true', default=False,
help='Get list of available scenarios')
parser.add_argument('-s', '--simulate', dest="simulate", action='store_true', default=False,
help='Run simulation and postprocessing (false by default)')
parser.add_argument('-g', '--no-graph', dest="graph", action='store_false', default=True,
help='Do not build a graph for the scenario (builds a graph by default)')
args = parser.parse_args()
if not args.list and len(args.scenarios)==0:
print "ERROR: at least one scenario need to be specified"
parser.print_help()
exit (1)
if args.list:
print "Available scenarios: "
else:
if args.simulate:
print "Simulating the following scenarios: " + ",".join (args.scenarios)
if args.graph:
print "Building graphs for the following scenarios: " + ",".join (args.scenarios)
######################################################################
######################################################################
######################################################################
class SimulationJob (workerpool.Job):
"Job to simulate things"
def __init__ (self, cmdline):
self.cmdline = cmdline
def run (self):
print (" ".join (self.cmdline))
subprocess.call (self.cmdline)
pool = workerpool.WorkerPool(size = multiprocessing.cpu_count())
class Processor:
def run (self):
if args.list:
print " " + self.name
return
if "all" not in args.scenarios and self.name not in args.scenarios:
return
if args.list:
pass
else:
if args.simulate:
self.simulate ()
pool.join ()
self.postprocess ()
if args.graph:
self.graph ()
class CarRelay (Processor):
def __init__ (self, name, extra=[], runs = range(1,11), distances = range (10, 180, 40)):
self.name = name
self.extra = extra
self.runs = runs
self.distances = distances
def simulate (self):
for distance in self.distances:
for run in self.runs:
cmdline = ["./build/car-relay",
"--run=%d" % run,
"--distance=%d" % distance,
] + self.extra
job = SimulationJob (cmdline)
pool.put (job)
def postprocess (self):
try:
os.mkdir ("results/%s" % self.name)
except:
pass
prefix_out = "results/%s/car-relay" % self.name;
prefix_in = "results/car-relay";
subtypes = ["jump-distance", "distance", "in-cache", "tx"]
for subtype in subtypes:
needHeader = True
f_out = open ("%s-%s.txt" % (prefix_out, subtype), "w")
for distance in self.distances:
for run in self.runs:
f_in = open ("%s-%d-%d-%s.txt" % (prefix_in, run, distance, subtype), "r")
firstline = f_in.readline ()
if needHeader:
f_out.write ("Run\tDistance\t%s" % firstline)
needHeader = False
for line in f_in:
f_out.write ("%d\t%d\t%s" % (run, distance, line))
f_in.close ()
os.remove ("%s-%d-%d-%s.txt" % (prefix_in, run, distance, subtype))
f_out.close ()
subprocess.call ("bzip2 -f \"%s-%s.txt\"" % (prefix_out, subtype), shell=True)
def graph (self):
subprocess.call ("./graphs/%s.R" % self.name, shell=True)
try:
# Simulation, processing, and graph building for Figure 3
fig3 = CarRelay (name="figure-3-data-propagation-vs-time", extra = ["--fixedDistance=10000"], runs = range(1,11), distances = range (10, 180, 40))
fig3.run ()
# Simulation, processing, and graph building for Figure 4
fig4 = CarRelay (name="figure-4-data-propagation-vs-distance", distances = range (10, 160, 5), runs = range(1,11))
fig4.run ()
# Simulation, processing, and graph building for Figure 5
fig5 = CarRelay (name="figure-5-retx-count", runs = range(1,11), distances = range (10, 180, 40))
fig5.run ()
finally:
pool.join ()
pool.shutdown ()