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') +}