Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Benchmark #166

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ cef.log
/website_dev

screenshots/

zaplib/examples/benchmark_json/data.json
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"zaplib/cargo-zaplib",
"zaplib/ci",
"zaplib/examples/benchmark_json/zaplib",
"zaplib/examples/example_bigedit",
"zaplib/examples/example_bigedit/http",
"zaplib/examples/example_bigedit/hub",
Expand Down
23 changes: 23 additions & 0 deletions zaplib/examples/benchmark_json/generate_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# https://github.com/kostya/benchmarks/blob/1dd7deb29a813d1095e6062c25ad92bd81ce0273/json/generate_json.rb

# frozen_string_literal: true

require 'json'

x = []

524_288.times do
h = {
'x' => rand * -10e-30,
'y' => rand * 10e30,
'z' => rand,
'name' => "#{('a'..'z').to_a.sample(6).join} #{rand(10_000)}",
'opts' => { '1' => [1, true] }
}
x << h
end

File.write(
'data.json',
JSON.pretty_generate('coordinates' => x, 'info' => 'some info')
)
82 changes: 82 additions & 0 deletions zaplib/examples/benchmark_json/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<head>
<meta charset="utf-8" />
<style>
body {
padding: 50px;
font-family: Georgia, 'Times New Roman', Times, serif;
}

table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body style="margin: 0; overflow: hidden">
<h1><a href="https://zaplib.com">Zaplib</a> (rust/wasm) vs JS: 100mb JSON parsing</h1>
<p>
This benchmark was adapted from <a href="https://github.com/kostya/benchmarks#json">kostya/benchmarks</a> to work for JS (web) vs Zaplib (rust/wasm).
</p>
<p>
To re-generate the json file locally, run the <code>generate_json.rb</code> file in this directory.
</p>
<p>
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!
</p>
<p>
You may notice that the Zaplib benchmark runs about 2x <em>slower</em> (about the same time as the JS) if the devtools are open. We don't exactly know why this is – likely it's a debug or profiling mode that gets enabled.
</p>
<table>
<tr>
<th>Language</th>
<th>Parsing Time (ms)</th>
<th>Compute Time (ms</th>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<th>Compute Time (ms</th>
<th>Compute Time (ms)</th>

</tr>
<tr>
<td>JavaScript (<code>JSON.parse</code>)</td>
<td id="js-parsing"></td>
<td id="js-compute"></td>
</tr>
<tr>
<td>Zaplib Rust Wasm (w/ Serde)</td>
<td id="zaplib-serde-parsing"></td>
<td id="zaplib-serde-compute"></td>
</tr>
</table>
<script src="/zaplib/examples/benchmark_json/js/index.js"></script>
<script type="text/javascript" src="/zaplib/web/dist/zaplib_runtime.development.js"></script>
<script>
async function repeatedBenchmark(name, func) {
let totals = [0, 0];
for (let i = 1; i <= 10; i++) {
const [parsingT, computeT] = await func();
totals[0] += parsingT;
totals[1] += computeT;

document.getElementById(`${name}-parsing`).innerHTML = Math.round(totals[0] / i);
document.getElementById(`${name}-compute`).innerHTML = Math.round(totals[1] / i);
await new Promise(requestAnimationFrame);
}
}

(async function() {
await repeatedBenchmark('js', benchmarkJS);

zaplib.initialize({ wasmModule: '/target/wasm32-unknown-unknown/release/benchmark_json_zaplib.wasm' }).then(() => {
repeatedBenchmark('zaplib-serde', async () => (await zaplib.callRustAsync(""))[0]);
});
}());
</script>
</body>
40 changes: 40 additions & 0 deletions zaplib/examples/benchmark_json/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Based on https://github.com/kostya/benchmarks/blob/1dd7deb29a813d1095e6062c25ad92bd81ce0273/json/test.js

'use strict';

function calc(jobj) {
const coordinates = jobj['coordinates'];
const len = coordinates.length;
let x = 0;
let y = 0;
let z = 0;

for (let i = 0; i < coordinates.length; i++) {
const coord = coordinates[i];
x += coord['x'];
y += coord['y'];
z += coord['z'];
}

return {
x: x / len,
y: y / len,
z: z / len
};
}

const textP = fetch('/zaplib/examples/benchmark_json/data.json').then(response => response.text());

const benchmarkJS = async function() {
const text = await textP;

const startP = performance.now()
const jobj = JSON.parse(text);
const endP = performance.now();

const start = performance.now()
const results = calc(jobj);
const end = performance.now();

return [endP-startP, end - start];
}
10 changes: 10 additions & 0 deletions zaplib/examples/benchmark_json/zaplib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "benchmark_json_zaplib"
version = "0.0.1"
edition = "2021"
publish = false

[dependencies]
zaplib = { path = "../../../main" }
serde_json = "1.0.79"
serde = { version = "1.0.136", features = ["derive"] }
53 changes: 53 additions & 0 deletions zaplib/examples/benchmark_json/zaplib/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Based on https://github.com/kostya/benchmarks/blob/1dd7deb29a813d1095e6062c25ad92bd81ce0273/json/json.rs/src/json_struct.rs

use serde::Deserialize;
use std::io::Read;
use zaplib::*;

#[derive(Deserialize, PartialEq)]
struct Coordinate {
x: f64,
y: f64,
z: f64,
}

#[derive(Deserialize)]
struct TestStruct {
coordinates: Vec<Coordinate>,
}

fn calc(jobj: TestStruct) -> Coordinate {
let len = jobj.coordinates.len() as f64;
let mut x = 0_f64;
let mut y = 0_f64;
let mut z = 0_f64;

for coord in &jobj.coordinates {
x += coord.x;
y += coord.y;
z += coord.z;
}

Coordinate { x: x / len, y: y / len, z: z / len }
}

fn call_rust(_name: String, _params: Vec<ZapParam>) -> Vec<ZapParam> {
let mut file = UniversalFile::open("zaplib/examples/benchmark_json/data.json").unwrap();
let mut s = String::new();
let ret = file.read_to_string(&mut s);
if ret.is_err() {
panic!("Failed to read file");
}

let start_p = Instant::now();
let jobj = serde_json::from_str::<TestStruct>(&s).unwrap();
let end_p: UniversalInstant = Instant::now();

let start = Instant::now();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use universal instant here

calc(jobj);
let end: UniversalInstant = Instant::now();

vec![vec![end_p.duration_since(start_p).as_millis() as u32, end.duration_since(start).as_millis() as u32].into_param()]
}

register_call_rust!(call_rust);