|
| 1 | +import os |
| 2 | +import json |
| 3 | +import x.json2 |
| 4 | +import msgpack |
| 5 | +import time |
| 6 | +import benchmark |
| 7 | + |
| 8 | +struct Person { |
| 9 | + name string |
| 10 | + age int |
| 11 | + created_at time.Time |
| 12 | +} |
| 13 | + |
| 14 | +fn main() { |
| 15 | + max_iterations := os.getenv_opt('MAX_ITERATIONS') or { '1000' }.int() |
| 16 | + s := '{"name":"Bilbo Baggins","age":99,"created_at":1670840340}' |
| 17 | + mut b := benchmark.start() |
| 18 | + |
| 19 | + for _ in 0 .. max_iterations { |
| 20 | + p := json2.decode[Person](s)! |
| 21 | + if p.age != 99 { |
| 22 | + println('error: ${p}') |
| 23 | + } |
| 24 | + } |
| 25 | + b.measure('json2.decode') |
| 26 | + |
| 27 | + for _ in 0 .. max_iterations { |
| 28 | + p := json.decode(Person, s)! |
| 29 | + if p.age != 99 { |
| 30 | + println('error: ${p}') |
| 31 | + } |
| 32 | + } |
| 33 | + b.measure('json.decode') |
| 34 | + |
| 35 | + // encoding measurements: |
| 36 | + p := json.decode(Person, s)! |
| 37 | + |
| 38 | + for _ in 0 .. max_iterations { |
| 39 | + es := json2.encode(p) |
| 40 | + if es[0] != `{` { |
| 41 | + println('json2.encode error: ${es}') |
| 42 | + } |
| 43 | + } |
| 44 | + b.measure('json2.encode') |
| 45 | + |
| 46 | + for _ in 0 .. max_iterations { |
| 47 | + es := json.encode(p) |
| 48 | + if es[0] != `{` { |
| 49 | + println('json.encode error: ${es}') |
| 50 | + } |
| 51 | + } |
| 52 | + b.measure('json.encode') |
| 53 | + |
| 54 | + for _ in 0 .. max_iterations { |
| 55 | + es := msgpack.encode[Person](p) |
| 56 | + if p.age != 99 { |
| 57 | + println('error: ${es}') |
| 58 | + } |
| 59 | + } |
| 60 | + b.measure('msgpack.encode') |
| 61 | + |
| 62 | + encoded := msgpack.encode[Person](p) |
| 63 | + dump(encoded#[0..10]) |
| 64 | + mut decoder := msgpack.new_decoder() |
| 65 | + for _ in 0 .. max_iterations { |
| 66 | + // TODO: investigate why decoder.decode does not return a result at all :-| |
| 67 | + decoder.decode(encoded)! |
| 68 | + } |
| 69 | + b.measure('msgpack.decode') |
| 70 | +} |
0 commit comments