-
Notifications
You must be signed in to change notification settings - Fork 85
/
2018_ist.py
354 lines (260 loc) · 10.3 KB
/
2018_ist.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
#for 100k budget, use 300 minutes (max is around 190)
# for s in `ls *.sh`; do sbatch $s; done
# scancel --user=arcand
# squeue -u arcand
# For interactive session:
# qlogin --account=nn9476k
# http://www.uio.no/english/services/it/research/hpc/abel/help/user-guide/queue-system.html
#
# Max 400 submitted jobs per user at any time.
# Some useful commands as a user on the Abel system are:
#
# sbatch <job-script-file> Submit a job script to the queue system
# squeue List of all jobs
# pending List of all pending jobs
# qsumm Summary information about queue usage
# cost -u User CPU Usage information
# cost -p Project CPU Usage information
# projects Lists the projects you are member of
# module avail Lists available software Modules
# module list Lists your currently used software Modules
# dusage List home directory disk usage
# dusage -p <project> List project disk usage
# scontrol show job <id> Details of a job
import math
import os
import random
import shutil
import sys
import stat
import io
import statistics
EXP_ID = "mio_ext"
if len(sys.argv) != 9:
print("Usage:\n<nameOfScript>.py <cluster> <baseSeed> <dir> <minSeed> <maxSeed> <maxActions> <minutesPerRun> <nJobs>")
exit(1)
#input parameters
CLUSTER = sys.argv[1].lower() in ("yes", "true", "t")
BASE_SEED = int(sys.argv[2])
BASE_DIR = os.path.abspath(sys.argv[3])
MIN_SEED = int(sys.argv[4])
MAX_SEED = int(sys.argv[5])
MAX_ACTIONS = int(sys.argv[6])
MINUTES_PER_RUN = int(sys.argv[7])
NJOBS = int(sys.argv[8])
#input parameter validation
if MIN_SEED > MAX_SEED:
print("ERROR: min seed is greater than max seed")
exit(1)
if not os.path.isdir(BASE_DIR):
print("creating folder: " + BASE_DIR)
os.makedirs(BASE_DIR)
else:
print("ERROR: target folder already exists")
exit(1)
if CLUSTER:
SUTS = [("features-service", 1),
("scout-api", 1),
("proxyprint", 2),
("rest-ncs", 1),
("rest-scs", 1),
("rest-news", 1),
("catwatch", 1)#,
# ("ocvn", 1) # currently having problems
]
HOME = os.environ['HOME']
# How to run EvoMaster
EVOMASTER = "java -Xms2G -Xmx4G -jar evomaster.jar"
EVOMASTER_DIR = HOME + "/tools"
CASESTUDY_DIR = HOME + "/casestudies/dist"
LOGS_DIR = HOME + "/nobackup"
else:
SUTS = [("ocvn", 1)]
EVOMASTER = os.environ['EVOMASTER']
CASESTUDY_DIR = os.environ['EMB_DIR']
LOGS_DIR = BASE_DIR
if NJOBS < len(SUTS):
print("ERROR: you need at least one job per SUT, and those are " + str(len(SUTS)))
exit(1)
# Where to put stuff (default in subdirs of BASEDIR)
REPORT_DIR = BASE_DIR + "/reports"
os.makedirs(REPORT_DIR)
SCRIPT_DIR = BASE_DIR + "/scripts"
os.makedirs(SCRIPT_DIR)
TEST_DIR = BASE_DIR + "/tests"
os.makedirs(TEST_DIR)
ALL_LOGS = LOGS_DIR + "/logs"
shutil.rmtree(ALL_LOGS, ignore_errors=True)
LOG_DIR = ALL_LOGS + "/" + EXP_ID
os.makedirs(LOG_DIR)
CONTROLLER_PID = "CONTROLLER_PID"
CPUS = 3
def writeScript(code, port, sut_name):
script_path = SCRIPT_DIR + "/evomaster_" + str(port) + "_" + sut_name + ".sh"
script = open(script_path, "w")
script.write(code)
st = os.stat(script_path)
os.chmod(script_path, st.st_mode | stat.S_IEXEC)
return script
def getScriptHead(timeoutMinutes):
s = "#!/bin/bash \n"
if CLUSTER:
s += "#SBATCH --job-name=" + EXP_ID+ " \n"
s += "#SBATCH --account=nn9476k \n"
s += "#SBATCH --mem-per-cpu=4G \n"
s += "#SBATCH --nodes=1 --ntasks-per-node=" + str(CPUS) + " \n"
s += "#SBATCH --time=" + str(timeoutMinutes) + ":00 \n\n"
return s
def createJobHead(port, sut_name, timeoutMinutes):
script = io.StringIO()
script.write(getScriptHead(timeoutMinutes))
sut_log = LOG_DIR + "/log_sut_" + sut_name + "_" + str(port) + ".txt"
# Start SUT as background process on the given port
controllerPort = str(port)
sutPort = str(port + 1)
EM_POSTFIX = "-evomaster-runner.jar"
if CLUSTER:
sut_em_path = os.path.join(CASESTUDY_DIR, sut_name + EM_POSTFIX)
sut_jar_path = os.path.join(CASESTUDY_DIR, sut_name + ".jar")
script.write("\nmodule load java/jdk1.8.0_112\n\n")
script.write("cd $SCRATCH \n")
script.write("cp " + EVOMASTER_DIR + "/evomaster.jar . \n")
script.write("cp " + sut_em_path + " . \n")
script.write("cp " + sut_jar_path + " . \n")
script.write("\n")
em_runner = sut_name + EM_POSTFIX
em_sut = sut_name + ".jar"
else:
em_runner = CASESTUDY_DIR + "/" + sut_name + EM_POSTFIX
em_sut = CASESTUDY_DIR + "/" + sut_name + ".jar"
script.write("\n")
params = " " + controllerPort + " " + sutPort + " " + em_sut + " 600 "
# JVM properties
jvm = " -Xms1G -Xmx2G -Dem.muteSUT=true"
command = "java " + jvm + " -jar " + em_runner + " " + params + " > " + sut_log + " 2>&1 &"
if not CLUSTER:
script.write("\n\necho \"Starting EM Runner with: " + command +"\"\n")
script.write("echo\n\n")
script.write(command + "\n\n")
script.write(CONTROLLER_PID + "=$! \n\n") # store pid of process, so can kill it
script.write("sleep 20 \n\n") # wait a bit to be sure the SUT handler can respond
return script.getvalue()
def closeJob(port, sut_name):
return "kill $" + CONTROLLER_PID + "\n"
####################################
def addJobBody(port, sut_name, seed, alg, mode):
script = io.StringIO()
em_log = LOG_DIR + "/log_em_" + sut_name + "_" + str(port) + ".txt"
params = " --alg=" + alg
params += " --stoppingCriterion=FITNESS_EVALUATIONS"
params += " --maxActionEvaluations=" + str(MAX_ACTIONS)
params += " --testSuiteFileName=EM_" + alg + "_" + str(seed) + "_Test"
params += " --probOfSmartSampling=0.5"
params += " --feedbackDirectedSampling="+mode
params += " --heuristicsForSQL=true"
params += " --statisticsColumnId=" + sut_name
params += " --seed=" + str(seed)
params += " --sutControllerPort=" + str(port)
params += " --outputFolder=" + TEST_DIR + "/" + sut_name
params += " --statisticsFile=" + \
REPORT_DIR + "/statistics_" + sut_name + "_" + str(seed) + ".csv"
params += " --snapshotInterval=5"
params += " --snapshotStatisticsFile=" + \
REPORT_DIR + "/snapshot_" + sut_name + "_" + str(seed) + ".csv"
params += " --appendToStatisticsFile=true"
params += " --writeStatistics=true"
params += " --showProgress=false"
command = EVOMASTER + params + " >> " + em_log + " 2>&1"
if not CLUSTER:
script.write("\n\necho \"Starting SUT with: " + command +"\"\n")
script.write("echo\n\n")
script.write(command + " \n\n")
return script.getvalue()
####################################
class State:
def __init__(self, budget):
self.budget = budget
generated = 0
waits = []
jobsLeft = NJOBS
sutsLeft = len(SUTS)
counter = 0
opened = False
perJob = 0
port = BASE_SEED
def updatePerJob(self):
if self.jobsLeft == 0:
self.perJob = 0
else:
self.perJob = self.budget / self.jobsLeft
def updatePort(self):
self.port += 10
def updateBudget(self, weight):
self.counter += weight
self.budget -= weight
def getTimeoutMinutes(self):
timeoutMinutes = 10 + int(math.ceil(1.2 * self.counter * MINUTES_PER_RUN))
self.waits.append(timeoutMinutes)
return timeoutMinutes
def resetTmpForNewRun(self):
self.counter = 0
self.opened = False
self.updatePerJob()
self.updatePort()
def hasSpareJobs(self):
return self.jobsLeft > self.sutsLeft
def createOneJob(state, sut_name, seed, weight, alg, mode):
code = addJobBody(state.port, sut_name, seed, alg, mode)
state.updateBudget(weight)
state.jobsLeft -= 1
state.opened = True
state.generated += 1
return code
def writeWithHeadAndFooter(code, port, sut_name, timeout):
head = createJobHead(port, sut_name, timeout)
footer = closeJob(port, sut_name)
code = head + code + footer
writeScript(code, port, sut_name)
def createJobs():
ALGS = ["RANDOM", "WTS", "MOSA", "MIO"]
NRUNS_PER_SUT = (1 + MAX_SEED - MIN_SEED) * (len(ALGS) + 2)
SUT_WEIGHTS = sum(map(lambda x: x[1], SUTS))
state = State(NRUNS_PER_SUT * SUT_WEIGHTS)
SUTS.sort(key = lambda x : -x[1])
for sut in SUTS:
sut_name = sut[0]
weight = sut[1]
state.sutsLeft -= 1
state.resetTmpForNewRun()
code = ""
completedForSut = 0
for seed in range(MIN_SEED, MAX_SEED + 1):
random.shuffle(ALGS)
for alg in ALGS:
MODES = ["NONE"]
if alg == "MIO":
MODES = ["NONE","LAST", "FOCUSED_QUICKEST"]
random.shuffle(MODES)
for mode in MODES:
if state.counter == 0:
code = createOneJob(state, sut_name, seed, weight, alg, mode)
elif (state.counter + weight) < state.perJob \
or not state.hasSpareJobs() or \
(NRUNS_PER_SUT - completedForSut < 0.3 * state.perJob / weight):
code += addJobBody(state.port, sut_name, seed, alg, mode)
state.updateBudget(weight)
else:
writeWithHeadAndFooter(code, state.port, sut_name, state.getTimeoutMinutes())
state.resetTmpForNewRun()
code = createOneJob(state, sut_name, seed, weight, alg, mode)
completedForSut += 1
if state.opened:
writeWithHeadAndFooter(code, state.port, sut_name, state.getTimeoutMinutes())
print("Generated scripts: " + str(state.generated))
print("Max wait for a job: " + str(max(state.waits)) + " minutes")
print("Median wait for a job: " + str(statistics.median(state.waits)) + " minutes")
print("Budget left: "+ str(state.budget))
print("Total time: " + str(sum(state.waits) / 60) + " hours")
print("Total budget: " + str(CPUS * sum(state.waits) / 60) + " hours")
createJobs()