diff --git a/output_tests/builtins/bool/json_test.go b/output_tests/builtins/bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/bool/json_test.go +++ b/output_tests/builtins/bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/bool_alias/json_test.go b/output_tests/builtins/bool_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/bool_alias/json_test.go +++ b/output_tests/builtins/bool_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/byte/json_test.go b/output_tests/builtins/byte/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/byte/json_test.go +++ b/output_tests/builtins/byte/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/byte_alias/json_test.go b/output_tests/builtins/byte_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/byte_alias/json_test.go +++ b/output_tests/builtins/byte_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/float32/json_test.go b/output_tests/builtins/float32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/float32/json_test.go +++ b/output_tests/builtins/float32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/float32_alias/json_test.go b/output_tests/builtins/float32_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/float32_alias/json_test.go +++ b/output_tests/builtins/float32_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/float64/json_test.go b/output_tests/builtins/float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/float64/json_test.go +++ b/output_tests/builtins/float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/float64_alias/json_test.go b/output_tests/builtins/float64_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/float64_alias/json_test.go +++ b/output_tests/builtins/float64_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/int16/json_test.go b/output_tests/builtins/int16/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/int16/json_test.go +++ b/output_tests/builtins/int16/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/int16_alias/json_test.go b/output_tests/builtins/int16_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/int16_alias/json_test.go +++ b/output_tests/builtins/int16_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/int32/json_test.go b/output_tests/builtins/int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/int32/json_test.go +++ b/output_tests/builtins/int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/int32_alias/json_test.go b/output_tests/builtins/int32_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/int32_alias/json_test.go +++ b/output_tests/builtins/int32_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/int8/json_test.go b/output_tests/builtins/int8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/int8/json_test.go +++ b/output_tests/builtins/int8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/int8_alias/json_test.go b/output_tests/builtins/int8_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/int8_alias/json_test.go +++ b/output_tests/builtins/int8_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/string/json_test.go b/output_tests/builtins/string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/string/json_test.go +++ b/output_tests/builtins/string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/string_alias/json_test.go b/output_tests/builtins/string_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/string_alias/json_test.go +++ b/output_tests/builtins/string_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/uint16/json_test.go b/output_tests/builtins/uint16/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/uint16/json_test.go +++ b/output_tests/builtins/uint16/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/uint16_alias/json_test.go b/output_tests/builtins/uint16_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/uint16_alias/json_test.go +++ b/output_tests/builtins/uint16_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/uint32/json_test.go b/output_tests/builtins/uint32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/uint32/json_test.go +++ b/output_tests/builtins/uint32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/uint32_alias/json_test.go b/output_tests/builtins/uint32_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/uint32_alias/json_test.go +++ b/output_tests/builtins/uint32_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/uint8/json_test.go b/output_tests/builtins/uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/uint8/json_test.go +++ b/output_tests/builtins/uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/builtins/uint8_alias/json_test.go b/output_tests/builtins/uint8_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/builtins/uint8_alias/json_test.go +++ b/output_tests/builtins/uint8_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/bool/json_test.go b/output_tests/maps/builtins/int32/bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/bool/json_test.go +++ b/output_tests/maps/builtins/int32/bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/byte/json_test.go b/output_tests/maps/builtins/int32/byte/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/byte/json_test.go +++ b/output_tests/maps/builtins/int32/byte/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/float32/json_test.go b/output_tests/maps/builtins/int32/float32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/float32/json_test.go +++ b/output_tests/maps/builtins/int32/float32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/float64/json_test.go b/output_tests/maps/builtins/int32/float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/float64/json_test.go +++ b/output_tests/maps/builtins/int32/float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/int32/json_test.go b/output_tests/maps/builtins/int32/int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/int32/json_test.go +++ b/output_tests/maps/builtins/int32/int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/int8/json_test.go b/output_tests/maps/builtins/int32/int8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/int8/json_test.go +++ b/output_tests/maps/builtins/int32/int8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/string/json_test.go b/output_tests/maps/builtins/int32/string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/string/json_test.go +++ b/output_tests/maps/builtins/int32/string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/string_alias/json_test.go b/output_tests/maps/builtins/int32/string_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/string_alias/json_test.go +++ b/output_tests/maps/builtins/int32/string_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/int32/uint8/json_test.go b/output_tests/maps/builtins/int32/uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/int32/uint8/json_test.go +++ b/output_tests/maps/builtins/int32/uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/bool/json_test.go b/output_tests/maps/builtins/string/bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/bool/json_test.go +++ b/output_tests/maps/builtins/string/bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/byte/json_test.go b/output_tests/maps/builtins/string/byte/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/byte/json_test.go +++ b/output_tests/maps/builtins/string/byte/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/float32/json_test.go b/output_tests/maps/builtins/string/float32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/float32/json_test.go +++ b/output_tests/maps/builtins/string/float32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/float64/json_test.go b/output_tests/maps/builtins/string/float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/float64/json_test.go +++ b/output_tests/maps/builtins/string/float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/int32/json_test.go b/output_tests/maps/builtins/string/int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/int32/json_test.go +++ b/output_tests/maps/builtins/string/int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/int8/json_test.go b/output_tests/maps/builtins/string/int8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/int8/json_test.go +++ b/output_tests/maps/builtins/string/int8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/string/json_test.go b/output_tests/maps/builtins/string/string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/string/json_test.go +++ b/output_tests/maps/builtins/string/string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/string_alias/json_test.go b/output_tests/maps/builtins/string/string_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/string_alias/json_test.go +++ b/output_tests/maps/builtins/string/string_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string/uint8/json_test.go b/output_tests/maps/builtins/string/uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string/uint8/json_test.go +++ b/output_tests/maps/builtins/string/uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/bool/json_test.go b/output_tests/maps/builtins/string_alias/bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/bool/json_test.go +++ b/output_tests/maps/builtins/string_alias/bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/byte/json_test.go b/output_tests/maps/builtins/string_alias/byte/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/byte/json_test.go +++ b/output_tests/maps/builtins/string_alias/byte/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/float32/json_test.go b/output_tests/maps/builtins/string_alias/float32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/float32/json_test.go +++ b/output_tests/maps/builtins/string_alias/float32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/float64/json_test.go b/output_tests/maps/builtins/string_alias/float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/float64/json_test.go +++ b/output_tests/maps/builtins/string_alias/float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/int32/json_test.go b/output_tests/maps/builtins/string_alias/int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/int32/json_test.go +++ b/output_tests/maps/builtins/string_alias/int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/int8/json_test.go b/output_tests/maps/builtins/string_alias/int8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/int8/json_test.go +++ b/output_tests/maps/builtins/string_alias/int8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/string/json_test.go b/output_tests/maps/builtins/string_alias/string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/string/json_test.go +++ b/output_tests/maps/builtins/string_alias/string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/string_alias/json_test.go b/output_tests/maps/builtins/string_alias/string_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/string_alias/json_test.go +++ b/output_tests/maps/builtins/string_alias/string_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/string_alias/uint8/json_test.go b/output_tests/maps/builtins/string_alias/uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/string_alias/uint8/json_test.go +++ b/output_tests/maps/builtins/string_alias/uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/bool/json_test.go b/output_tests/maps/builtins/uint8/bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/bool/json_test.go +++ b/output_tests/maps/builtins/uint8/bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/byte/json_test.go b/output_tests/maps/builtins/uint8/byte/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/byte/json_test.go +++ b/output_tests/maps/builtins/uint8/byte/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/float32/json_test.go b/output_tests/maps/builtins/uint8/float32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/float32/json_test.go +++ b/output_tests/maps/builtins/uint8/float32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/float64/json_test.go b/output_tests/maps/builtins/uint8/float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/float64/json_test.go +++ b/output_tests/maps/builtins/uint8/float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/int32/json_test.go b/output_tests/maps/builtins/uint8/int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/int32/json_test.go +++ b/output_tests/maps/builtins/uint8/int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/int8/json_test.go b/output_tests/maps/builtins/uint8/int8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/int8/json_test.go +++ b/output_tests/maps/builtins/uint8/int8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/string/json_test.go b/output_tests/maps/builtins/uint8/string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/string/json_test.go +++ b/output_tests/maps/builtins/uint8/string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/string_alias/json_test.go b/output_tests/maps/builtins/uint8/string_alias/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/string_alias/json_test.go +++ b/output_tests/maps/builtins/uint8/string_alias/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/builtins/uint8/uint8/json_test.go b/output_tests/maps/builtins/uint8/uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/builtins/uint8/uint8/json_test.go +++ b/output_tests/maps/builtins/uint8/uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/int16/ptr_bool/json_test.go b/output_tests/maps/pointers/int16/ptr_bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/int16/ptr_bool/json_test.go +++ b/output_tests/maps/pointers/int16/ptr_bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/int16/ptr_float64/json_test.go b/output_tests/maps/pointers/int16/ptr_float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/int16/ptr_float64/json_test.go +++ b/output_tests/maps/pointers/int16/ptr_float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/int16/ptr_int32/json_test.go b/output_tests/maps/pointers/int16/ptr_int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/int16/ptr_int32/json_test.go +++ b/output_tests/maps/pointers/int16/ptr_int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/int16/ptr_string/json_test.go b/output_tests/maps/pointers/int16/ptr_string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/int16/ptr_string/json_test.go +++ b/output_tests/maps/pointers/int16/ptr_string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/int16/ptr_uint8/json_test.go b/output_tests/maps/pointers/int16/ptr_uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/int16/ptr_uint8/json_test.go +++ b/output_tests/maps/pointers/int16/ptr_uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/string/ptr_bool/json_test.go b/output_tests/maps/pointers/string/ptr_bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/string/ptr_bool/json_test.go +++ b/output_tests/maps/pointers/string/ptr_bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/string/ptr_float64/json_test.go b/output_tests/maps/pointers/string/ptr_float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/string/ptr_float64/json_test.go +++ b/output_tests/maps/pointers/string/ptr_float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/string/ptr_int32/json_test.go b/output_tests/maps/pointers/string/ptr_int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/string/ptr_int32/json_test.go +++ b/output_tests/maps/pointers/string/ptr_int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/string/ptr_string/json_test.go b/output_tests/maps/pointers/string/ptr_string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/string/ptr_string/json_test.go +++ b/output_tests/maps/pointers/string/ptr_string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/string/ptr_uint8/json_test.go b/output_tests/maps/pointers/string/ptr_uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/string/ptr_uint8/json_test.go +++ b/output_tests/maps/pointers/string/ptr_uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/uint16/ptr_bool/json_test.go b/output_tests/maps/pointers/uint16/ptr_bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/uint16/ptr_bool/json_test.go +++ b/output_tests/maps/pointers/uint16/ptr_bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/uint16/ptr_float64/json_test.go b/output_tests/maps/pointers/uint16/ptr_float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/uint16/ptr_float64/json_test.go +++ b/output_tests/maps/pointers/uint16/ptr_float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/uint16/ptr_int32/json_test.go b/output_tests/maps/pointers/uint16/ptr_int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/uint16/ptr_int32/json_test.go +++ b/output_tests/maps/pointers/uint16/ptr_int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/uint16/ptr_string/json_test.go b/output_tests/maps/pointers/uint16/ptr_string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/uint16/ptr_string/json_test.go +++ b/output_tests/maps/pointers/uint16/ptr_string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/maps/pointers/uint16/ptr_uint8/json_test.go b/output_tests/maps/pointers/uint16/ptr_uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/maps/pointers/uint16/ptr_uint8/json_test.go +++ b/output_tests/maps/pointers/uint16/ptr_uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/builtins/bool/json_test.go b/output_tests/slices/builtins/bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/builtins/bool/json_test.go +++ b/output_tests/slices/builtins/bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/builtins/byte/json_test.go b/output_tests/slices/builtins/byte/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/builtins/byte/json_test.go +++ b/output_tests/slices/builtins/byte/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/builtins/float32/json_test.go b/output_tests/slices/builtins/float32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/builtins/float32/json_test.go +++ b/output_tests/slices/builtins/float32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/builtins/float64/json_test.go b/output_tests/slices/builtins/float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/builtins/float64/json_test.go +++ b/output_tests/slices/builtins/float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/builtins/int32/json_test.go b/output_tests/slices/builtins/int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/builtins/int32/json_test.go +++ b/output_tests/slices/builtins/int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/builtins/int8/json_test.go b/output_tests/slices/builtins/int8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/builtins/int8/json_test.go +++ b/output_tests/slices/builtins/int8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/builtins/string/json_test.go b/output_tests/slices/builtins/string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/builtins/string/json_test.go +++ b/output_tests/slices/builtins/string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/builtins/uint8/json_test.go b/output_tests/slices/builtins/uint8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/builtins/uint8/json_test.go +++ b/output_tests/slices/builtins/uint8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/pointers/bool/json_test.go b/output_tests/slices/pointers/bool/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/pointers/bool/json_test.go +++ b/output_tests/slices/pointers/bool/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/pointers/float32/json_test.go b/output_tests/slices/pointers/float32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/pointers/float32/json_test.go +++ b/output_tests/slices/pointers/float32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/pointers/float64/json_test.go b/output_tests/slices/pointers/float64/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/pointers/float64/json_test.go +++ b/output_tests/slices/pointers/float64/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/pointers/int32/json_test.go b/output_tests/slices/pointers/int32/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/pointers/int32/json_test.go +++ b/output_tests/slices/pointers/int32/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/pointers/int8/json_test.go b/output_tests/slices/pointers/int8/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/pointers/int8/json_test.go +++ b/output_tests/slices/pointers/int8/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) } diff --git a/output_tests/slices/pointers/string/json_test.go b/output_tests/slices/pointers/string/json_test.go index f678ec9..a7b1d0d 100644 --- a/output_tests/slices/pointers/string/json_test.go +++ b/output_tests/slices/pointers/string/json_test.go @@ -20,7 +20,7 @@ func Test_Roundtrip(t *testing.T) { if err != nil { t.Errorf("failed to marshal with stdlib: %v", err) } - jbIter, err := jsoniter.Marshal(before) + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) if err != nil { t.Errorf("failed to marshal with jsoniter: %v", err) } @@ -35,7 +35,7 @@ func Test_Roundtrip(t *testing.T) { t.Errorf("failed to unmarshal with stdlib: %v", err) } var afterIter T - err = jsoniter.Unmarshal(jbIter, &afterIter) + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { t.Errorf("failed to unmarshal with jsoniter: %v", err) } @@ -69,7 +69,7 @@ func indent(src []byte, prefix string) string { return buf.String() } -func BenchmarkStandardMarshal(t *testing.B) { +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { t.ReportAllocs() t.ResetTimer() @@ -77,68 +77,63 @@ func BenchmarkStandardMarshal(t *testing.B) { fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) fz.Fuzz(&obj) for i := 0; i < t.N; i++ { - jb, err := json.Marshal(obj) + jb, err := fn(obj) if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) } _ = jb } } +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { + t.ReportAllocs() + t.ResetTimer() + + var before T + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) + fz.Fuzz(&before) + jb, err := json.Marshal(before) + if err != nil { + t.Fatalf("failed to marshal: %v", err) + } + + for i := 0; i < t.N; i++ { + var after T + err = fn(jb, &after) + if err != nil { + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) + } + } +} + +func BenchmarkStandardMarshal(t *testing.B) { + benchmarkMarshal(t, "stdlib", json.Marshal) +} + func BenchmarkStandardUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = json.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) } -func BenchmarkJSONIterMarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := jsoniter.Marshal(obj) - if err != nil { - t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err) - } - _ = jb - } +func BenchmarkJSONIterMarshalFastest(t *testing.B) { + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) } -func BenchmarkJSONIterUnmarshal(t *testing.B) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = jsoniter.Unmarshal(jb, &after) - if err != nil { - t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err) - } - } +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) +} + +func BenchmarkJSONIterMarshalDefault(t *testing.B) { + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) +} + +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) +} + +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) +} + +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) }