mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Add distinct IP counter
This commit is contained in:
parent
97dea533da
commit
211254fa98
8 changed files with 307 additions and 0 deletions
47
common/ipsetsink/sink_test.go
Normal file
47
common/ipsetsink/sink_test.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package ipsetsink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/clarkduvall/hyperloglog"
|
||||
"testing"
|
||||
)
|
||||
import . "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
func TestSinkInit(t *testing.T) {
|
||||
Convey("Context", t, func() {
|
||||
sink := NewIPSetSink("demo")
|
||||
sink.AddIPToSet("test1")
|
||||
sink.AddIPToSet("test2")
|
||||
data, err := sink.Dump()
|
||||
So(err, ShouldBeNil)
|
||||
structure, err := hyperloglog.NewPlus(18)
|
||||
So(err, ShouldBeNil)
|
||||
err = structure.GobDecode(data)
|
||||
So(err, ShouldBeNil)
|
||||
count := structure.Count()
|
||||
So(count, ShouldBeBetweenOrEqual, 1, 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSinkCounting(t *testing.T) {
|
||||
Convey("Context", t, func() {
|
||||
for itemCount := 300; itemCount <= 10000; itemCount += 200 {
|
||||
sink := NewIPSetSink("demo")
|
||||
for i := 0; i <= itemCount; i++ {
|
||||
sink.AddIPToSet(fmt.Sprintf("demo%v", i))
|
||||
}
|
||||
for i := 0; i <= itemCount; i++ {
|
||||
sink.AddIPToSet(fmt.Sprintf("demo%v", i))
|
||||
}
|
||||
data, err := sink.Dump()
|
||||
So(err, ShouldBeNil)
|
||||
structure, err := hyperloglog.NewPlus(18)
|
||||
So(err, ShouldBeNil)
|
||||
err = structure.GobDecode(data)
|
||||
So(err, ShouldBeNil)
|
||||
count := structure.Count()
|
||||
So((float64(count)/float64(itemCount))-1.0, ShouldAlmostEqual, 0, 0.01)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue