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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
// Copyright ©2019 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package card
import (
"encoding"
"fmt"
"hash"
"hash/fnv"
"io"
"strconv"
"strings"
"sync"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/floats/scalar"
)
// exact is an exact cardinality accumulator.
type exact map[string]struct{}
func (e exact) Write(b []byte) (int, error) {
if _, exists := e[string(b)]; exists {
return len(b), nil
}
e[string(b)] = struct{}{}
return len(b), nil
}
func (e exact) Count() float64 {
return float64(len(e))
}
type counter interface {
io.Writer
Count() float64
}
var counterTests = []struct {
name string
count float64
counter func() counter
tol float64
}{
{name: "exact-1e5", count: 1e5, counter: func() counter { return make(exact) }, tol: 0},
{name: "HyperLogLog32-0-10-FNV-1a", count: 0, counter: func() counter { return mustCounter(NewHyperLogLog32(10, fnv.New32a())) }, tol: 0},
{name: "HyperLogLog64-0-10-FNV-1a", count: 0, counter: func() counter { return mustCounter(NewHyperLogLog64(10, fnv.New64a())) }, tol: 0},
{name: "HyperLogLog32-10-14-FNV-1a", count: 10, counter: func() counter { return mustCounter(NewHyperLogLog32(14, fnv.New32a())) }, tol: 0.0005},
{name: "HyperLogLog32-1e3-4-FNV-1a", count: 1e3, counter: func() counter { return mustCounter(NewHyperLogLog32(4, fnv.New32a())) }, tol: 0.1},
{name: "HyperLogLog32-1e4-6-FNV-1a", count: 1e4, counter: func() counter { return mustCounter(NewHyperLogLog32(6, fnv.New32a())) }, tol: 0.06},
{name: "HyperLogLog32-1e7-8-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog32(8, fnv.New32a())) }, tol: 0.03},
{name: "HyperLogLog64-1e7-8-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog64(8, fnv.New64a())) }, tol: 0.07},
{name: "HyperLogLog32-1e7-10-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog32(10, fnv.New32a())) }, tol: 0.06},
{name: "HyperLogLog64-1e7-10-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog64(10, fnv.New64a())) }, tol: 0.02},
{name: "HyperLogLog32-1e7-14-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog32(14, fnv.New32a())) }, tol: 0.005},
{name: "HyperLogLog64-1e7-14-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog64(14, fnv.New64a())) }, tol: 0.002},
{name: "HyperLogLog32-1e7-16-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog32(16, fnv.New32a())) }, tol: 0.005},
{name: "HyperLogLog64-1e7-16-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog64(16, fnv.New64a())) }, tol: 0.002},
{name: "HyperLogLog64-1e7-20-FNV-1a", count: 1e7, counter: func() counter { return mustCounter(NewHyperLogLog64(20, fnv.New64a())) }, tol: 0.001},
{name: "HyperLogLog64-1e3-20-FNV-1a", count: 1e3, counter: func() counter { return mustCounter(NewHyperLogLog64(20, fnv.New64a())) }, tol: 0.001},
}
func mustCounter(c counter, err error) counter {
if err != nil {
panic(fmt.Sprintf("bad test: %v", err))
}
return c
}
func TestCounters(t *testing.T) {
t.Parallel()
for _, test := range counterTests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rnd := rand.New(rand.NewSource(1))
var dst []byte
c := test.counter()
for i := 0; i < int(test.count); i++ {
dst = strconv.AppendUint(dst[:0], rnd.Uint64(), 16)
dst = append(dst, '-')
dst = strconv.AppendUint(dst, uint64(i), 16)
n, err := c.Write(dst)
if n != len(dst) {
t.Errorf("unexpected number of bytes written for %s: got:%d want:%d",
test.name, n, len(dst))
break
}
if err != nil {
t.Errorf("unexpected error for %s: %v", test.name, err)
break
}
}
if got := c.Count(); !scalar.EqualWithinRel(got, test.count, test.tol) {
t.Errorf("unexpected count for %s: got:%.0f want:%.0f", test.name, got, test.count)
}
})
}
}
func TestUnion(t *testing.T) {
t.Parallel()
for _, test := range counterTests {
if strings.HasPrefix(test.name, "exact") {
continue
}
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rnd := rand.New(rand.NewSource(1))
var dst []byte
var cs [2]counter
for j := range cs {
cs[j] = test.counter()
for i := 0; i < int(test.count); i++ {
dst = strconv.AppendUint(dst[:0], rnd.Uint64(), 16)
dst = append(dst, '-')
dst = strconv.AppendUint(dst, uint64(i), 16)
n, err := cs[j].Write(dst)
if n != len(dst) {
t.Errorf("unexpected number of bytes written for %s: got:%d want:%d",
test.name, n, len(dst))
break
}
if err != nil {
t.Errorf("unexpected error for %s: %v", test.name, err)
break
}
}
}
u := test.counter()
var err error
switch u := u.(type) {
case *HyperLogLog32:
err = u.Union(cs[0].(*HyperLogLog32), cs[1].(*HyperLogLog32))
case *HyperLogLog64:
err = u.Union(cs[0].(*HyperLogLog64), cs[1].(*HyperLogLog64))
}
if err != nil {
t.Errorf("unexpected error from Union call: %v", err)
}
if got := u.Count(); !scalar.EqualWithinRel(got, 2*test.count, 2*test.tol) {
t.Errorf("unexpected count for %s: got:%.0f want:%.0f", test.name, got, 2*test.count)
}
})
}
}
type resetCounter interface {
counter
Reset()
}
var counterResetTests = []struct {
name string
count int
resetCounter func() resetCounter
}{
{name: "HyperLogLog32-1e3-4-FNV-1a", count: 1e3, resetCounter: func() resetCounter { return mustResetCounter(NewHyperLogLog32(4, fnv.New32a())) }},
{name: "HyperLogLog64-1e3-4-FNV-1a", count: 1e3, resetCounter: func() resetCounter { return mustResetCounter(NewHyperLogLog64(4, fnv.New64a())) }},
{name: "HyperLogLog32-1e4-6-FNV-1a", count: 1e4, resetCounter: func() resetCounter { return mustResetCounter(NewHyperLogLog32(6, fnv.New32a())) }},
{name: "HyperLogLog64-1e4-6-FNV-1a", count: 1e4, resetCounter: func() resetCounter { return mustResetCounter(NewHyperLogLog64(6, fnv.New64a())) }},
}
func mustResetCounter(c resetCounter, err error) resetCounter {
if err != nil {
panic(fmt.Sprintf("bad test: %v", err))
}
return c
}
func TestResetCounters(t *testing.T) {
t.Parallel()
var dst []byte
for _, test := range counterResetTests {
c := test.resetCounter()
var counts [2]float64
for k := range counts {
rnd := rand.New(rand.NewSource(1))
for i := 0; i < test.count; i++ {
dst = strconv.AppendUint(dst[:0], rnd.Uint64(), 16)
dst = append(dst, '-')
dst = strconv.AppendUint(dst, uint64(i), 16)
n, err := c.Write(dst)
if n != len(dst) {
t.Errorf("unexpected number of bytes written for %s: got:%d want:%d",
test.name, n, len(dst))
break
}
if err != nil {
t.Errorf("unexpected error for %s: %v", test.name, err)
break
}
}
counts[k] = c.Count()
c.Reset()
}
if counts[0] != counts[1] {
t.Errorf("unexpected counts for %s after reset: got:%.0f", test.name, counts)
}
}
}
type counterEncoder interface {
counter
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
var counterEncoderTests = []struct {
name string
count int
src, dst, zdst func() counterEncoder
}{
{
name: "HyperLogLog32-4-4-FNV-1a", count: 1e3,
src: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog32(4, fnv.New32a())) },
dst: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog32(4, fnv.New32a())) },
zdst: func() counterEncoder { return &HyperLogLog32{} },
},
{
name: "HyperLogLog32-4-8-FNV-1a", count: 1e3,
src: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog32(4, fnv.New32a())) },
dst: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog32(8, fnv.New32a())) },
zdst: func() counterEncoder { return &HyperLogLog32{} },
},
{
name: "HyperLogLog32-8-4-FNV-1a", count: 1e3,
src: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog32(8, fnv.New32a())) },
dst: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog32(4, fnv.New32a())) },
zdst: func() counterEncoder { return &HyperLogLog32{} },
},
{
name: "HyperLogLog64-4-4-FNV-1a", count: 1e3,
src: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog64(4, fnv.New64a())) },
dst: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog64(4, fnv.New64a())) },
zdst: func() counterEncoder { return &HyperLogLog64{} },
},
{
name: "HyperLogLog64-4-8-FNV-1a", count: 1e3,
src: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog64(4, fnv.New64a())) },
dst: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog64(8, fnv.New64a())) },
zdst: func() counterEncoder { return &HyperLogLog64{} },
},
{
name: "HyperLogLog64-8-4-FNV-1a", count: 1e3,
src: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog64(8, fnv.New64a())) },
dst: func() counterEncoder { return mustCounterEncoder(NewHyperLogLog64(4, fnv.New64a())) },
zdst: func() counterEncoder { return &HyperLogLog64{} },
},
}
func mustCounterEncoder(c counterEncoder, err error) counterEncoder {
if err != nil {
panic(fmt.Sprintf("bad test: %v", err))
}
return c
}
func TestBinaryEncoding(t *testing.T) {
t.Parallel()
RegisterHash(fnv.New32a)
RegisterHash(fnv.New64a)
defer func() {
hashes = sync.Map{}
}()
for _, test := range counterEncoderTests {
rnd := rand.New(rand.NewSource(1))
src := test.src()
for i := 0; i < test.count; i++ {
buf := strconv.AppendUint(nil, rnd.Uint64(), 16)
buf = append(buf, '-')
buf = strconv.AppendUint(buf, uint64(i), 16)
n, err := src.Write(buf)
if n != len(buf) {
t.Errorf("unexpected number of bytes written for %s: got:%d want:%d",
test.name, n, len(buf))
break
}
if err != nil {
t.Errorf("unexpected error for %s: %v", test.name, err)
break
}
}
buf, err := src.MarshalBinary()
if err != nil {
t.Errorf("unexpected error marshaling binary for %s: %v", test.name, err)
continue
}
dst := test.dst()
err = dst.UnmarshalBinary(buf)
if err != nil {
t.Errorf("unexpected error unmarshaling binary for %s: %v", test.name, err)
continue
}
zdst := test.zdst()
err = zdst.UnmarshalBinary(buf)
if err != nil {
t.Errorf("unexpected error unmarshaling binary into zero receiver for %s: %v", test.name, err)
continue
}
gotSrc := src.Count()
gotDst := dst.Count()
gotZdst := zdst.Count()
if gotSrc != gotDst {
t.Errorf("unexpected count for %s: got:%.0f want:%.0f", test.name, gotDst, gotSrc)
}
if gotSrc != gotZdst {
t.Errorf("unexpected count for %s into zero receiver: got:%.0f want:%.0f", test.name, gotZdst, gotSrc)
}
}
}
var invalidRegisterTests = []struct {
fn interface{}
panics bool
}{
{fn: int(0), panics: true},
{fn: func() {}, panics: true},
{fn: func(int) {}, panics: true},
{fn: func() int { return 0 }, panics: true},
{fn: func() hash.Hash { return fnv.New32a() }, panics: true},
{fn: func() hash.Hash32 { return fnv.New32a() }, panics: false},
{fn: func() hash.Hash { return fnv.New64a() }, panics: true},
{fn: func() hash.Hash64 { return fnv.New64a() }, panics: false},
}
func TestRegisterInvalid(t *testing.T) {
t.Parallel()
for _, test := range invalidRegisterTests {
var r interface{}
func() {
defer func() {
r = recover()
}()
RegisterHash(test.fn)
}()
panicked := r != nil
if panicked != test.panics {
if panicked {
t.Errorf("unexpected panic for %T", test.fn)
} else {
t.Errorf("expected panic for %T", test.fn)
}
}
}
}
var rhoQTests = []struct {
bits uint
q uint8
want uint8
}{
{bits: 0xff, q: 8, want: 1},
{bits: 0xfe, q: 8, want: 1},
{bits: 0x0f, q: 8, want: 5},
{bits: 0x1f, q: 8, want: 4},
{bits: 0x00, q: 8, want: 9},
}
func TestRhoQ(t *testing.T) {
t.Parallel()
for _, test := range rhoQTests {
got := rho32q(uint32(test.bits), test.q)
if got != test.want {
t.Errorf("unexpected rho32q for %0*b: got:%d want:%d", test.q, test.bits, got, test.want)
}
got = rho64q(uint64(test.bits), test.q)
if got != test.want {
t.Errorf("unexpected rho64q for %0*b: got:%d want:%d", test.q, test.bits, got, test.want)
}
}
}
var counterBenchmarks = []struct {
name string
count int
counter func() counter
}{
{name: "exact-1e6", count: 1e6, counter: func() counter { return make(exact) }},
{name: "HyperLogLog32-1e6-8-FNV-1a", count: 1e6, counter: func() counter { return mustCounter(NewHyperLogLog32(8, fnv.New32a())) }},
{name: "HyperLogLog64-1e6-8-FNV-1a", count: 1e6, counter: func() counter { return mustCounter(NewHyperLogLog64(8, fnv.New64a())) }},
{name: "HyperLogLog32-1e6-16-FNV-1a", count: 1e6, counter: func() counter { return mustCounter(NewHyperLogLog32(16, fnv.New32a())) }},
{name: "HyperLogLog64-1e6-16-FNV-1a", count: 1e6, counter: func() counter { return mustCounter(NewHyperLogLog64(16, fnv.New64a())) }},
}
func BenchmarkCounters(b *testing.B) {
for _, bench := range counterBenchmarks {
c := bench.counter()
rnd := rand.New(rand.NewSource(1))
var dst []byte
b.Run(bench.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < bench.count; j++ {
dst = strconv.AppendUint(dst[:0], rnd.Uint64(), 16)
dst = append(dst, '-')
dst = strconv.AppendUint(dst, uint64(j), 16)
_, _ = c.Write(dst)
}
}
_ = c.Count()
})
}
}
|