BenchmarkMarshalDeterministic 与标准 json.Marshal 对比
(b *testing.B)
| 256 | |
| 257 | // BenchmarkMarshalDeterministic 与标准 json.Marshal 对比 |
| 258 | func BenchmarkMarshalDeterministic(b *testing.B) { |
| 259 | m := map[string]any{ |
| 260 | "zebra": 1, |
| 261 | "apple": 2, |
| 262 | "mango": 3, |
| 263 | "nested": map[string]int{ |
| 264 | "c": 3, |
| 265 | "a": 1, |
| 266 | "b": 2, |
| 267 | }, |
| 268 | } |
| 269 | |
| 270 | b.Run("Deterministic", func(b *testing.B) { |
| 271 | for i := 0; i < b.N; i++ { |
| 272 | _, _ = MarshalDeterministic(m) |
| 273 | } |
| 274 | }) |
| 275 | |
| 276 | b.Run("StandardJSON", func(b *testing.B) { |
| 277 | for i := 0; i < b.N; i++ { |
| 278 | _, _ = json.Marshal(m) |
| 279 | } |
| 280 | }) |
| 281 | } |
| 282 | |
| 283 | // 验证标准 json.Marshal 确实是不确定的 |
| 284 | func TestStandardJSONIsNonDeterministic(t *testing.T) { |
nothing calls this directly
no test coverage detected