-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.dart
191 lines (167 loc) · 5.65 KB
/
benchmark.dart
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
import 'dart:io';
Future<void> bench(
String name,
String cmd, {
required String path,
required String benchmarkCmd,
String? warmupCmd,
String? setupCmd,
String? versionCmd,
String? cwd,
}) async {
print('=== BEGIN $name ===');
final log = (File('$path/$name.log')..createSync(recursive: true))
.openWrite(mode: FileMode.append);
ProcessResult run(String cmd, [StringSink? sink]) {
ProcessResult? r;
try {
r = Process.runSync('sh', ['-c', cmd], workingDirectory: cwd);
(sink ?? log)
..writeln(r.stdout)
..writeln(r.stderr);
return r;
} catch (err, st) {
Error.throwWithStackTrace('CMD "$cmd" [${r?.exitCode}]: $err', st);
}
}
Process? process;
try {
if (versionCmd != null) run(versionCmd);
if (setupCmd != null) run(setupCmd);
process = await Process.start(
'sh',
['-c', 'time $cmd'],
workingDirectory: cwd,
mode: ProcessStartMode.normal,
);
process.stderr.forEach((bytes) => log.add(bytes)).whenComplete(log.close);
// Scientifically chosen, *correct* wait period for the server to come up.
await Future.delayed(Duration(seconds: 2));
run(warmupCmd ?? benchmarkCmd, StringBuffer());
print(run(benchmarkCmd).stdout);
} finally {
if (process != null) {
run('kill \$(lsof -t -i:3000)');
}
}
print('=== END: $name ===\n');
}
Future<void> runHelloWorld() async {
const benchmarkCmd = 'wrk -c 10 -t 10 -d 30s --latency http://localhost:3000';
const warmupCmd = 'wrk -c 10 -t 10 -d 5s http://localhost:3000';
Future<void> run(
String name,
String cmd, {
String? setupCmd,
String? versionCmd,
String? cwd,
}) =>
bench(
name,
cmd,
path: 'output_hello_world',
setupCmd: setupCmd,
versionCmd: versionCmd,
cwd: cwd,
benchmarkCmd: benchmarkCmd,
warmupCmd: warmupCmd,
);
await run('node', 'node node.js', versionCmd: 'node -v');
await run('deno', 'deno run --allow-net deno.ts',
versionCmd: 'deno --version');
await run('bun', 'bun bun.ts', versionCmd: 'bun --version');
await run('go', '/tmp/goserver',
setupCmd: 'go build -o /tmp/goserver go.go', versionCmd: 'go version');
const dartVersionCmd = 'dart --version';
await run('dart_jit', 'dart run dart.dart', versionCmd: dartVersionCmd);
await run(
'dart_aot',
'/tmp/dart_aot',
setupCmd: 'dart compile exe dart.dart -o /tmp/dart_aot',
versionCmd: dartVersionCmd,
);
const axumBuild =
'cargo +nightly build -Z unstable-options --release --out-dir=/tmp/';
const rustVersionCmd = 'cargo +nightly version';
await run('axum_mt', '/tmp/hello_world_axum_server m',
setupCmd: axumBuild, versionCmd: rustVersionCmd, cwd: 'axum');
await run('axum_st', '/tmp/hello_world_axum_server',
setupCmd: axumBuild, versionCmd: rustVersionCmd, cwd: 'axum');
final dartAxumVersionCmd = '$dartVersionCmd && $rustVersionCmd';
await run(
'dart_axum_jit',
'dart --enable-experiment=native-assets run bin/main.dart',
setupCmd: 'dart --enable-experiment=native-assets run bin/main.dart build',
versionCmd: dartAxumVersionCmd,
cwd: 'dart_axum_tokio',
);
await run(
'dart_axum_aot',
'/tmp/dart_axum_aot',
setupCmd:
'dart --enable-experiment=native-assets compile exe bin/main.dart -o /tmp/dart_axum_aot',
versionCmd: dartAxumVersionCmd,
cwd: 'dart_axum_tokio',
);
}
Future<void> runJsonEcho() async {
const script = 'wrk_post.lua';
final benchmarkCmd =
'wrk -c 10 -t 10 -d 30s --latency -s $script http://localhost:3000/';
final warmupCmd = 'wrk -c 10 -t 10 -d 5s -s $script http://localhost:3000/';
Future<void> run(
String name,
String cmd, {
String? setupCmd,
String? versionCmd,
String? cwd,
}) =>
bench(
name,
cmd,
path: 'output_json_echo',
setupCmd: setupCmd,
versionCmd: versionCmd,
cwd: cwd,
benchmarkCmd: benchmarkCmd,
warmupCmd: warmupCmd,
);
await run('node', 'node node.js', versionCmd: 'node -v');
await run('deno', 'deno run --allow-net deno.ts',
versionCmd: 'deno --version');
await run('bun', 'bun bun.ts', versionCmd: 'bun --version');
await run('go', '/tmp/goserver',
setupCmd: 'go build -o /tmp/goserver go.go', versionCmd: 'go version');
const dartVersionCmd = 'dart --version';
await run('dart_jit', 'dart run dart.dart', versionCmd: dartVersionCmd);
await run(
'dart_aot',
'/tmp/dart_aot',
setupCmd: 'dart compile exe dart.dart -o /tmp/dart_aot',
versionCmd: dartVersionCmd,
);
const axumBuild =
'cargo +nightly build -Z unstable-options --release --out-dir=/tmp/';
const rustVersionCmd = 'cargo +nightly version';
await run('axum_mt_serde', '/tmp/hello_world_axum_server -m',
setupCmd: axumBuild, versionCmd: rustVersionCmd, cwd: 'axum');
await run('axum_st_serde', '/tmp/hello_world_axum_server',
setupCmd: axumBuild, versionCmd: rustVersionCmd, cwd: 'axum');
await run('axum_mt_simd', '/tmp/hello_world_axum_server -m -s',
setupCmd: axumBuild, versionCmd: rustVersionCmd, cwd: 'axum');
await run('axum_st_simd', '/tmp/hello_world_axum_server -s',
setupCmd: axumBuild, versionCmd: rustVersionCmd, cwd: 'axum');
final dartAxumVersionCmd = '$dartVersionCmd && $rustVersionCmd';
await run(
'dart_axum_aot',
'/tmp/dart_axum_aot',
setupCmd:
'dart --enable-experiment=native-assets compile exe bin/main.dart -o /tmp/dart_axum_aot',
versionCmd: dartAxumVersionCmd,
cwd: 'dart_axum_tokio',
);
}
Future<void> main(List<String> arguments) async {
//await runHelloWorld();
await runJsonEcho();
}