github.com/hamba/avro/v2@v2.22.1-0.20240518180522-aff3955acf7d/bench_test.go (about) 1 package avro_test 2 3 import ( 4 "io" 5 "os" 6 "testing" 7 8 "github.com/hamba/avro/v2" 9 ) 10 11 type Superhero struct { 12 ID int `avro:"id"` 13 AffiliationID int `avro:"affiliation_id"` 14 Name string `avro:"name"` 15 Life float32 `avro:"life"` 16 Energy float32 `avro:"energy"` 17 Powers []*Superpower `avro:"powers"` 18 } 19 20 type Superpower struct { 21 ID int `avro:"id"` 22 Name string `avro:"name"` 23 Damage float32 `avro:"damage"` 24 Energy float32 `avro:"energy"` 25 Passive bool `avro:"passive"` 26 } 27 28 type PartialSuperhero struct { 29 ID int `avro:"id"` 30 AffiliationID int `avro:"affiliation_id"` 31 Name string `avro:"name"` 32 Life float32 `avro:"life"` 33 Energy float32 `avro:"energy"` 34 } 35 36 func BenchmarkSuperheroDecode(b *testing.B) { 37 data, err := os.ReadFile("testdata/superhero.bin") 38 if err != nil { 39 panic(err) 40 } 41 42 schema, err := avro.ParseFiles("testdata/superhero.avsc") 43 if err != nil { 44 panic(err) 45 } 46 47 super := &Superhero{} 48 49 b.ReportAllocs() 50 b.ResetTimer() 51 for i := 0; i < b.N; i++ { 52 _ = avro.Unmarshal(schema, data, super) 53 } 54 } 55 56 func BenchmarkSuperheroEncode(b *testing.B) { 57 schema, err := avro.ParseFiles("testdata/superhero.avsc") 58 if err != nil { 59 panic(err) 60 } 61 62 super := &Superhero{ 63 ID: 234765, 64 AffiliationID: 9867, 65 Name: "Wolverine", 66 Life: 85.25, 67 Energy: 32.75, 68 Powers: []*Superpower{ 69 {ID: 2345, Name: "Bone Claws", Damage: 5, Energy: 1.15, Passive: false}, 70 {ID: 2346, Name: "Regeneration", Damage: -2, Energy: 0.55, Passive: true}, 71 {ID: 2347, Name: "Adamant skeleton", Damage: -10, Energy: 0, Passive: true}, 72 }, 73 } 74 75 b.ReportAllocs() 76 b.ResetTimer() 77 for i := 0; i < b.N; i++ { 78 _, _ = avro.Marshal(schema, super) 79 } 80 } 81 82 func BenchmarkPartialSuperheroDecode(b *testing.B) { 83 data, err := os.ReadFile("testdata/superhero.bin") 84 if err != nil { 85 panic(err) 86 } 87 88 schema, err := avro.ParseFiles("testdata/superhero.avsc") 89 if err != nil { 90 panic(err) 91 } 92 93 super := &PartialSuperhero{} 94 95 b.ReportAllocs() 96 b.ResetTimer() 97 for i := 0; i < b.N; i++ { 98 _ = avro.Unmarshal(schema, data, super) 99 } 100 } 101 102 func BenchmarkSuperheroGenericDecode(b *testing.B) { 103 data, err := os.ReadFile("testdata/superhero.bin") 104 if err != nil { 105 panic(err) 106 } 107 108 schema, err := avro.ParseFiles("testdata/superhero.avsc") 109 if err != nil { 110 panic(err) 111 } 112 113 b.ReportAllocs() 114 b.ResetTimer() 115 for i := 0; i < b.N; i++ { 116 var m any 117 _ = avro.Unmarshal(schema, data, &m) 118 } 119 } 120 121 func BenchmarkSuperheroGenericEncode(b *testing.B) { 122 schema, err := avro.ParseFiles("testdata/superhero.avsc") 123 if err != nil { 124 panic(err) 125 } 126 127 super := map[string]any{ 128 "id": 234765, 129 "affiliation_id": 9867, 130 "Name": "Wolverine", 131 "life": 85.25, 132 "energy": 32.75, 133 "powers": []map[string]any{ 134 {"id": 2345, "name": "Bone Claws", "damage": 5, "energy": 1.15, "passive": false}, 135 {"id": 2346, "name": "Regeneration", "damage": -2, "energy": 0.55, "passive": true}, 136 {"id": 2347, "name": "Adamant skeleton", "damage": -10, "energy": 0, "passive": true}, 137 }, 138 } 139 140 b.ReportAllocs() 141 b.ResetTimer() 142 for i := 0; i < b.N; i++ { 143 _, _ = avro.Marshal(schema, super) 144 } 145 } 146 147 func BenchmarkSuperheroWriteFlush(b *testing.B) { 148 schema, err := avro.ParseFiles("testdata/superhero.avsc") 149 if err != nil { 150 panic(err) 151 } 152 153 super := &Superhero{ 154 ID: 234765, 155 AffiliationID: 9867, 156 Name: "Wolverine", 157 Life: 85.25, 158 Energy: 32.75, 159 Powers: []*Superpower{ 160 {ID: 2345, Name: "Bone Claws", Damage: 5, Energy: 1.15, Passive: false}, 161 {ID: 2346, Name: "Regeneration", Damage: -2, Energy: 0.55, Passive: true}, 162 {ID: 2347, Name: "Adamant skeleton", Damage: -10, Energy: 0, Passive: true}, 163 }, 164 } 165 166 b.ReportAllocs() 167 b.ResetTimer() 168 169 w := avro.NewWriter(io.Discard, 128) 170 for i := 0; i < b.N; i++ { 171 w.WriteVal(schema, super) 172 _ = w.Flush() 173 } 174 }