1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-29 15:02:30 +00:00
Files
go-chasquid-smtp/internal/nettrace/bench_test.go
T
Alberto Bertogli 8b19760f4e test: Use modern testing features instead of custom helpers
Many of our tests rely on helpers that predate features which are now
available in the standard library.

This patch replaces them with the standard equivalents:

- MustTempDir/RemoveIfOk -> t.ArtifactDir() or t.TempDir() (depending on
  whether it is useful to keep the data on request).
- GetFreePort + waitForServer -> opening the listeners before starting
  the servers. To support this, localrpc.Server gains a Serve method. We
  may re-introduce these in the future if more use cases appear.
- WaitFor and real-time timing checks -> testing/synctest.
- context.Background() -> t.Context().
- Benchmark b.N loops -> b.Loop().
- reflect.DeepEqual -> cmp.Diff, and proto.Equal for protobufs.
2026-10-03 10:34:35 +01:00

57 lines
1007 B
Go

package nettrace
import (
"testing"
)
// Our benchmark loop is similar to the one from golang.org/x/net/trace, so we
// can compare results.
func runBench(b *testing.B, events int) {
nTraces := (b.N + events + 1) / events
for range nTraces {
tr := New("bench", "test")
for j := range events {
tr.Printf("%d", j)
}
tr.Finish()
}
}
func BenchmarkTrace_2(b *testing.B) {
runBench(b, 2)
}
func BenchmarkTrace_10(b *testing.B) {
runBench(b, 10)
}
func BenchmarkTrace_100(b *testing.B) {
runBench(b, 100)
}
func BenchmarkTrace_1000(b *testing.B) {
runBench(b, 1000)
}
func BenchmarkTrace_10000(b *testing.B) {
runBench(b, 10000)
}
func BenchmarkNewAndFinish(b *testing.B) {
for b.Loop() {
tr := New("bench", "test")
tr.Finish()
}
}
func BenchmarkPrintf(b *testing.B) {
tr := New("bench", "test")
defer tr.Finish()
for b.Loop() {
// Keep this without any formatting, so we measure our code instead of
// the performance of fmt.Sprintf.
tr.Printf("this is printf")
}
}