|
1 | 1 | package detected |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "runtime" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/axiomhq/hyperloglog" |
@@ -88,6 +89,27 @@ func Test_MergeFields(t *testing.T) { |
88 | 89 | assert.Equal(t, logproto.DetectedFieldString, baz.Type) |
89 | 90 | }) |
90 | 91 |
|
| 92 | + t.Run("huge limit doesn't explode the heap", func(t *testing.T) { |
| 93 | + runtime.GC() |
| 94 | + var before runtime.MemStats |
| 95 | + runtime.ReadMemStats(&before) |
| 96 | + |
| 97 | + result, err := MergeFields(fields, 10000000) |
| 98 | + require.NoError(t, err) |
| 99 | + |
| 100 | + runtime.GC() |
| 101 | + var after runtime.MemStats |
| 102 | + runtime.ReadMemStats(&after) |
| 103 | + |
| 104 | + delta := int64(after.TotalAlloc) - int64(before.TotalAlloc) |
| 105 | + // 10 MB |
| 106 | + if delta > 10*1024*1024 { |
| 107 | + t.Fatalf("heap grew too much: %d MB", delta/1024/1024) |
| 108 | + } |
| 109 | + |
| 110 | + runtime.KeepAlive(result) |
| 111 | + }) |
| 112 | + |
91 | 113 | t.Run("returns up to limit number of fields", func(t *testing.T) { |
92 | 114 | lowLimit := uint32(1) |
93 | 115 | result, err := MergeFields(fields, lowLimit) |
@@ -125,6 +147,28 @@ func Test_MergeValues(t *testing.T) { |
125 | 147 | assert.ElementsMatch(t, []string{"foo", "bar", "baz", "qux"}, result) |
126 | 148 | }) |
127 | 149 |
|
| 150 | + t.Run("huge limit doesn't explode the heap", func(t *testing.T) { |
| 151 | + runtime.GC() |
| 152 | + var before runtime.MemStats |
| 153 | + runtime.ReadMemStats(&before) |
| 154 | + |
| 155 | + values := []string{"foo", "bar", "baz", "qux"} |
| 156 | + result, err := MergeValues(values, 1000000) |
| 157 | + require.NoError(t, err) |
| 158 | + |
| 159 | + runtime.GC() |
| 160 | + var after runtime.MemStats |
| 161 | + runtime.ReadMemStats(&after) |
| 162 | + |
| 163 | + delta := int64(after.TotalAlloc) - int64(before.TotalAlloc) |
| 164 | + // 10 MB |
| 165 | + if delta > 10*1024*1024 { |
| 166 | + t.Fatalf("heap grew too much: %d MB", delta/1024/1024) |
| 167 | + } |
| 168 | + |
| 169 | + runtime.KeepAlive(result) |
| 170 | + }) |
| 171 | + |
128 | 172 | t.Run("merges repeating values", func(t *testing.T) { |
129 | 173 | values := []string{"foo", "bar", "baz", "qux", "foo", "bar", "baz", "qux"} |
130 | 174 | limit := uint32(50) |
|
0 commit comments