|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/davecgh/go-spew/spew" |
| 11 | + fuzz "github.com/google/gofuzz" |
| 12 | + jsoniter "github.com/json-iterator/go" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_Roundtrip(t *testing.T) { |
| 16 | + fz := fuzz.New().MaxDepth(10).NilChance(0.3) |
| 17 | + for i := 0; i < 1000; i++ { |
| 18 | + var before T |
| 19 | + fz.Fuzz(&before) |
| 20 | + |
| 21 | + jbStd, err := json.Marshal(before) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("failed to marshal with stdlib: %v", err) |
| 24 | + } |
| 25 | + if len(strings.TrimSpace(string(jbStd))) == 0 { |
| 26 | + t.Fatal("stdlib marshal produced empty result and no error") |
| 27 | + } |
| 28 | + jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("failed to marshal with jsoniter: %v", err) |
| 31 | + } |
| 32 | + if len(strings.TrimSpace(string(jbIter))) == 0 { |
| 33 | + t.Fatal("jsoniter marshal produced empty result and no error") |
| 34 | + } |
| 35 | + if string(jbStd) != string(jbIter) { |
| 36 | + t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", |
| 37 | + indent(jbStd, " "), indent(jbIter, " "), dump(before)) |
| 38 | + } |
| 39 | + |
| 40 | + var afterStd T |
| 41 | + err = json.Unmarshal(jbIter, &afterStd) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", |
| 44 | + err, indent(jbIter, " ")) |
| 45 | + } |
| 46 | + var afterIter T |
| 47 | + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", |
| 50 | + err, indent(jbIter, " ")) |
| 51 | + } |
| 52 | + if fingerprint(afterStd) != fingerprint(afterIter) { |
| 53 | + t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", |
| 54 | + dump(afterStd), dump(afterIter), indent(jbIter, " ")) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +const indentStr = "> " |
| 60 | + |
| 61 | +func fingerprint(obj interface{}) string { |
| 62 | + c := spew.ConfigState{ |
| 63 | + SortKeys: true, |
| 64 | + SpewKeys: true, |
| 65 | + } |
| 66 | + return c.Sprintf("%v", obj) |
| 67 | +} |
| 68 | + |
| 69 | +func dump(obj interface{}) string { |
| 70 | + cfg := spew.ConfigState{ |
| 71 | + Indent: indentStr, |
| 72 | + } |
| 73 | + return cfg.Sdump(obj) |
| 74 | +} |
| 75 | + |
| 76 | +func indent(src []byte, prefix string) string { |
| 77 | + var buf bytes.Buffer |
| 78 | + err := json.Indent(&buf, src, prefix, indentStr) |
| 79 | + if err != nil { |
| 80 | + return fmt.Sprintf("!!! %v", err) |
| 81 | + } |
| 82 | + return buf.String() |
| 83 | +} |
| 84 | + |
| 85 | +func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { |
| 86 | + t.ReportAllocs() |
| 87 | + t.ResetTimer() |
| 88 | + |
| 89 | + var obj T |
| 90 | + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) |
| 91 | + fz.Fuzz(&obj) |
| 92 | + for i := 0; i < t.N; i++ { |
| 93 | + jb, err := fn(obj) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) |
| 96 | + } |
| 97 | + _ = jb |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { |
| 102 | + t.ReportAllocs() |
| 103 | + t.ResetTimer() |
| 104 | + |
| 105 | + var before T |
| 106 | + fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) |
| 107 | + fz.Fuzz(&before) |
| 108 | + jb, err := json.Marshal(before) |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("failed to marshal: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + for i := 0; i < t.N; i++ { |
| 114 | + var after T |
| 115 | + err = fn(jb, &after) |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func BenchmarkStandardMarshal(t *testing.B) { |
| 123 | + benchmarkMarshal(t, "stdlib", json.Marshal) |
| 124 | +} |
| 125 | + |
| 126 | +func BenchmarkStandardUnmarshal(t *testing.B) { |
| 127 | + benchmarkUnmarshal(t, "stdlib", json.Unmarshal) |
| 128 | +} |
| 129 | + |
| 130 | +func BenchmarkJSONIterMarshalFastest(t *testing.B) { |
| 131 | + benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) |
| 132 | +} |
| 133 | + |
| 134 | +func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { |
| 135 | + benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) |
| 136 | +} |
| 137 | + |
| 138 | +func BenchmarkJSONIterMarshalDefault(t *testing.B) { |
| 139 | + benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) |
| 140 | +} |
| 141 | + |
| 142 | +func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { |
| 143 | + benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) |
| 144 | +} |
| 145 | + |
| 146 | +func BenchmarkJSONIterMarshalCompatible(t *testing.B) { |
| 147 | + benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) |
| 148 | +} |
| 149 | + |
| 150 | +func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { |
| 151 | + benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) |
| 152 | +} |
0 commit comments