This repository was archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbench.sc
More file actions
executable file
·370 lines (350 loc) · 12.8 KB
/
Copy pathbench.sc
File metadata and controls
executable file
·370 lines (350 loc) · 12.8 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env amm
import ammonite.ops._
import ammonite.ops.ImplicitWd._
import scala.concurrent.duration._
import scala.collection.mutable.ListBuffer
import scala.collection.immutable.List
//import scala.collection.JavaConverters._
import scala.util.{Try, Success, Failure}
//import java.lang.{Process, ProcessBuilder}
import java.io.{PrintWriter, OutputStream, File, FileWriter}
import java.nio.file.Files
import $file.build, build.{relps, relp, binp, format}
import $file.benchmarks, benchmarks._
import $ivy.`com.decodified::scala-ssh:0.10.0`, com.decodified.scalassh.{SSH, HostConfigProvider, PublicKeyLogin}
//import $ivy.`ch.qos.logback:logback-classic:1.1.7`
val runnerAddr = "127.0.0.1:45678";
//val masterAddr = "192.168.0.106:45679";
val masterAddr = "127.0.0.1:45679";
def getExperimentRunner(prefix: String, results: Path, testing: Boolean, benchmarks_string: String): BenchmarkRunner = {
var params: Seq[Shellable] = Seq(
"-jar",
"target/scala-2.12/Benchmark Suite Runner-assembly-0.3.0-SNAPSHOT.jar",
"--server", runnerAddr,
"--prefix", prefix,
"--output-folder", results.toString);
if (testing) {
params ++= Seq[Shellable]("--testing");
}
if (!benchmarks_string.isEmpty) {
params ++= Seq[Shellable]("--benchmarks", benchmarks_string);
}
BenchmarkRunner(
bench = BenchmarkInfo(
"RUN",
"Experiment Runner"),
runner = Runner(
relp("runner"),
javaBin,
params
)
)
};
val logs = pwd / 'logs;
val results = pwd / 'results;
val defaultNodesFile = pwd / "nodes.conf";
@arg(doc ="Run a specific benchmark client.")
@main
def client(name: String, master: AddressArg, runid: String, publicif: String, clientPort: Int = 45678): Unit = {
val runId = runid;
val publicIf = publicif;
implementations.get(name) match {
case Some(impl) => {
println(s"Found Benchmark ${impl.label} for ${name}. Master is at $master");
val logdir = logs / runId;
mkdir! logdir;
val clientRunner = impl.clientRunner(master, s"${publicIf}:${clientPort}");
val client = clientRunner.run(logdir);
Runtime.getRuntime().addShutdownHook(new Thread() {
override def run(): Unit = {
println("Got termination signal. Killing client.");
client.destroy();
}
});
client.waitFor();
Console.err.println("Client shut down with code="+client.exitValue()+"!");
}
case None => {
Console.err.println(s"No Benchmark Implementation found for '${name}'");
System.exit(1);
}
}
}
@arg(doc ="Run benchmarks using a cluster of nodes.")
@main
def remote(withNodes: Path = defaultNodesFile, testing: Boolean = false, impls: String = "", benchmarks: String = ""): Unit = {
val nodes = readNodes(withNodes);
val masters = runnersForImpl(impls, _.remoteRunner(runnerAddr, masterAddr, nodes.size));
val totalStart = System.currentTimeMillis();
val runId = s"run-${totalStart}";
val logdir = logs / runId;
mkdir! logdir;
val resultsdir = results / runId;
mkdir! resultsdir;
if (!testing) {
%%.apply(root/'bin/'bash, "-c",s"./exp_setup.sh $resultsdir");
}
val nRunners = masters.size;
var errors = 0;
masters.zipWithIndex.foreach { case (master, i) =>
val experimentRunner = getExperimentRunner(master.symbol, resultsdir, testing, benchmarks);
println(s"Starting run [${i+1}/$nRunners]: ${master.label}");
val start = System.currentTimeMillis();
val r = remoteExperiment(experimentRunner, master, runId, logdir, nodes);
val end = System.currentTimeMillis();
val time = FiniteDuration(end-start, MILLISECONDS);
r match {
case Success(_) => println(s"Finished ${master.label} in ${format(time)}");
case Failure(e) => {
errors += 1;
println(s"Runner did not finish successfully: ${master.label} (${format(time)})");
Console.err.println(e);
e.printStackTrace(Console.err);
}
}
endSeparator(master.label, experimentRunner.errorLog(logdir));
endSeparator(master.label, experimentRunner.outputLog(logdir));
}
val totalEnd = System.currentTimeMillis();
val totalTime = FiniteDuration(totalEnd-totalStart, MILLISECONDS);
println("========");
println(s"Finished all runners in ${format(totalTime)}");
println(s"There were $errors errors. Logs can be found in ${logdir}");
}
@arg(doc ="Run benchmarks using a cluster of nodes.")
@main
def fakeRemote(withClients: Int = 1, testing: Boolean = false, impls: String = "", benchmarks: String = "", remoteDir: os.Path = tmp.dir()): Unit = {
val alwaysCopyFiles = List[Path](relp("bench.sc"), relp("benchmarks.sc"), relp("build.sc"), relp("client.sh"));
val masterBenches = runnersForImpl(impls, identity);
val (copyFiles: List[RelPath], copyDirectories: List[RelPath]) = masterBenches.map(_.mustCopy).flatten.distinct.partition(_.isFile) match {
case (files, folders) => ((files ++ alwaysCopyFiles).map(_.relativeTo(pwd)), folders.map(_.relativeTo(pwd)))
};
println(s"Going to copy files=${copyFiles.mkString("[", ",", "]")} and folders==${copyDirectories.mkString("[", ",", "]")}.");
val totalStart = System.currentTimeMillis();
val runId = s"run-${totalStart}";
val nodes = (0 until withClients).map(45700 + _).map { p =>
val ip = "127.0.0.1";
val addr = s"${ip}:${p}";
val dirName = s"${ip}-port-${p}";
val dir = remoteDir / runId / dirName;
print(s"Created temporary directory for test node $addr: ${dir}, copying data...");
for (d <- copyDirectories) {
mkdir(dir / d);
cp.over(pwd / d, dir / d);
}
for (file <- copyFiles) {
os.copy(pwd / file, dir / file, createFolders = true);
}
println("done.");
NodeEntry(ip, p, dir.toString)
} toList;
val masters = masterBenches.map(_.remoteRunner(runnerAddr, masterAddr, nodes.size));
val logdir = logs / runId;
mkdir! logdir;
val resultsdir = results / runId;
mkdir! resultsdir;
if (!testing) {
%%.apply(root/'bin/'bash, "-c",s"./exp_setup.sh $resultsdir");
}
val nRunners = masters.size;
var errors = 0;
masters.zipWithIndex.foreach { case (master, i) =>
val experimentRunner = getExperimentRunner(master.symbol, resultsdir, testing, benchmarks);
println(s"Starting run [${i+1}/$nRunners]: ${master.label}");
val start = System.currentTimeMillis();
val r = fakeRemoteExperiment(experimentRunner, master, runId, logdir, nodes);
val end = System.currentTimeMillis();
val time = FiniteDuration(end-start, MILLISECONDS);
r match {
case Success(_) => println(s"Finished ${master.label} in ${format(time)}");
case Failure(e) => {
errors += 1;
println(s"Runner did not finish successfully: ${master.label} (${format(time)})");
Console.err.println(e);
e.printStackTrace(Console.err);
}
}
endSeparator(master.label, experimentRunner.errorLog(logdir));
endSeparator(master.label, experimentRunner.outputLog(logdir));
}
val totalEnd = System.currentTimeMillis();
val totalTime = FiniteDuration(totalEnd-totalStart, MILLISECONDS);
println("========");
println(s"Finished all runners in ${format(totalTime)}");
println(s"There were $errors errors. Logs can be found in ${logdir}");
println(s"Run the following command to cleanup when remote logs are no longer required:\n rm -rf $remoteDir");
}
@arg(doc ="Run local benchmarks only.")
@main
def local(testing: Boolean = false, impls: String = "", benchmarks: String = ""): Unit = {
val runners = runnersForImpl(impls, _.localRunner(runnerAddr));
val totalStart = System.currentTimeMillis();
val runId = s"run-${totalStart}";
val logdir = logs / runId;
mkdir! logdir;
val resultsdir = results / runId;
mkdir! resultsdir;
if (!testing) {
%%.apply(root/'bin/'bash, "-c",s"./exp_setup.sh $resultsdir");
}
val nRunners = runners.size;
var errors = 0;
runners.zipWithIndex.foreach { case (r, i) =>
try {
val experimentRunner = getExperimentRunner(r.symbol, resultsdir, testing, benchmarks);
println(s"Starting run [${i+1}/$nRunners]: ${r.label}");
val start = System.currentTimeMillis();
val runner = r.run(logdir);
val experimenter = experimentRunner.run(logdir);
experimenter.waitFor();
runner.destroy();
val end = System.currentTimeMillis();
val time = FiniteDuration(end-start, MILLISECONDS);
endSeparator(r.label, experimentRunner.errorLog(logdir));
endSeparator(r.label, experimentRunner.outputLog(logdir));
if (experimenter.exitValue() == 0) {
println(s"Finished ${r.label} in ${format(time)}");
} else {
errors += 1;
println(s"Runner did not finish successfully: ${r.label} (${format(time)})");
}
} catch {
case e: Throwable => e.printStackTrace(Console.err);
}
}
val totalEnd = System.currentTimeMillis();
val totalTime = FiniteDuration(totalEnd-totalStart, MILLISECONDS);
println("========");
println(s"Finished all runners in ${format(totalTime)}");
println(s"There were $errors errors. Logs can be found in ${logdir}");
}
private def runnersForImpl[T](impl_string: String, mapper: BenchmarkImpl => T): List[T] = {
val impls: Array[String] = impl_string.split(",")
val runners: List[T] = if (impls.isEmpty) {
implementations.values.map(mapper).toList;
} else {
impls.map(_.toUpperCase).flatMap(impl => {
val res: Option[BenchmarkImpl] = implementations.get(impl);
if (res.isEmpty) {
Console.err.println(s"No benchmark found for impl ${impl}!");
}
res.map(mapper)
}).toList
};
if (runners.isEmpty) {
Console.err.println(s"No benchmarks found!");
System.exit(1);
}
runners
}
private def endSeparator(label: String, log: File): Unit = {
val fw = new FileWriter(log, true);
val w = new PrintWriter(fw);
try {
w.println(s"===== END $label =====");
} finally {
w.flush();
w.close();
fw.close();
}
}
private def remoteExperiment(experimentRunner: BenchmarkRunner, master: BenchmarkRunner, runId: String, logDir: Path, nodes: List[NodeEntry]): Try[Unit] = {
Try {
val runner = master.run(logDir);
val pids = nodes.map { node =>
val pid = startClient(node, master.symbol, runId, masterAddr);
(node -> pid)
};
println(s"Got pids: $pids");
val experimenter = experimentRunner.run(logDir);
experimenter.waitFor();
runner.destroy();
pids.foreach {
case (node, Success(pid)) => {
val r = stopClient(node, pid);
println(s"Tried to stop client $node: $r");
}
case(node, Failure(_)) => Console.err.println(s"Could not stop client $node due to missing pid")
}
}
}
private def fakeRemoteExperiment(experimentRunner: BenchmarkRunner, master: BenchmarkRunner, runId: String, logDir: Path, nodes: List[NodeEntry]): Try[Unit] = {
Try {
val runner = master.run(logDir);
val pids = nodes.map { node =>
val pid = startFakeClient(node, master.symbol, runId, masterAddr);
(node -> pid)
};
println(s"Got pids: $pids");
val experimenter = experimentRunner.run(logDir);
experimenter.waitFor();
runner.destroy();
pids.foreach {
case (node, Success(pid)) => {
val r = stopFakeClient(node, pid);
println(s"Tried to stop client $node: $r");
}
case(node, Failure(_)) => Console.err.println(s"Could not stop client $node due to missing pid")
}
}
}
case class NodeEntry(ip: String, port: Int, benchDir: String)
private def readNodes(p: Path): List[NodeEntry] = {
if (exists! p) {
println(s"Reading nodes from '${p}'");
val nodesS = read! p;
val nodeLines = nodesS.split("\n").filterNot(_.contains("#")).toList;
nodeLines.map { l =>
val ls = l.split("""\s\|\s""");
println(s"Got ${ls.mkString}");
assert(ls.size == 2);
val node = ls(0).trim;
val path = ls(1).trim;
NodeEntry(node, 45678, path)
}
} else {
Console.err.println(s"Could not find nodes config file '${p}'");
System.exit(1);
???
}
}
val login = HostConfigProvider.fromLogin(PublicKeyLogin("lkroll", "/Users/lkroll/.ssh/id_rsa"));
private def startClient(node: NodeEntry, bench: String, runId: String, master: String): Try[Int] = {
println(s"Connecting to ${node}...");
val connRes = SSH(node.ip, login) { client =>
for {
r <- client.exec(s"source ~/.profile; cd ${node.benchDir}; ./client.sh --name $bench --master $master --runid $runId --publicif ${node.ip} --clientPort ${node.port}");
pid <- Try(r.stdOutAsString().trim.toInt)
} yield pid
};
println(s"Connection: $connRes");
connRes
}
private def stopClient(node: NodeEntry, pid: Int): Try[Unit] = {
println(s"Connecting to ${node}...");
val connRes = SSH(node.ip, login) { client =>
for {
r <- client.exec(s"kill -15 $pid")
} yield ()
};
println(s"Connection: $connRes");
connRes
}
private def startFakeClient(node: NodeEntry, bench: String, runId: String, master: String): Try[Int] = {
println(s"Starting ${node}...");
Try {
val wd = Path(node.benchDir);
val res = %%.apply(root/'bin/'bash, "-c", s"./client.sh --name $bench --master $master --runid $runId --publicif ${node.ip} --clientPort ${node.port}")(wd);
val connRes = res.out.string;
println(s"Result: $connRes");
connRes.trim.toInt
}
}
private def stopFakeClient(node: NodeEntry, pid: Int): Try[Unit] = {
println(s"Killing ${node}...");
Try {
val res = %%.apply(root/'bin/'bash, "-c",s"kill -15 $pid");
val connRes = res.out.string;
println(s"Connection: $connRes");
}
}