|
1 | 1 | package astjson |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
4 | 7 | "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
5 | 10 | ) |
6 | 11 |
|
7 | 12 | func TestObjectDelSet(t *testing.T) { |
@@ -101,3 +106,109 @@ func TestValueDelSet(t *testing.T) { |
101 | 106 | v.Set("x", MustParse(`[]`)) |
102 | 107 | v.SetArrayItem(1, MustParse(`[]`)) |
103 | 108 | } |
| 109 | + |
| 110 | +func TestValue_AppendArrayItems(t *testing.T) { |
| 111 | + left := MustParse(`[1,2,3]`) |
| 112 | + right := MustParse(`[4,5,6]`) |
| 113 | + left.AppendArrayItems(right) |
| 114 | + if len(left.GetArray()) != 6 { |
| 115 | + t.Fatalf("unexpected length; got %d; want %d", len(left.GetArray()), 6) |
| 116 | + } |
| 117 | + out := left.MarshalTo(nil) |
| 118 | + if string(out) != `[1,2,3,4,5,6]` { |
| 119 | + t.Fatalf("unexpected output; got %q; want %q", out, `[1,2,3,4,5,6]`) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestMergeWithSwap(t *testing.T) { |
| 124 | + left := MustParse(`{"a":{"b":1,"c":2,"e":[],"f":[1],"h":[1,2,3]}}`) |
| 125 | + right := MustParse(`{"a":{"b":2,"d":3,"e":[1,2,3],"g":[1],"h":[4,5,6]}}`) |
| 126 | + out, _ := MergeValues(left, right) |
| 127 | + assert.Equal(t, `{"a":{"b":2,"c":2,"e":[1,2,3],"f":[1],"h":[4,5,6],"d":3,"g":[1]}}`, out.String()) |
| 128 | +} |
| 129 | + |
| 130 | +type RootObject struct { |
| 131 | + Child *ChildObject `json:"child"` |
| 132 | +} |
| 133 | + |
| 134 | +type ChildObject struct { |
| 135 | + GrandChild *GrandChildObject `json:"grand_child"` |
| 136 | +} |
| 137 | + |
| 138 | +type GrandChildObject struct { |
| 139 | + Items []string `json:"items"` |
| 140 | +} |
| 141 | + |
| 142 | +func BenchmarkValue_SetArrayItem(b *testing.B) { |
| 143 | + |
| 144 | + root := &RootObject{ |
| 145 | + Child: &ChildObject{ |
| 146 | + GrandChild: &GrandChildObject{ |
| 147 | + Items: make([]string, 0), |
| 148 | + }, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + l, err := json.Marshal(root) |
| 153 | + assert.NoError(b, err) |
| 154 | + |
| 155 | + root.Child.GrandChild.Items = make([]string, 1024*1024) |
| 156 | + |
| 157 | + for i := 0; i < 1024*1024; i++ { |
| 158 | + root.Child.GrandChild.Items[i] = strings.Repeat("a", 1024) |
| 159 | + } |
| 160 | + |
| 161 | + r, err := json.Marshal(root) |
| 162 | + assert.NoError(b, err) |
| 163 | + |
| 164 | + b.ResetTimer() |
| 165 | + b.ReportAllocs() |
| 166 | + |
| 167 | + for i := 0; i < b.N; i++ { |
| 168 | + l, _ := ParseBytesWithoutCache(l) |
| 169 | + r, _ := ParseBytesWithoutCache(r) |
| 170 | + out, _ := MergeValues(l, r) |
| 171 | + arr := out.GetArray("child", "grand_child", "items") |
| 172 | + assert.Len(b, arr, 1024*1024) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func BenchmarkMergeValuesWithATonOfRecursion(b *testing.B) { |
| 177 | + b.ReportAllocs() |
| 178 | + |
| 179 | + left := MustParse(`{"a":{}}`) |
| 180 | + str := fmt.Sprintf( |
| 181 | + `{"ba":{"bb":{"bc":{"bd":[%s, %s, %s], "be": {"bf": %s}}}}}`, |
| 182 | + objectWithRecursion(10), |
| 183 | + objectWithRecursion(20), |
| 184 | + objectWithRecursion(2), |
| 185 | + objectWithRecursion(3)) |
| 186 | + |
| 187 | + expected := fmt.Sprintf( |
| 188 | + `{"a":{},"ba":{"bb":{"bc":{"bd":[%s,%s,%s],"be":{"bf":%s}}}}}`, |
| 189 | + objectWithRecursion(10), |
| 190 | + objectWithRecursion(20), |
| 191 | + objectWithRecursion(2), |
| 192 | + objectWithRecursion(3)) |
| 193 | + |
| 194 | + _ = expected |
| 195 | + |
| 196 | + right := MustParse(str) |
| 197 | + |
| 198 | + for i := 0; i < b.N; i++ { |
| 199 | + _, _ = MergeValues(left, right) |
| 200 | + /*v, _ := MergeValues(left, right) |
| 201 | + if v.String() != expected { |
| 202 | + assert.Equal(b, expected, v.String()) |
| 203 | + }*/ |
| 204 | + } |
| 205 | + |
| 206 | + fmt.Printf("left: %s\n", left.String()) |
| 207 | +} |
| 208 | + |
| 209 | +func objectWithRecursion(depth int) string { |
| 210 | + if depth == 0 { |
| 211 | + return `{}` |
| 212 | + } |
| 213 | + return `{"a":` + objectWithRecursion(depth-1) + `}` |
| 214 | +} |
0 commit comments