-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpac_bench.rs
More file actions
639 lines (599 loc) · 20.7 KB
/
Copy pathpac_bench.rs
File metadata and controls
639 lines (599 loc) · 20.7 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//! Benchmark every available PAC evaluation path against the others on the
//! same PAC script and URLs:
//!
//! * **system** (backend-less Windows only) —
//! [`ProxyResolver::evaluate_pac_source`], i.e. WinHTTP
//! (`WinHttpGetProxyForUrl`).
//! * **native** — [`ProxyResolver::evaluate_pac`] with the in-process
//! QuickJS-NG engine (`--features pac-engine`).
//! * **wasmtime** — the same QuickJS-NG compiled to WebAssembly and run in a
//! Wasmtime sandbox (`--features pac-engine-wasmtime`, any platform
//! Cranelift can AOT-compile for), selected via
//! [`ResolverOptions::pac_backend`].
//! * **wasmtime-jit** — the same Wasmtime sandbox with Cranelift compiled into
//! the runtime (`--features pac-engine-wasmtime-jit`), so the raw guest is
//! compiled when the process starts instead of AOT-compiled by `build.rs`.
//! * **wasm2c** — the same wasm guest translated to portable C with WABT's
//! `wasm2c` (`--features pac-engine-wasm2c`, any platform with a C
//! compiler, including 32-bit armv7).
//!
//! All embedded backends run byte-identical engine + helper sources, so the
//! cross-check requires them to agree **exactly** on every URL and the process
//! exits non-zero on any diff. WinHTTP is allowed to differ (it e.g. drops a
//! trailing DIRECT); its diffs are reported but not fatal. For CI builds that
//! compile one backend per binary, `--backend` selects that path and
//! `--results-file` emits deterministic TSV for cross-process comparison.
//!
//! ```text
//! cargo run --release --example pac_bench \
//! --features "pac-engine pac-engine-wasmtime pac-engine-wasm2c"
//! cargo run --release --example pac_bench --features pac-engine -- \
//! --iterations 5000 --pac-script my.pac https://a.example/ http://b.corp/
//! ```
//!
//! In a backend-less Windows build the PAC script is served from an ephemeral
//! `127.0.0.1` HTTP endpoint because WinHTTP only loads PAC over http(s).
#[cfg(any(
feature = "pac-engine",
feature = "pac-engine-wasmtime",
feature = "pac-engine-wasmtime-jit",
feature = "pac-engine-wasm2c"
))]
use os_proxy_resolver::{PacBackendKind, ResolverOptions};
use os_proxy_resolver::{ProxyKind, ProxyResolver};
use std::time::{Duration, Instant};
use url::Url;
/// A small but non-trivial PAC script: a few helper calls and branches so the
/// measurement reflects real evaluation cost rather than a bare `return`.
/// Keep this identical to DEFAULT_PAC in bench/electron/main.js.
const DEFAULT_PAC: &str = r#"
function FindProxyForURL(url, host) {
if (isPlainHostName(host) ||
shExpMatch(host, "*.local") ||
(host === "127.0.0.1" &&
isInNet(host, "127.0.0.0", "255.0.0.0"))) {
return "DIRECT";
}
if (dnsDomainIs(host, ".corp.example.com") ||
shExpMatch(url, "http://intra.example.com/*")) {
return "PROXY proxy1.example.com:8080; PROXY proxy2.example.com:8080; DIRECT";
}
if (shExpMatch(host, "*.example.net")) {
return "SOCKS5 socks.example.com:1080; DIRECT";
}
return "PROXY edge.example.com:3128; DIRECT";
}
"#;
/// Keep this identical to DEFAULT_URLS in bench/electron/main.js.
const DEFAULT_URLS: &[&str] = &[
"http://plainhost/",
"https://db.corp.example.com/",
"http://intra.example.com/dashboard",
"https://cdn.example.net/asset.js",
"https://www.example.org/",
"http://127.0.0.1/",
];
struct Args {
iterations: usize,
pac_script: Option<String>,
backend: Option<String>,
results_file: Option<String>,
urls: Vec<String>,
}
fn parse_args() -> Args {
let mut iterations = 2000usize;
let mut pac_script = None;
let mut backend = None;
let mut results_file = None;
let mut urls = Vec::new();
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
match arg.as_str() {
"--iterations" => {
iterations = args
.next()
.and_then(|v| v.parse().ok())
.unwrap_or_else(|| usage_error("--iterations requires a positive integer"));
}
"--pac-script" => {
pac_script = Some(
args.next()
.unwrap_or_else(|| usage_error("--pac-script requires a value")),
);
}
"--backend" => {
backend = Some(
args.next()
.unwrap_or_else(|| usage_error("--backend requires a value")),
);
}
"--results-file" => {
results_file = Some(
args.next()
.unwrap_or_else(|| usage_error("--results-file requires a value")),
);
}
"-h" | "--help" => {
print_usage();
std::process::exit(0);
}
other if other.starts_with('-') && other != "-" => {
usage_error(&format!("unknown option: {other}"));
}
_ => urls.push(arg),
}
}
if iterations == 0 {
usage_error("--iterations must be greater than zero");
}
Args {
iterations,
pac_script,
backend,
results_file,
urls,
}
}
/// One PAC evaluation as run by a backend.
type EvalFn = Box<dyn Fn(&Url) -> Result<Vec<ProxyKind>, String>>;
/// One benchmarked evaluation path.
struct Backend {
label: &'static str,
/// Backends flagged `exact` must agree with each other byte-for-byte
/// (they run the same engine sources); a diff is a bug and fails the run.
exact: bool,
call: EvalFn,
}
/// Assembles every path compiled into this build.
fn backends(script: &str) -> Vec<Backend> {
let mut backends: Vec<Backend> = Vec::new();
// WinHTTP is the system evaluator only in a backend-less Windows build.
#[cfg(all(
windows,
not(any(
feature = "pac-engine",
feature = "pac-engine-wasmtime",
feature = "pac-engine-wasmtime-jit",
feature = "pac-engine-wasm2c"
))
))]
{
let resolver = ProxyResolver::new();
let pac_url = serve_pac(script.to_string());
println!(" winhttp : PAC served at {pac_url}");
backends.push(Backend {
label: "winhttp",
exact: false,
call: Box::new(move |u| {
resolver
.evaluate_pac_source(&pac_url, u)
.map_err(|e| e.to_string())
}),
});
}
#[cfg(feature = "pac-engine")]
{
let mut options = ResolverOptions::default();
options.pac_backend = PacBackendKind::Native;
let resolver = ProxyResolver::with_options(options);
let script = script.to_string();
backends.push(Backend {
label: "native",
exact: true,
call: Box::new(move |u| resolver.evaluate_pac(&script, u).map_err(|e| e.to_string())),
});
}
#[cfg(feature = "pac-engine-wasmtime")]
{
let mut options = ResolverOptions::default();
options.pac_backend = PacBackendKind::Wasmtime;
let resolver = ProxyResolver::with_options(options);
let script = script.to_string();
backends.push(Backend {
label: "wasmtime",
exact: true,
call: Box::new(move |u| resolver.evaluate_pac(&script, u).map_err(|e| e.to_string())),
});
}
#[cfg(feature = "pac-engine-wasm2c")]
{
let mut options = ResolverOptions::default();
options.pac_backend = PacBackendKind::Wasm2c;
let resolver = ProxyResolver::with_options(options);
let script = script.to_string();
backends.push(Backend {
label: "wasm2c",
exact: true,
call: Box::new(move |u| resolver.evaluate_pac(&script, u).map_err(|e| e.to_string())),
});
}
#[cfg(feature = "pac-engine-wasmtime-jit")]
{
let mut options = ResolverOptions::default();
options.pac_backend = PacBackendKind::WasmtimeJit;
let resolver = ProxyResolver::with_options(options);
let script = script.to_string();
backends.push(Backend {
label: "wasmtime-jit",
exact: true,
call: Box::new(move |u| resolver.evaluate_pac(&script, u).map_err(|e| e.to_string())),
});
}
backends
}
fn main() {
let args = parse_args();
let script = match &args.pac_script {
Some(path) => std::fs::read_to_string(path).unwrap_or_else(|e| {
eprintln!("error: cannot read PAC file {path}: {e}");
std::process::exit(1);
}),
None => DEFAULT_PAC.to_string(),
};
let raw_urls: Vec<String> = if args.urls.is_empty() {
DEFAULT_URLS.iter().map(|s| s.to_string()).collect()
} else {
args.urls.clone()
};
let urls: Vec<Url> = raw_urls
.iter()
.map(|u| {
Url::parse(u).unwrap_or_else(|e| {
eprintln!("error: invalid URL {u}: {e}");
std::process::exit(1);
})
})
.collect();
println!("PAC benchmark");
println!(
" iterations : {} per engine (across {} URLs)",
args.iterations,
urls.len()
);
println!(
" pac source : {}",
args.pac_script.as_deref().unwrap_or("<built-in>")
);
let mut backends = backends(&script);
if let Some(requested) = &args.backend {
let available = backends
.iter()
.map(|backend| backend.label)
.collect::<Vec<_>>()
.join(", ");
backends.retain(|backend| backend.label == requested);
if backends.is_empty() {
usage_error(&format!(
"backend {requested:?} is not compiled in (available: {available})"
));
}
}
if args.results_file.is_some() && backends.len() != 1 {
usage_error("--results-file requires selecting exactly one backend with --backend");
}
println!(
" backends : {}",
backends
.iter()
.map(|b| b.label)
.collect::<Vec<_>>()
.join(", ")
);
report_binary_size();
println!();
if backends.is_empty() {
println!(
"no PAC backend compiled in — build with `--features pac-engine` \
(and/or `pac-engine-wasmtime`, `pac-engine-wasm2c`)."
);
return;
}
// Cross-check all backends on each URL before timing, so divergences
// (e.g. WinHTTP dropping a trailing DIRECT) are reported up front. The
// `exact` embedded backends run byte-identical engine sources and MUST
// agree; anything else is a bug in a wasm backend.
let exact_diffs = cross_check(&backends, &urls, args.results_file.as_deref());
let mut stats: Vec<Stats> = Vec::new();
for backend in &backends {
let s = bench(backend.label, args.iterations, &urls, &backend.call);
s.print();
stats.push(s);
}
let timed_errors: usize = stats.iter().map(|stats| stats.errors).sum();
if timed_errors == 0 {
compare(&stats);
}
if exact_diffs > 0 {
eprintln!();
eprintln!(
"error: the embedded backends diverged on {exact_diffs} URL(s) — \
they run identical engine sources, so this is a bug."
);
std::process::exit(1);
}
if timed_errors > 0 {
eprintln!();
eprintln!("error: PAC benchmark had {timed_errors} timed call error(s)");
std::process::exit(1);
}
}
/// Returns the number of URLs on which the `exact` backends disagreed.
fn cross_check(backends: &[Backend], urls: &[Url], results_file: Option<&str>) -> usize {
use std::io::Write;
let mut result_output = results_file.map(|path| {
let file = std::fs::File::create(path).unwrap_or_else(|e| {
eprintln!("error: cannot create results file {path}: {e}");
std::process::exit(1);
});
std::io::BufWriter::new(file)
});
let mut diffs = 0;
let mut exact_diffs = 0;
for u in urls {
let results: Vec<(usize, String)> = backends
.iter()
.enumerate()
.map(|(i, b)| {
let r = (b.call)(u)
.map(render)
.unwrap_or_else(|e| format!("<error: {e}>"));
(i, r)
})
.collect();
if let Some(output) = &mut result_output {
writeln!(output, "{u}\t{}", results[0].1).unwrap_or_else(|e| {
eprintln!("error: cannot write results file: {e}");
std::process::exit(1);
});
}
let all_equal = results.windows(2).all(|w| w[0].1 == w[1].1);
if !all_equal {
diffs += 1;
println!(" diff {u}");
for (i, r) in &results {
println!(" {:<8} -> {r}", backends[*i].label);
}
}
let exact: Vec<&String> = results
.iter()
.filter(|(i, _)| backends[*i].exact)
.map(|(_, r)| r)
.collect();
if exact.windows(2).any(|w| w[0] != w[1]) {
exact_diffs += 1;
}
}
if let Some(output) = &mut result_output {
output.flush().unwrap_or_else(|e| {
eprintln!("error: cannot flush results file: {e}");
std::process::exit(1);
});
}
if backends.len() == 1 {
println!(
"result capture: {} evaluated all {} URLs",
backends[0].label,
urls.len()
);
} else if diffs == 0 {
println!(
"cross-check: all {} backends agree on all {} URLs",
backends.len(),
urls.len()
);
} else {
println!("cross-check: {diffs} URL(s) differ between backends (see above)");
}
println!();
exact_diffs
}
fn render(list: Vec<ProxyKind>) -> String {
list.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("; ")
}
fn report_binary_size() {
if let Ok(exe) = std::env::current_exe() {
if let Ok(meta) = std::fs::metadata(&exe) {
println!(
" binary : {} ({:.2} MiB)",
exe.display(),
meta.len() as f64 / (1024.0 * 1024.0)
);
}
}
#[cfg(feature = "pac-engine-wasmtime")]
println!(
" wasm guest : {:.2} MiB AOT artifact embedded (plus the Wasmtime \
runtime; compare against a `--features pac-engine`-only build for \
the full delta)",
os_proxy_resolver::pac_wasm_artifact_size() as f64 / (1024.0 * 1024.0)
);
}
/// Timing statistics for one engine, in nanoseconds.
struct Stats {
label: &'static str,
samples: Vec<u128>,
errors: usize,
first_error: Option<String>,
}
impl Stats {
fn mean(&self) -> f64 {
if self.samples.is_empty() {
return 0.0;
}
self.samples.iter().sum::<u128>() as f64 / self.samples.len() as f64
}
/// `p` in `0.0..=1.0`. Requires `samples` sorted ascending.
fn percentile(&self, p: f64) -> u128 {
if self.samples.is_empty() {
return 0;
}
let idx = ((self.samples.len() - 1) as f64 * p).round() as usize;
self.samples[idx]
}
fn print(&self) {
let n = self.samples.len();
println!("{}", self.label);
if n == 0 {
println!(" no successful samples ({} errors)", self.errors);
if let Some(error) = &self.first_error {
println!(" first error: {error}");
}
return;
}
println!(" calls : {n} ({} errors)", self.errors);
if let Some(error) = &self.first_error {
println!(" first error: {error}");
}
println!(" mean : {}", fmt_ns(self.mean() as u128));
println!(" p50 : {}", fmt_ns(self.percentile(0.50)));
println!(" p90 : {}", fmt_ns(self.percentile(0.90)));
println!(" p99 : {}", fmt_ns(self.percentile(0.99)));
println!(
" min/max : {} / {}",
fmt_ns(self.samples[0]),
fmt_ns(self.samples[n - 1])
);
let per_sec = if self.mean() > 0.0 {
1e9 / self.mean()
} else {
0.0
};
println!(" throughput: {per_sec:.0} calls/s");
}
}
fn bench(label: &'static str, iterations: usize, urls: &[Url], call: &EvalFn) -> Stats {
// Warm up so first-call costs (PAC download/compile, WinHTTP autoproxy
// cache priming, wasm instantiation) don't skew the samples.
for u in urls {
let _ = call(u);
}
let mut samples = Vec::with_capacity(iterations);
let mut errors = 0;
let mut first_error = None;
for i in 0..iterations {
let u = &urls[i % urls.len()];
let start = Instant::now();
let result = call(u);
let elapsed = start.elapsed();
match result {
Ok(_) => samples.push(elapsed.as_nanos()),
Err(error) => {
errors += 1;
first_error.get_or_insert(error);
}
}
}
samples.sort_unstable();
Stats {
label,
samples,
errors,
first_error,
}
}
/// Pairwise mean comparison of all timed backends.
fn compare(stats: &[Stats]) {
if stats.len() < 2 {
return;
}
println!();
for i in 0..stats.len() {
for j in (i + 1)..stats.len() {
let (a, b) = (&stats[i], &stats[j]);
let (ma, mb) = (a.mean(), b.mean());
if ma <= 0.0 || mb <= 0.0 {
continue;
}
if ma < mb {
println!(
"=> {} is {:.2}x faster than {} (by mean)",
a.label,
mb / ma,
b.label
);
} else {
println!(
"=> {} is {:.2}x faster than {} (by mean)",
b.label,
ma / mb,
a.label
);
}
}
}
}
fn fmt_ns(ns: u128) -> String {
let d = Duration::from_nanos(ns as u64);
if d.as_secs() >= 1 {
format!("{:.3} s", d.as_secs_f64())
} else if d.as_millis() >= 1 {
format!("{:.3} ms", d.as_secs_f64() * 1e3)
} else {
format!("{:.1} us", d.as_secs_f64() * 1e6)
}
}
/// Serve `script` from an ephemeral `127.0.0.1` HTTP endpoint for the whole
/// life of the process (WinHTTP only loads PAC over http(s)). Each connection
/// is answered with the script and closed; the accept loop lives on a detached
/// thread.
#[cfg(all(
windows,
not(any(
feature = "pac-engine",
feature = "pac-engine-wasmtime",
feature = "pac-engine-wasmtime-jit",
feature = "pac-engine-wasm2c"
))
))]
fn serve_pac(script: String) -> String {
use std::io::{Read, Write};
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:0").unwrap_or_else(|e| {
eprintln!("error: cannot start local PAC server: {e}");
std::process::exit(1);
});
let addr = listener.local_addr().expect("local address");
let body = script.into_bytes();
std::thread::spawn(move || {
let head = format!(
"HTTP/1.1 200 OK\r\n\
Content-Type: application/x-ns-proxy-autoconfig\r\n\
Content-Length: {}\r\n\
Connection: close\r\n\r\n",
body.len()
);
for stream in listener.incoming() {
let Ok(mut stream) = stream else { continue };
let mut buf = [0u8; 1024];
let _ = stream.read(&mut buf);
let _ = stream.write_all(head.as_bytes());
let _ = stream.write_all(&body);
let _ = stream.flush();
}
});
format!("http://{addr}/proxy.pac")
}
fn print_usage() {
eprintln!(
"usage: pac_bench [--iterations N] [--pac-script <path>]\n\
[--backend <name>] [--results-file <path>] [<url>...]\n\
\n\
Times every compiled-in PAC path (WinHTTP on backend-less Windows,\n\
native QuickJS, Wasmtime AOT/JIT, and wasm2c) on the same PAC script\n\
and URLs. `--backend` times one compiled-in path. With exactly one\n\
selected backend, `--results-file` writes deterministic TSV rows for\n\
cross-process result comparison."
);
}
fn usage_error(msg: &str) -> ! {
eprintln!("error: {msg}");
print_usage();
std::process::exit(2);
}