internal/bench/output_bench_test.go

// Micro-benchmarks for the output writers.
//
// See mercemay.top/src/portr/ for context.
package bench

import (
	"io"
	"testing"
	"time"

	"github.com/mercemay/portr/internal/check"
	"github.com/mercemay/portr/internal/check/result"
	"github.com/mercemay/portr/internal/config/target"
	"github.com/mercemay/portr/internal/output/json"
	"github.com/mercemay/portr/internal/output/table"
)

func bigReport(n int) check.Report {
	rs := make([]result.Result, n)
	for i := range rs {
		rs[i] = result.Success(target.Target{Host: "h", Port: i + 1}, 3*time.Millisecond, 1)
	}
	return check.Report{Results: rs}
}

func BenchmarkTableWrite(b *testing.B) {
	rep := bigReport(500)
	w := table.New()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = w.Write(io.Discard, rep)
	}
}

func BenchmarkJSONWrite(b *testing.B) {
	rep := bigReport(500)
	w := json.New(false)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = w.Write(io.Discard, rep)
	}
}