From 471ab0714f3966621f9f40aa707f3d12b0711410 Mon Sep 17 00:00:00 2001 From: Philipp Winter Date: Wed, 30 Sep 2020 09:35:36 -0700 Subject: [PATCH] Add benchmark for IsCached. Thanks to Cecylia for questioning whether the cache lookup mechanism is fast enough: https://gitlab.torproject.org/tpo/anti-censorship/bridgestrap/-/commit/ecc6e0487655af9ab6f91ff4fbab7ce76d11caa7#note_2709958 --- tor_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tor_test.go b/tor_test.go index 50ad590..94b8a52 100644 --- a/tor_test.go +++ b/tor_test.go @@ -2,8 +2,10 @@ package main import ( "bytes" + "errors" "fmt" "io/ioutil" + "math/rand" "net" "os" "testing" @@ -107,6 +109,30 @@ func TestCacheExpiration(t *testing.T) { } } +func BenchmarkIsCached(b *testing.B) { + + getRandAddrPort := func() string { + return fmt.Sprintf("%d.%d.%d.%d:%d", + rand.Intn(256), rand.Intn(256), rand.Intn(256), rand.Intn(256), rand.Intn(65536)) + } + getRandError := func() error { + errors := []error{nil, errors.New("censorship"), errors.New("no censorship")} + return errors[rand.Intn(len(errors))] + } + + numCacheEntries := 10000 + cache := make(TestCache) + for i := 0; i < numCacheEntries; i++ { + cache.AddEntry(getRandAddrPort(), getRandError()) + } + + // How long does it take to iterate over numCacheEntries cache entries? + b.ResetTimer() + for i := 0; i < b.N; i++ { + cache.IsCached("invalid bridge line") + } +} + func TestCacheSerialisation(t *testing.T) { cache := make(TestCache) -- GitLab