Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a worker-specific random source to remove lock contention. (#178)
Use a worker-specific random source to remove lock contention.
- Loading branch information
1 parent
8a12112
commit f6b3e86
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package statsd | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShouldSample(t *testing.T) { | ||
rates := []float64{0.01, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99, 1.0} | ||
iterations := 50_000 | ||
|
||
for _, rate := range rates { | ||
rate := rate // Capture range variable. | ||
t.Run(fmt.Sprintf("Rate %0.2f", rate), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
worker := newWorker(newBufferPool(1, 1, 1), nil) | ||
count := 0 | ||
for i := 0; i < iterations; i++ { | ||
if worker.shouldSample(rate) { | ||
count++ | ||
} | ||
} | ||
assert.InDelta(t, rate, float64(count)/float64(iterations), 0.01) | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkShouldSample(b *testing.B) { | ||
b.RunParallel(func(pb *testing.PB) { | ||
worker := newWorker(newBufferPool(1, 1, 1), nil) | ||
for pb.Next() { | ||
worker.shouldSample(0.1) | ||
} | ||
}) | ||
} |