Skip to content

Commit

Permalink
remove MergeKVs tests
Browse files Browse the repository at this point in the history
MergeKVs is about to become a lot simpler. Removing the tests and benchmarks
simplifies the before/after performance comparison.
  • Loading branch information
pohly committed Nov 21, 2024
1 parent 3cfa068 commit 48fdaa7
Showing 1 changed file with 0 additions and 138 deletions.
138 changes: 0 additions & 138 deletions internal/serialize/keyvalues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package serialize_test
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -191,140 +190,3 @@ No whitespace.`,
}
}
}

func TestDuplicates(t *testing.T) {
for name, test := range map[string]struct {
first, second []interface{}
expected []interface{}
}{
"empty": {},
"no-duplicates": {
first: makeKV("a", 3),
second: makeKV("b", 3),
expected: append(makeKV("a", 3), makeKV("b", 3)...),
},
"all-duplicates": {
first: makeKV("a", 3),
second: makeKV("a", 3),
expected: makeKV("a", 3),
},
"start-duplicate": {
first: append([]interface{}{"x", 1}, makeKV("a", 3)...),
second: append([]interface{}{"x", 2}, makeKV("b", 3)...),
expected: append(append(makeKV("a", 3), "x", 2), makeKV("b", 3)...),
},
"subset-first": {
first: append([]interface{}{"x", 1}, makeKV("a", 3)...),
second: append([]interface{}{"x", 2}, makeKV("a", 3)...),
expected: append([]interface{}{"x", 2}, makeKV("a", 3)...),
},
"subset-second": {
first: append([]interface{}{"x", 1}, makeKV("a", 1)...),
second: append([]interface{}{"x", 2}, makeKV("b", 2)...),
expected: append(append(makeKV("a", 1), "x", 2), makeKV("b", 2)...),
},
"end-duplicate": {
first: append(makeKV("a", 3), "x", 1),
second: append(makeKV("b", 3), "x", 2),
expected: append(makeKV("a", 3), append(makeKV("b", 3), "x", 2)...),
},
"middle-duplicate": {
first: []interface{}{"a-0", 0, "x", 1, "a-1", 2},
second: []interface{}{"b-0", 0, "x", 2, "b-1", 2},
expected: []interface{}{"a-0", 0, "a-1", 2, "b-0", 0, "x", 2, "b-1", 2},
},
"internal-duplicates": {
first: []interface{}{"a", 0, "x", 1, "a", 2},
second: []interface{}{"b", 0, "x", 2, "b", 2},
// This is the case where Merged keeps key/value pairs
// that were already duplicated inside the slices, for
// performance.
expected: []interface{}{"a", 0, "a", 2, "b", 0, "x", 2, "b", 2},
},
} {
t.Run(name, func(t *testing.T) {
t.Run("Merged", func(t *testing.T) {
actual := serialize.MergeKVs(test.first, test.second)
expectEqual(t, "merged key/value pairs", test.expected, actual)
})
})
}
}

// BenchmarkMergeKVs checks performance when MergeKVs is called with two slices.
// In practice that is how the function is used.
func BenchmarkMergeKVs(b *testing.B) {
for firstLength := 0; firstLength < 10; firstLength++ {
firstA := makeKV("a", firstLength)
for secondLength := 0; secondLength < 10; secondLength++ {
secondA := makeKV("a", secondLength)
secondB := makeKV("b", secondLength)
b.Run(fmt.Sprintf("%dx%d", firstLength, secondLength), func(b *testing.B) {
// This is the most common case: all key/value pairs are kept.
b.Run("no-duplicates", func(b *testing.B) {
expected := append(firstA, secondB...)
benchMergeKVs(b, expected, firstA, secondB)
})

// Fairly unlikely...
b.Run("all-duplicates", func(b *testing.B) {
var expected []interface{}
if firstLength > secondLength {
expected = firstA[secondLength*2:]
}
expected = append(expected, secondA...)
benchMergeKVs(b, expected, firstA, secondA)
})

// First entry is the same.
b.Run("start-duplicate", func(b *testing.B) {
first := []interface{}{"x", 1}
first = append(first, firstA...)
second := []interface{}{"x", 1}
second = append(second, secondB...)
expected := append(firstA, second...)
benchMergeKVs(b, expected, first, second)
})

// Last entry is the same.
b.Run("end-duplicate", func(b *testing.B) {
first := firstA[:]
first = append(first, "x", 1)
second := secondB[:]
second = append(second, "x", 1)
expected := append(firstA, second...)
benchMergeKVs(b, expected, first, second)
})
})
}
}
}

func makeKV(prefix string, length int) []interface{} {
if length == 0 {
return []interface{}{}
}
kv := make([]interface{}, 0, length*2)
for i := 0; i < length; i++ {
kv = append(kv, fmt.Sprintf("%s-%d", prefix, i), i)
}
return kv
}

func benchMergeKVs(b *testing.B, expected []interface{}, first, second []interface{}) {
if len(expected) == 0 {
expected = nil
}
actual := serialize.MergeKVs(first, second)
expectEqual(b, "trimmed key/value pairs", expected, actual)
b.ResetTimer()
for i := 0; i < b.N; i++ {
serialize.MergeKVs(first, second)
}
}

func expectEqual(tb testing.TB, what string, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
tb.Fatalf("Did not get correct %s. Expected:\n %v\nActual:\n %v", what, expected, actual)
}
}

0 comments on commit 48fdaa7

Please sign in to comment.