github.com/aacfactory/avro@v1.2.12/api_test.go (about) 1 package avro_test 2 3 import ( 4 "encoding/json" 5 "github.com/aacfactory/avro" 6 "github.com/aacfactory/avro/internal/base" 7 "math/big" 8 "testing" 9 "time" 10 ) 11 12 type Any struct { 13 p []byte 14 } 15 16 func (a *Any) UnmarshalAvro(p []byte) error { 17 a.p = p 18 return nil 19 } 20 21 func (a Any) MarshalAvro() ([]byte, error) { 22 return a.p, nil 23 } 24 25 type Anonymous struct { 26 Anonymous string `avro:"anonymous"` 27 } 28 29 type Bar struct { 30 String string `avro:"string"` 31 Next *Bar `avro:"next"` 32 } 33 34 type Foo struct { 35 Anonymous 36 String string `avro:"string"` 37 Boolean bool `avro:"boolean"` 38 Int int `avro:"int"` 39 Long int64 `avro:"long"` 40 Float float32 `avro:"float"` 41 Double float64 `avro:"double"` 42 Uint uint64 `avro:"uint"` 43 Time time.Time `avro:"time"` 44 Dur time.Duration `avro:"dur"` 45 Byte byte `avro:"byte"` 46 Bytes []byte `avro:"bytes"` 47 BigInt *big.Int `avro:"bigInt"` 48 Bar Bar `avro:"bar"` 49 Baz *Bar `avro:"baz"` 50 Bars []Bar `avro:"bars"` 51 Map map[string]Bar `avro:"map"` 52 Any Any `avro:"any"` 53 } 54 55 func TestMarshal(t *testing.T) { 56 s, parseErr := base.ParseValue(Foo{}) 57 if parseErr != nil { 58 t.Error(parseErr) 59 return 60 } 61 t.Log(s) 62 foo := Foo{ 63 Anonymous: Anonymous{ 64 Anonymous: "Anonymous", 65 }, 66 String: "foo", 67 Boolean: true, 68 Int: 1, 69 Long: 2, 70 Float: 3.3, 71 Double: 4.4, 72 Uint: uint64(5), 73 Time: time.Now(), 74 Dur: 10 * time.Hour, 75 BigInt: big.NewInt(12), 76 Byte: 'B', 77 Bytes: []byte("bytes"), 78 Bar: Bar{ 79 String: "bar", 80 Next: &Bar{ 81 String: "Bar-Next", 82 Next: nil, 83 }, 84 }, 85 Baz: nil, 86 Bars: []Bar{{String: "bar-1"}}, 87 Map: map[string]Bar{"bar2": {String: "bar-2"}}, 88 Any: Any{ 89 p: []byte("any"), 90 }, 91 } 92 93 p, encodeErr := base.Marshal(s, foo) 94 if encodeErr != nil { 95 t.Error(encodeErr) 96 return 97 } 98 t.Log(len(p)) 99 r := Foo{} 100 decodeErr := base.Unmarshal(s, p, &r) 101 if decodeErr != nil { 102 t.Error(decodeErr) 103 return 104 } 105 t.Logf("%+v", r) 106 } 107 108 func BenchmarkAvro(b *testing.B) { 109 // BenchmarkAvro-20 1000000 1054 ns/op 626 B/op 9 allocs/op 110 b.ReportAllocs() 111 foo := Foo{ 112 String: "foo", 113 Boolean: true, 114 Int: 1, 115 Long: 2, 116 Float: 3.3, 117 Double: 4.4, 118 Uint: uint64(5), 119 Time: time.Now(), 120 Dur: 10 * time.Hour, 121 Byte: 'B', 122 Bytes: []byte("bytes"), 123 Bar: Bar{ 124 String: "bar", 125 Next: &Bar{ 126 String: "Bar-Next", 127 Next: nil, 128 }, 129 }, 130 Baz: nil, 131 Bars: []Bar{{String: "bar-1"}}, 132 Map: map[string]Bar{"bar2": {String: "bar-2"}}, 133 } 134 for i := 0; i < b.N; i++ { 135 p, _ := avro.Marshal(foo) 136 _ = avro.Unmarshal(p, &foo) 137 } 138 } 139 140 func BenchmarkJson(b *testing.B) { 141 // fastjson BenchmarkJson-20 591304 1953 ns/op 889 B/op 19 allocs/op 142 // json BenchmarkJson-20 260964 4495 ns/op 1218 B/op 29 allocs/op 143 b.ReportAllocs() 144 foo := Foo{ 145 String: "foo", 146 Boolean: true, 147 Int: 1, 148 Long: 2, 149 Float: 3.3, 150 Double: 4.4, 151 Uint: uint64(5), 152 Time: time.Now(), 153 Dur: 10 * time.Hour, 154 Byte: 'B', 155 Bytes: []byte("bytes"), 156 Bar: Bar{ 157 String: "bar", 158 Next: &Bar{ 159 String: "Bar-Next", 160 Next: nil, 161 }, 162 }, 163 Baz: nil, 164 Bars: []Bar{{String: "bar-1"}}, 165 Map: map[string]Bar{"bar2": {String: "bar-2"}}, 166 } 167 for i := 0; i < b.N; i++ { 168 p, _ := json.Marshal(foo) 169 _ = json.Unmarshal(p, &foo) 170 } 171 } 172 173 type Big struct { 174 Int *big.Int `avro:"Int"` 175 Float *big.Float `avro:"float"` 176 Rat *big.Rat `avro:"rat"` 177 } 178 179 func TestBigInt(t *testing.T) { 180 v := Big{ 181 Int: big.NewInt(1), 182 Float: big.NewFloat(2), 183 Rat: big.NewRat(1, 3), 184 } 185 p, err := avro.Marshal(v) 186 if err != nil { 187 t.Error(err) 188 return 189 } 190 t.Log(string(p)) 191 v = Big{} 192 err = avro.Unmarshal(p, &v) 193 if err != nil { 194 t.Error(err) 195 return 196 } 197 t.Log(v) 198 } 199 200 func TestMap(t *testing.T) { 201 v := map[string]string{"a": "a", "b": "b"} 202 p, err := avro.Marshal(v) 203 if err != nil { 204 t.Error(err) 205 return 206 } 207 t.Log(string(p)) 208 v = make(map[string]string) 209 err = avro.Unmarshal(p, &v) 210 if err != nil { 211 t.Error(err) 212 return 213 } 214 t.Log(v) 215 }