-
Notifications
You must be signed in to change notification settings - Fork 37
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
stevekrouse
wants to merge
5
commits into
main
Choose a base branch
from
benchmark_json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
JSON Benchmark #166
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ cef.log | |
/website_dev | ||
|
||
screenshots/ | ||
|
||
zaplib/examples/benchmark_json/data.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.