Skip to content

Commit 1442139

Browse files
committed
summary html file that runs both benchmarks
1 parent 9731eb3 commit 1442139

File tree

7 files changed

+114
-41
lines changed

7 files changed

+114
-41
lines changed

zaplib/examples/benchmark_json/generate_json.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# https://github.com/kostya/benchmarks/blob/1dd7deb29a813d1095e6062c25ad92bd81ce0273/json/generate_json.rb
2+
13
# frozen_string_literal: true
24

35
require 'json'
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<head>
2+
<meta charset="utf-8" />
3+
<style>
4+
body {
5+
padding: 50px;
6+
font-family: Georgia, 'Times New Roman', Times, serif;
7+
}
8+
9+
table {
10+
font-family: arial, sans-serif;
11+
border-collapse: collapse;
12+
width: 100%;
13+
}
14+
15+
td,
16+
th {
17+
border: 1px solid #dddddd;
18+
text-align: left;
19+
padding: 8px;
20+
}
21+
22+
tr:nth-child(even) {
23+
background-color: #dddddd;
24+
}
25+
</style>
26+
</head>
27+
<body style="margin: 0; overflow: hidden">
28+
<h1><a href="https://zaplib.com">Zaplib</a> (rust/wasm) vs JS: 100mb JSON parsing</h1>
29+
<p>
30+
This benchmark was adapted from <a href="https://github.com/kostya/benchmarks#json">kostya/benchmarks</a>, but adapted to work for JS (web) vs Zaplib (rust/wasm).
31+
</p>
32+
<p>
33+
To re-generate the json file locally, run the generate_json.rb file in this directory.
34+
</p>
35+
<p>
36+
It would be interesting augment the Rust benchmark with a simd-powered parsing library, but I wasn't able to get one to compile on my M1 mac... yet!
37+
</p>
38+
<p>
39+
You may notice that the Zaplib benchmark runs about 2x as <em>slow</em> if the devtools are open. We don't exactly know why this is – likely it's a debug or profiling mode that gets enabled.
40+
</p>
41+
<table>
42+
<tr>
43+
<th>Language</th>
44+
<th>Parsing Time (ms)</th>
45+
<th>Compute Time (ms</th>
46+
</tr>
47+
<tr>
48+
<td>JavaScript (JSON.parse)</td>
49+
<td id="js-parsing"></td>
50+
<td id="js-compute"></td>
51+
</tr>
52+
<tr>
53+
<td>Zaplib Rust Wasm (w/ Serde)</td>
54+
<td id="zaplib-serde-parsing"></td>
55+
<td id="zaplib-serde-compute"></td>
56+
</tr>
57+
</table>
58+
<script src="/zaplib/examples/benchmark_json/js/index.js"></script>
59+
<script type="text/javascript" src="/zaplib/web/dist/zaplib_runtime.development.js"></script>
60+
<script>
61+
async function repeatedBenchmark(name, func) {
62+
let totals = [0, 0];
63+
for (let i = 1; i <= 10; i++) {
64+
const [parsingT, computeT] = await func();
65+
totals[0] += parsingT;
66+
totals[1] += computeT;
67+
68+
document.getElementById(`${name}-parsing`).innerHTML = Math.round(totals[0] / i);
69+
document.getElementById(`${name}-compute`).innerHTML = Math.round(totals[1] / i);
70+
await new Promise(requestAnimationFrame);
71+
}
72+
}
73+
74+
(async function() {
75+
await repeatedBenchmark('js', benchmarkJS);
76+
77+
zaplib.initialize({ wasmModule: '/target/wasm32-unknown-unknown/release/benchmark_json_zaplib.wasm' }).then(() => {
78+
repeatedBenchmark('zaplib-serde', async () => (await zaplib.callRustAsync(""))[0]);
79+
});
80+
}());
81+
</script>
82+
</body>

zaplib/examples/benchmark_json/js/index.html

Lines changed: 0 additions & 6 deletions
This file was deleted.

zaplib/examples/benchmark_json/js/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
'use strict';
44

5-
function calc(text) {
6-
const jobj = JSON.parse(text);
7-
5+
function calc(jobj) {
86
const coordinates = jobj['coordinates'];
97
const len = coordinates.length;
108
let x = 0;
@@ -25,11 +23,18 @@ function calc(text) {
2523
};
2624
}
2725

28-
fetch('../data.json').then(response => response.text()).then(text => {
26+
const textP = fetch('/zaplib/examples/benchmark_json/data.json').then(response => response.text());
27+
28+
const benchmarkJS = async function() {
29+
const text = await textP;
30+
31+
const startP = performance.now()
32+
const jobj = JSON.parse(text);
33+
const endP = performance.now();
34+
2935
const start = performance.now()
30-
const results = calc(text);
36+
const results = calc(jobj);
3137
const end = performance.now();
32-
33-
console.log(`Time: ${end - start} ms`);
34-
});
3538

39+
return [endP-startP, end - start];
40+
}

zaplib/examples/benchmark_json/zaplib/index.html

Lines changed: 0 additions & 9 deletions
This file was deleted.

zaplib/examples/benchmark_json/zaplib/index.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

zaplib/examples/benchmark_json/zaplib/src/main.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ struct TestStruct {
1616
coordinates: Vec<Coordinate>,
1717
}
1818

19-
fn calc(s: &str) -> Coordinate {
20-
let jobj = serde_json::from_str::<TestStruct>(s).unwrap();
21-
19+
fn calc(jobj: TestStruct) -> Coordinate {
2220
let len = jobj.coordinates.len() as f64;
2321
let mut x = 0_f64;
2422
let mut y = 0_f64;
@@ -33,21 +31,28 @@ fn calc(s: &str) -> Coordinate {
3331
Coordinate { x: x / len, y: y / len, z: z / len }
3432
}
3533

36-
fn run() {
34+
fn call_rust(_name: String, _params: Vec<ZapParam>) -> Vec<ZapParam> {
3735
let mut file = UniversalFile::open("zaplib/examples/benchmark_json/data.json").unwrap();
3836
let mut s = String::new();
39-
file.read_to_string(&mut s);
37+
let ret = file.read_to_string(&mut s);
38+
if ret.is_err() {
39+
panic!("Failed to read file");
40+
}
41+
42+
let start_p = Instant::now();
43+
let jobj = serde_json::from_str::<TestStruct>(&s).unwrap();
44+
let end_p: UniversalInstant = Instant::now();
4045

4146
let start = Instant::now();
42-
calc(&s);
47+
calc(jobj);
4348
let end: UniversalInstant = Instant::now();
44-
log!("{:?}", end.duration_since(start));
45-
}
46-
47-
fn call_rust(_name: String, _params: Vec<ZapParam>) -> Vec<ZapParam> {
48-
run();
4949

50-
vec![]
50+
vec![
51+
vec![
52+
end_p.duration_since(start_p).as_millis() as u32,
53+
end.duration_since(start).as_millis() as u32
54+
].into_param()
55+
]
5156
}
5257

5358
register_call_rust!(call_rust);

0 commit comments

Comments
 (0)