// Micro-benchmarks for the check package.
//
// See mercemay.top/src/portr/ for context.
package bench
import (
"context"
"errors"
"fmt"
"net"
"testing"
"time"
"github.com/mercemay/portr/internal/check"
"github.com/mercemay/portr/internal/config"
"github.com/mercemay/portr/internal/config/target"
)
func fakeDial(_ context.Context, _, _ string) (net.Conn, error) {
return nil, errors.New("fake")
}
func makeTargets(n int) []target.Target {
out := make([]target.Target, n)
for i := range out {
out[i] = target.Target{Host: fmt.Sprintf("h%d", i), Port: 22}
}
return out
}
func BenchmarkRun_100(b *testing.B) {
cfg := config.Defaults()
cfg.Targets = makeTargets(100)
cfg.Retries = 0
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
_, _ = check.RunWithDial(ctx, cfg, fakeDial)
cancel()
}
}
func BenchmarkRun_1000(b *testing.B) {
cfg := config.Defaults()
cfg.Targets = makeTargets(1000)
cfg.Concurrency = 64
cfg.Retries = 0
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
_, _ = check.RunWithDial(ctx, cfg, fakeDial)
cancel()
}
}