| 
1 | 1 | package jsonpatch  | 
2 | 2 | 
 
  | 
3 | 3 | import (  | 
 | 4 | +	"fmt"  | 
4 | 5 | 	"strings"  | 
5 | 6 | 	"testing"  | 
6 | 7 | )  | 
@@ -447,6 +448,45 @@ func TestCreateMergePatchComplexAddAll(t *testing.T) {  | 
447 | 448 | 	}  | 
448 | 449 | }  | 
449 | 450 | 
 
  | 
 | 451 | +// createNestedMap created a series of nested map objects such that the number of  | 
 | 452 | +// objects is roughly 2^n (precisely, 2^(n+1)-1).  | 
 | 453 | +func createNestedMap(m map[string]interface{}, depth int, objectCount *int) {  | 
 | 454 | +	if depth == 0 {  | 
 | 455 | +		return  | 
 | 456 | +	}  | 
 | 457 | +	for i := 0; i< 2;i++ {  | 
 | 458 | +		nested := map[string]interface{}{}  | 
 | 459 | +		*objectCount += 1  | 
 | 460 | +		createNestedMap(nested, depth-1, objectCount)  | 
 | 461 | +		m[fmt.Sprintf("key-%v", i)] = nested  | 
 | 462 | +	}  | 
 | 463 | +}  | 
 | 464 | + | 
 | 465 | +func benchmarkMatchesValueWithDeeplyNestedFields(depth int, b *testing.B) {  | 
 | 466 | +	a := map[string]interface{}{}  | 
 | 467 | +	objCount := 1  | 
 | 468 | +	createNestedMap(a, depth, &objCount)  | 
 | 469 | +	b.ResetTimer()  | 
 | 470 | +	b.Run(fmt.Sprintf("objectCount=%v", objCount), func(b *testing.B) {  | 
 | 471 | +		for i := 0; i < b.N; i++ {  | 
 | 472 | +			if !matchesValue(a, a) {  | 
 | 473 | +				b.Errorf("Should be equal")  | 
 | 474 | +			}  | 
 | 475 | +		}  | 
 | 476 | +	})  | 
 | 477 | +}  | 
 | 478 | + | 
 | 479 | +func BenchmarkMatchesValue1(b *testing.B)  { benchmarkMatchesValueWithDeeplyNestedFields(1, b) }  | 
 | 480 | +func BenchmarkMatchesValue2(b *testing.B)  { benchmarkMatchesValueWithDeeplyNestedFields(2, b) }  | 
 | 481 | +func BenchmarkMatchesValue3(b *testing.B)  { benchmarkMatchesValueWithDeeplyNestedFields(3, b) }  | 
 | 482 | +func BenchmarkMatchesValue4(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(4, b) }  | 
 | 483 | +func BenchmarkMatchesValue5(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(5, b) }  | 
 | 484 | +func BenchmarkMatchesValue6(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(6, b) }  | 
 | 485 | +func BenchmarkMatchesValue7(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(7, b) }  | 
 | 486 | +func BenchmarkMatchesValue8(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(8, b) }  | 
 | 487 | +func BenchmarkMatchesValue9(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(9, b) }  | 
 | 488 | +func BenchmarkMatchesValue10(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(10, b) }  | 
 | 489 | + | 
450 | 490 | func TestCreateMergePatchComplexRemoveAll(t *testing.T) {  | 
451 | 491 | 	doc := `{"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4], "nested": {"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4]} }`  | 
452 | 492 | 	exp := `{"a":null,"f":null,"hello":null,"i":null,"n":null,"nested":null,"pi":null,"t":null}`  | 
 | 
0 commit comments