|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 | 4 | #![allow(dead_code)]
|
5 |
| -use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 5 | +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; |
6 | 6 | use llrt_json::{parse::json_parse, stringify::json_stringify};
|
7 | 7 | use rquickjs::{Context, Runtime};
|
8 | 8 |
|
9 | 9 | static JSON: &str = r#"{"organization":{"name":"TechCorp","founding_year":2000,"departments":[{"name":"Engineering","head":{"name":"Alice Smith","title":"VP of Engineering","contact":{"email":"alice.smith@techcorp.com","phone":"+1 (555) 123-4567"}},"employees":[{"id":101,"name":"Bob Johnson","position":"Software Engineer","contact":{"email":"bob.johnson@techcorp.com","phone":"+1 (555) 234-5678"},"projects":[{"project_id":"P001","name":"Project A","status":"In Progress","description":"Developing a revolutionary software solution for clients.","start_date":"2023-01-15","end_date":null,"team":[{"id":201,"name":"Sara Davis","role":"UI/UX Designer"},{"id":202,"name":"Charlie Brown","role":"Quality Assurance Engineer"}]},{"project_id":"P002","name":"Project B","status":"Completed","description":"Upgrading existing systems to enhance performance.","start_date":"2022-05-01","end_date":"2022-11-30","team":[{"id":203,"name":"Emily White","role":"Systems Architect"},{"id":204,"name":"James Green","role":"Database Administrator"}]}]},{"id":102,"name":"Carol Williams","position":"Senior Software Engineer","contact":{"email":"carol.williams@techcorp.com","phone":"+1 (555) 345-6789"},"projects":[{"project_id":"P001","name":"Project A","status":"In Progress","description":"Working on the backend development of Project A.","start_date":"2023-01-15","end_date":null,"team":[{"id":205,"name":"Alex Turner","role":"DevOps Engineer"},{"id":206,"name":"Mia Garcia","role":"Software Developer"}]},{"project_id":"P003","name":"Project C","status":"Planning","description":"Researching and planning for a future project.","start_date":null,"end_date":null,"team":[]}]}]},{"name":"Marketing","head":{"name":"David Brown","title":"VP of Marketing","contact":{"email":"david.brown@techcorp.com","phone":"+1 (555) 456-7890"}},"employees":[{"id":201,"name":"Eva Miller","position":"Marketing Specialist","contact":{"email":"eva.miller@techcorp.com","phone":"+1 (555) 567-8901"},"campaigns":[{"campaign_id":"C001","name":"Product Launch","status":"Upcoming","description":"Planning for the launch of a new product line.","start_date":"2023-03-01","end_date":null,"team":[{"id":301,"name":"Oliver Martinez","role":"Graphic Designer"},{"id":302,"name":"Sophie Johnson","role":"Content Writer"}]},{"campaign_id":"C002","name":"Brand Awareness","status":"Ongoing","description":"Executing strategies to increase brand visibility.","start_date":"2022-11-15","end_date":"2023-01-31","team":[{"id":303,"name":"Liam Taylor","role":"Social Media Manager"},{"id":304,"name":"Ava Clark","role":"Marketing Analyst"}]}]}]}]}}"#;
|
10 | 10 |
|
11 | 11 | static JSON_MIN: &str = r#"{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}"#;
|
12 | 12 |
|
13 |
| -// fn generate_json(child_json: &str, size: usize) -> String { |
14 |
| -// let mut json = String::with_capacity(child_json.len() * size); |
15 |
| -// json.push('{'); |
16 |
| -// for i in 0..size { |
17 |
| -// json.push_str("\"obj"); |
18 |
| -// json.push_str(&i.to_string()); |
19 |
| -// json.push_str("\":"); |
20 |
| -// json.push_str(child_json); |
21 |
| -// json.push(','); |
22 |
| -// } |
23 |
| -// json.push_str("\"array\":["); |
24 |
| -// for i in 0..size { |
25 |
| -// json.push_str(child_json); |
26 |
| -// if i < size - 1 { |
27 |
| -// json.push(','); |
28 |
| -// } |
29 |
| -// } |
30 |
| - |
31 |
| -// json.push_str("]}"); |
32 |
| -// json |
33 |
| -// } |
| 13 | +fn generate_json(child_json: &str, size: usize) -> String { |
| 14 | + let mut json = String::with_capacity(child_json.len() * size); |
| 15 | + json.push('{'); |
| 16 | + for i in 0..size { |
| 17 | + json.push_str("\"obj"); |
| 18 | + json.push_str(&i.to_string()); |
| 19 | + json.push_str("\":"); |
| 20 | + json.push_str(child_json); |
| 21 | + json.push(','); |
| 22 | + } |
| 23 | + json.push_str("\"array\":["); |
| 24 | + for i in 0..size { |
| 25 | + json.push_str(child_json); |
| 26 | + if i < size - 1 { |
| 27 | + json.push(','); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + json.push_str("]}"); |
| 32 | + json |
| 33 | +} |
34 | 34 |
|
35 | 35 | pub fn criterion_benchmark(c: &mut Criterion) {
|
36 |
| - // let mut group = c.benchmark_group("Parsing"); |
37 |
| - |
38 |
| - // let json = JSON.to_owned(); |
39 |
| - // for (id, json) in [ |
40 |
| - // json.clone(), |
41 |
| - // generate_json(&json, 1), |
42 |
| - // generate_json(&json, 10), |
43 |
| - // generate_json(&json, 100), |
44 |
| - // ] |
45 |
| - // .into_iter() |
46 |
| - // .enumerate() |
47 |
| - // { |
48 |
| - // let runtime = Runtime::new().unwrap(); |
49 |
| - // runtime.set_max_stack_size(512 * 1024); |
50 |
| - |
51 |
| - // let ctx = Context::full(&runtime).unwrap(); |
52 |
| - // group.bench_with_input(BenchmarkId::new("Custom", id), &json, |b, json| { |
53 |
| - // ctx.with(|ctx| { |
54 |
| - // b.iter(|| json_parse(&ctx, json.clone())); |
55 |
| - // }); |
56 |
| - // }); |
57 |
| - // group.bench_with_input(BenchmarkId::new("Built-in", id), &json, |b, json| { |
58 |
| - // ctx.with(|ctx| { |
59 |
| - // b.iter(|| ctx.json_parse(json.clone())); |
60 |
| - // }); |
61 |
| - // }); |
62 |
| - // } |
63 |
| - |
64 |
| - // group.finish(); |
| 36 | + let mut group = c.benchmark_group("Parsing"); |
| 37 | + |
| 38 | + let json = JSON.to_owned(); |
| 39 | + for (id, json) in [ |
| 40 | + json.clone(), |
| 41 | + generate_json(&json, 1), |
| 42 | + generate_json(&json, 10), |
| 43 | + generate_json(&json, 100), |
| 44 | + generate_json(&json, 1000), |
| 45 | + generate_json(&json, 10000), |
| 46 | + ] |
| 47 | + .into_iter() |
| 48 | + .enumerate() |
| 49 | + { |
| 50 | + let runtime = Runtime::new().unwrap(); |
| 51 | + runtime.set_max_stack_size(512 * 1024); |
| 52 | + |
| 53 | + let ctx = Context::full(&runtime).unwrap(); |
| 54 | + group.bench_with_input(BenchmarkId::new("Custom", id), &json, |b, json| { |
| 55 | + ctx.with(|ctx| { |
| 56 | + b.iter(|| json_parse(&ctx, json.clone())); |
| 57 | + }); |
| 58 | + }); |
| 59 | + group.bench_with_input(BenchmarkId::new("Built-in", id), &json, |b, json| { |
| 60 | + ctx.with(|ctx| { |
| 61 | + b.iter(|| ctx.json_parse(json.clone())); |
| 62 | + }); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + group.finish(); |
65 | 67 |
|
66 | 68 | c.bench_function("json parse", |b| {
|
67 | 69 | let runtime = Runtime::new().unwrap();
|
|
0 commit comments