Skip to content

Commit 9731eb3

Browse files
committed
JSON Benchmark
1 parent 64bd9f0 commit 9731eb3

File tree

10 files changed

+152
-0
lines changed

10 files changed

+152
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ cef.log
2222
/website_dev
2323

2424
screenshots/
25+
26+
zaplib/examples/benchmark_json/data.json

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ resolver = "2"
33
members = [
44
"zaplib/cargo-zaplib",
55
"zaplib/ci",
6+
"zaplib/examples/benchmark_json/zaplib",
67
"zaplib/examples/example_bigedit",
78
"zaplib/examples/example_bigedit/http",
89
"zaplib/examples/example_bigedit/hub",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require 'json'
4+
5+
x = []
6+
7+
524_288.times do
8+
h = {
9+
'x' => rand * -10e-30,
10+
'y' => rand * 10e30,
11+
'z' => rand,
12+
'name' => "#{('a'..'z').to_a.sample(6).join} #{rand(10_000)}",
13+
'opts' => { '1' => [1, true] }
14+
}
15+
x << h
16+
end
17+
18+
File.write(
19+
'data.json',
20+
JSON.pretty_generate('coordinates' => x, 'info' => 'some info')
21+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<head>
2+
<meta charset="utf-8" />
3+
</head>
4+
<body style="margin: 0; overflow:hidden;">
5+
<script type="module" src="index.js"></script>
6+
</body>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Based on https://github.com/kostya/benchmarks/blob/1dd7deb29a813d1095e6062c25ad92bd81ce0273/json/test.js
2+
3+
'use strict';
4+
5+
function calc(text) {
6+
const jobj = JSON.parse(text);
7+
8+
const coordinates = jobj['coordinates'];
9+
const len = coordinates.length;
10+
let x = 0;
11+
let y = 0;
12+
let z = 0;
13+
14+
for (let i = 0; i < coordinates.length; i++) {
15+
const coord = coordinates[i];
16+
x += coord['x'];
17+
y += coord['y'];
18+
z += coord['z'];
19+
}
20+
21+
return {
22+
x: x / len,
23+
y: y / len,
24+
z: z / len
25+
};
26+
}
27+
28+
fetch('../data.json').then(response => response.text()).then(text => {
29+
const start = performance.now()
30+
const results = calc(text);
31+
const end = performance.now();
32+
33+
console.log(`Time: ${end - start} ms`);
34+
});
35+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "benchmark_json_zaplib"
3+
version = "0.0.1"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
zaplib = { path = "../../../main" }
9+
serde_json = "1.0.79"
10+
serde = { version = "1.0.136", features = ["derive"] }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<head>
2+
<meta charset="utf-8" />
3+
</head>
4+
5+
<body style="margin: 0; overflow:hidden;">
6+
<script type="text/javascript" src="/zaplib/web/dist/zaplib_runtime.development.js"></script>
7+
<script type="module" src="index.js"></script>
8+
<div id="root" style="height: 100%; width: 100%;"></div>
9+
</body>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const init = async () => {
2+
await zaplib.initialize({ wasmModule: '/target/wasm32-unknown-unknown/release/benchmark_json_zaplib.wasm' });
3+
await zaplib.callRustAsync("");
4+
}
5+
6+
init();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Based on https://github.com/kostya/benchmarks/blob/1dd7deb29a813d1095e6062c25ad92bd81ce0273/json/json.rs/src/json_struct.rs
2+
3+
use serde::Deserialize;
4+
use std::io::Read;
5+
use zaplib::*;
6+
7+
#[derive(Deserialize, PartialEq)]
8+
struct Coordinate {
9+
x: f64,
10+
y: f64,
11+
z: f64,
12+
}
13+
14+
#[derive(Deserialize)]
15+
struct TestStruct {
16+
coordinates: Vec<Coordinate>,
17+
}
18+
19+
fn calc(s: &str) -> Coordinate {
20+
let jobj = serde_json::from_str::<TestStruct>(s).unwrap();
21+
22+
let len = jobj.coordinates.len() as f64;
23+
let mut x = 0_f64;
24+
let mut y = 0_f64;
25+
let mut z = 0_f64;
26+
27+
for coord in &jobj.coordinates {
28+
x += coord.x;
29+
y += coord.y;
30+
z += coord.z;
31+
}
32+
33+
Coordinate { x: x / len, y: y / len, z: z / len }
34+
}
35+
36+
fn run() {
37+
let mut file = UniversalFile::open("zaplib/examples/benchmark_json/data.json").unwrap();
38+
let mut s = String::new();
39+
file.read_to_string(&mut s);
40+
41+
let start = Instant::now();
42+
calc(&s);
43+
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();
49+
50+
vec![]
51+
}
52+
53+
register_call_rust!(call_rust);

0 commit comments

Comments
 (0)