json2go/json2go_bench_test.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com refactor: use an actual parser instead of reflection..., 11 days ago
olexsmir@gmail.com refactor: use an actual parser instead of reflection..., 11 days ago
| 1 | package json2go |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | func BenchmarkTransform(b *testing.B) { |
| 8 | benches := map[string]string{ |
| 9 | "simple_object": `{"name":"alice","age":30,"active":true}`, |
| 10 | "flat_object": `{"id":1,"first_name":"john","last_name":"doe","email":"john@example.com","phone":"+1234567890","created_at":"2024-01-01T00:00:00Z","updated_at":"2024-01-02T00:00:00Z"}`, |
| 11 | "nested_object": `{"user":{"id":1,"name":"alice","profile":{"bio":"engineer","location":"sf","skills":["go","rust","python"]},"settings":{"theme":"dark","notifications":true}}}`, |
| 12 | "array_of_objects": `[{"id":1,"name":"alice"},{"id":2,"name":"bob"},{"id":3,"name":"charlie"}]`, |
| 13 | "mixed_types": `{"string":"text","number":42,"decimal":3.14,"bool":true,"null_val":null,"array":[1,2,3],"object":{"nested":true}}`, |
| 14 | "deeply_nested": `{"a":{"b":{"c":{"d":{"e":{"f":{"g":{"h":{"i":{"j":{"value":"deep"}}}}}}}}}}}`, |
| 15 | "large_array": `[{"id":1,"val":"a"},{"id":2,"val":"b"},{"id":3,"val":"c"},{"id":4,"val":"d"},{"id":5,"val":"e"},{"id":6,"val":"f"},{"id":7,"val":"g"},{"id":8,"val":"h"},{"id":9,"val":"i"},{"id":10,"val":"j"}]`, |
| 16 | "unicode_strings": `{"english":"hello","chinese":"你好","arabic":"مرحبا","emoji":"🚀🔥✨"}`, |
| 17 | } |
| 18 | for bname, bjson := range benches { |
| 19 | b.Run(bname+"_with_tags", func(b *testing.B) { |
| 20 | b.ReportAllocs() |
| 21 | for i := 0; i < b.N; i++ { |
| 22 | Transform("Test", bjson, true) |
| 23 | } |
| 24 | }) |
| 25 | |
| 26 | b.Run(bname+"_no_tags", func(b *testing.B) { |
| 27 | b.ReportAllocs() |
| 28 | for i := 0; i < b.N; i++ { |
| 29 | Transform("Test", bjson, false) |
| 30 | } |
| 31 | }) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func BenchmarkPipeline(b *testing.B) { |
| 36 | json := `{"user":{"id":1,"name":"alice","email":"alice@example.com","profile":{"bio":"engineer","location":"sf"}}}` |
| 37 | |
| 38 | b.Run("lexer_only", func(b *testing.B) { |
| 39 | b.ReportAllocs() |
| 40 | for i := 0; i < b.N; i++ { |
| 41 | lexer := NewLexer([]byte(json)) |
| 42 | for { |
| 43 | tok := lexer.Next() |
| 44 | if tok.Type == EOF { |
| 45 | break |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | }) |
| 50 | |
| 51 | b.Run("parser_only", func(b *testing.B) { |
| 52 | b.ReportAllocs() |
| 53 | for i := 0; i < b.N; i++ { |
| 54 | lexer := NewLexer([]byte(json)) |
| 55 | parser := NewParser(lexer) |
| 56 | parser.Parse() |
| 57 | } |
| 58 | }) |
| 59 | |
| 60 | b.Run("transpiler_only", func(b *testing.B) { |
| 61 | lexer := NewLexer([]byte(json)) |
| 62 | parser := NewParser(lexer) |
| 63 | v, _ := parser.Parse() |
| 64 | |
| 65 | b.ReportAllocs() |
| 66 | b.ResetTimer() |
| 67 | for i := 0; i < b.N; i++ { |
| 68 | NewTranspiler().Transpile("Test", v, true) |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | b.Run("full_pipeline", func(b *testing.B) { |
| 73 | b.ReportAllocs() |
| 74 | for i := 0; i < b.N; i++ { |
| 75 | Transform("Test", json, true) |
| 76 | } |
| 77 | }) |
| 78 | } |