From 992621bd9119c5d89858ca60c78eeb631cfc4b33 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 12 Dec 2022 10:26:04 +0200 Subject: [PATCH] bench: add a small performance comparison script bench_json_vs_json2.v --- vlib/v/tests/bench/bench_json_vs_json2.v | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 vlib/v/tests/bench/bench_json_vs_json2.v diff --git a/vlib/v/tests/bench/bench_json_vs_json2.v b/vlib/v/tests/bench/bench_json_vs_json2.v new file mode 100644 index 0000000000..d562c8923d --- /dev/null +++ b/vlib/v/tests/bench/bench_json_vs_json2.v @@ -0,0 +1,54 @@ +import os +import json +import x.json2 +import time +import benchmark + +// assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val == fixed_time + +struct Person { + name string + age int + created_at time.Time +} + +fn main() { + max_iterations := os.getenv_opt('MAX_ITERATIONS') or { '1000' }.int() + s := '{"name":"Bilbo Baggins","age":99,"created_at":1670840340}' + mut b := benchmark.start() + + for _ in 0 .. max_iterations { + p := json2.decode[Person](s)! + if p.age != 99 { + println('error: ${p}') + } + } + b.measure('json2.decode') + + for _ in 0 .. max_iterations { + p := json.decode(Person, s)! + if p.age != 99 { + println('error: ${p}') + } + } + b.measure('json.decode') + + // encoding measurements: + p := json.decode(Person, s)! + + for _ in 0 .. max_iterations { + es := json2.encode(p) + if es[0] != `{` { + println('json2.encode error: ${es}') + } + } + b.measure('json2.encode') + + for _ in 0 .. max_iterations { + es := json.encode(p) + if es[0] != `{` { + println('json.encode error: ${es}') + } + } + b.measure('json.encode') +}