Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace Events Exporting As Logs #30

Merged
merged 8 commits into from
Mar 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
cuckoo unit test cases
  • Loading branch information
saikalyan.bhagavathula committed Jan 30, 2024
commit e36f5c8ec3b01f12e67ba8e0bbf517e3962931d0
156 changes: 156 additions & 0 deletions collect/cache/cuckoo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package cache

import (
"fmt"
"github.com/dgryski/go-wyhash"
"github.com/opsramp/tracing-proxy/metrics"
"github.com/sourcegraph/conc/pool"
"math/rand"

"testing"
"time"
)

// genID returns a random hex string of length numChars
func genID(numChars int) string {
seed := 3565269841805

const charset = "abcdef0123456789"

id := make([]byte, numChars)
for i := 0; i < numChars; i++ {
id[i] = charset[int(wyhash.Rng(seed))%len(charset)]
}
return string(id)
}

// Benchmark the Add function
func BenchmarkCuckooTraceChecker_Add(b *testing.B) {
traceIDs := make([]string, b.N)
for i := 0; i < b.N; i++ {
traceIDs[i] = genID(32)
}

c := NewCuckooTraceChecker(1000000, &metrics.NullMetrics{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Add(traceIDs[i])
}
}

func BenchmarkCuckooTraceChecker_AddParallel(b *testing.B) {
traceIDs := make([]string, b.N)
for i := 0; i < b.N; i++ {
traceIDs[i] = genID(32)
}
const numGoroutines = 70

p := pool.New().WithMaxGoroutines(numGoroutines + 1)
stop := make(chan struct{})
p.Go(func() {
select {
case <-stop:
return
default:
rand.Intn(100)
}
})

c := NewCuckooTraceChecker(1000000, &metrics.NullMetrics{})
ch := make(chan int, numGoroutines)
for i := 0; i < numGoroutines; i++ {
p.Go(func() {
for n := range ch {
if i%10000 == 0 {
c.Maintain()
}
c.Add(traceIDs[n])
}
})
}
b.ResetTimer()
for j := 0; j < b.N; j++ {
ch <- j
if j%1000 == 0 {
// just give things a moment to run
time.Sleep(1 * time.Microsecond)
}
}
close(ch)
close(stop)
p.Wait()
}

func BenchmarkCuckooTraceChecker_Check(b *testing.B) {
fmt.Println(b.N)
traceIDs := make([]string, b.N)
for i := 0; i < b.N; i++ {
traceIDs[i] = genID(32)
}

c := NewCuckooTraceChecker(1000000, &metrics.NullMetrics{})
// add every other one to the filter
for i := 0; i < b.N; i += 2 {
if i%10000 == 0 {
c.Maintain()
}
c.Add(traceIDs[i])
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Check(traceIDs[i])
}
}

func BenchmarkCuckooTraceChecker_CheckParallel(b *testing.B) {
fmt.Println(b.N)
traceIDs := make([]string, b.N)
for i := 0; i < b.N; i++ {
traceIDs[i] = genID(32)
}

c := NewCuckooTraceChecker(1000000, &metrics.NullMetrics{})
for i := 0; i < b.N; i += 2 {
if i%10000 == 0 {
c.Maintain()
}
c.Add(traceIDs[i])
}

const numGoroutines = 70

p := pool.New().WithMaxGoroutines(numGoroutines + 1)
stop := make(chan struct{})
p.Go(func() {
n := 0
select {
case <-stop:
return
default:
if n&1 == 0 {
c.Add(traceIDs[n])
}
n++
if n >= b.N {
n = 0
}
}
})

ch := make(chan int, numGoroutines)
for i := 0; i < numGoroutines; i++ {
p.Go(func() {
for n := range ch {
c.Check(traceIDs[n])
}
})
}
b.ResetTimer()
for j := 0; j < b.N; j++ {
ch <- j
}
close(ch)
close(stop)
p.Wait()
}