package asld_test import ( "encoding/json" "fmt" "math" "testing" "time" "git.sectorinf.com/emilis/asflab/pkg/asld" "git.sectorinf.com/emilis/asflab/pkg/epk" "github.com/stretchr/testify/require" ) type testObj struct { String string `asld:"string" json:"string"` NoTag string Int int Int8 int8 Int16 int16 Int32 int32 Int64 int64 Uint uint Uint8 uint8 Uint16 uint16 Uint32 uint32 Uint64 uint64 Float32 float32 Float64 float64 // Complex64 complex64 // Complex128 complex128 IntPtr *int Int8Ptr *int8 Int16Ptr *int16 Int32Ptr *int32 Int64Ptr *int64 UintPtr *uint Uint8Ptr *uint8 Uint16Ptr *uint16 Uint32Ptr *uint32 Uint64Ptr *uint64 Float32Ptr *float32 Float64Ptr *float64 // Complex64Ptr *complex64 // Complex128Ptr *complex128 TestPtr *testObj TestArray []testObj } func TestUnmarshal(t *testing.T) { tests := map[string]struct { obj testObj errCheck epk.ErrorTest }{ "string children": { obj: testObj{ String: "hello", NoTag: "no_tag", Int: math.MaxInt, Int8: math.MaxInt8, Int16: math.MaxInt16, Int32: math.MaxInt32, Int64: math.MaxInt64, Uint: math.MaxUint, Uint8: math.MaxUint8, Uint16: math.MaxUint16, Uint32: math.MaxUint32, Uint64: math.MaxUint64, Float32: math.MaxFloat32, Float64: math.MaxFloat64, TestPtr: &testObj{ String: "hello2", }, TestArray: []testObj{ { String: "hello3", }, { String: "hello4", TestPtr: &testObj{ TestArray: []testObj{ { String: "hello5", }, }, }, }, }, // Complex64: complex(math.MaxFloat32, math.MaxFloat32), // Complex128: complex(math.MaxFloat64, math.MaxFloat64), }, }, } for name, test := range tests { test := test t.Run(name, func(tt *testing.T) { that := require.New(tt) objJSON, err := json.MarshalIndent(test.obj, "", " ") that.NoError(err) result, err := asld.Unmarshal[testObj](objJSON) that.NoError(err) that.Equal(test.obj, result) }) } } func TestBench(t *testing.T) { obj := testObj{ String: "hello", NoTag: "no_tag", Int: math.MaxInt, Int8: math.MaxInt8, Int16: math.MaxInt16, Int32: math.MaxInt32, Int64: math.MaxInt64, Uint: math.MaxUint, Uint8: math.MaxUint8, Uint16: math.MaxUint16, Uint32: math.MaxUint32, Uint64: math.MaxUint64, Float32: math.MaxFloat32, Float64: math.MaxFloat64, // Complex64: complex(math.MaxFloat32, math.MaxFloat32), // Complex128: complex(math.MaxFloat64, math.MaxFloat64), } that := require.New(t) asldTotal := int64(0) jsonTotal := int64(0) jsonMax := int64(0) jsonMin := math.MaxInt64 asldMax := int64(0) asldMin := math.MaxInt64 count := int64(1 << 20) for index := int64(0); index < count; index++ { objJSON, err := json.Marshal(obj) that.NoError(err) asldStart := time.Now() _, err = asld.Unmarshal[testObj](objJSON) asldDur := time.Since(asldStart) asldTotal += int64(asldDur) if asldDur < time.Duration(asldMin) { asldMin = int(asldDur) } if asldDur > time.Duration(asldMax) { asldMax = int64(asldDur) } that.NoError(err) a := testObj{} jsonStart := time.Now() err = json.Unmarshal(objJSON, &a) jsonDur := time.Since(jsonStart) jsonTotal += int64(jsonDur) if jsonDur < time.Duration(jsonMin) { jsonMin = int(jsonDur) } if jsonDur > time.Duration(jsonMax) { jsonMax = int64(jsonDur) } that.NoError(err) } fmt.Println(count, "runs") fmt.Printf("json avg (%s), min (%s), max (%s)\n", time.Duration(jsonTotal/count), time.Duration(jsonMin), time.Duration(jsonMax)) fmt.Printf("asld avg (%s), min (%s), max (%s)\n", time.Duration(asldTotal/count), time.Duration(asldMin), time.Duration(asldMax)) }