-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsort_uniq_test.go
47 lines (39 loc) · 1.3 KB
/
sort_uniq_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package util
import (
"fmt"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSortUniqInPlace(t *testing.T) {
elements := []string{"tag3:tagggg", "tag2:tagval", "tag1:tagval", "tag2:tagval"}
elements = SortUniqInPlace(elements)
assert.ElementsMatch(t, elements, []string{"tag1:tagval", "tag2:tagval", "tag3:tagggg"})
}
func benchmarkDeduplicateTags(b *testing.B, numberOfTags int) {
tags := make([]string, 0, numberOfTags+1)
for i := 0; i < numberOfTags; i++ {
tags = append(tags, fmt.Sprintf("aveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerylong:tag%d", i))
}
// this is the worst case for the insertion sort we are using
sort.Sort(sort.Reverse(sort.StringSlice(tags)))
tempTags := make([]string, len(tags))
copy(tempTags, tags)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
copy(tempTags, tags)
SortUniqInPlace(tempTags)
}
}
func BenchmarkDeduplicateTags(b *testing.B) {
for i := 1; i <= 128; i *= 2 {
b.Run(fmt.Sprintf("deduplicate-%d-tags-in-place", i), func(b *testing.B) {
benchmarkDeduplicateTags(b, i)
})
}
}