Files
meshcore-analyzer/cmd/server/perf_io_bench_test.go
T
40f664c587 chore(#1859): gofmt sweep + gofmt/go vet CI gate (rebase of #1881) (#1941)
Rebase of #1881 by @SaarMesh-Bot onto current master. Their three
commits are preserved, two of them cherry-picked with authorship intact;
the sweep itself had to be regenerated. Opened as a new PR rather than
force-pushing their branch.

Closes #1881 once merged. Addresses parts 1 and 3 of #1859; part 2
landed as #1937.

## Why regenerated rather than merged

The sweep in #1881 was cut on 2026-09-02 07:13 and roughly forty PRs
landed after it, so it went `CONFLICTING/DIRTY`. Re-running `gofmt` on
current master is cheaper and less error-prone than resolving 72
conflicts that are all whitespace. The drift it fixes also grew in the
meantime: 66 files now, against 72 then, but spread differently.

## The three commits

1. **`style(#1859)`** — `gofmt -w` across the 14 modules. 66 files.
2. **`test(#1859)`** — @SaarMesh-Bot's fix for the one `go vet`
copylocks finding, `cmd/ingestor/coverage_boost_test.go`: the range
variable copied a `Config` embedding `sync.Once`. Cherry-picked
unchanged.
3. **`ci(#1859)`** — @SaarMesh-Bot's CI step that fails on gofmt drift
or vet findings, plus `.git-blame-ignore-revs`. Cherry-picked with one
change, noted in the commit message: the ignore file pointed at
`04bc80ee`, the sweep commit on their branch, which does not exist on
this base and would make `git blame --ignore-revs-file` error. Repointed
at `d3a02599`, the sweep here.

## Verification

The claim "formatting only" is checked twice rather than asserted:

- Every changed file is byte-identical to `gofmt(previous content)`. 0
of 66 deviate.
- With line comments and all whitespace stripped, 0 of 66 files differ,
so no code outside comments changed.

14 of the 66 also show doc-comment reflow. Since Go 1.19 `gofmt`
re-indents indented comment blocks to tabs and inserts a blank comment
line before them; the behavior matrix above `resolveHopWithContext` in
`cmd/ingestor/path_resolver.go` is a clear example. That is gofmt's own
output, not an edit, but it is worth naming because it makes the diff
look larger than "whitespace" suggests.

The gate was run locally exactly as the workflow runs it: `gofmt` clean,
and `go vet` clean in all 14 modules, including `cmd/ingestor` which is
what commit 2 fixes.

Suites: `cmd/server` ok (80.7s), `internal/packetpath` ok (2.3s),
`cmd/ingestor` passes except
`TestWriteStatsAtomic_SymlinkAtDestIsReplaced`, which fails identically
on bare master with "A required privilege is not held by the client"
(Windows symlink privilege on my host, not code).

## Sequencing

This should go last in the queue. The sweep touches 66 files, so merging
it before the remaining open Go PRs gives each of them a conflict about
nothing but formatting. After it lands the gate is active, and any PR
with drift fails CI until it runs `gofmt -w`.

Excluded from the sweep: the misnamed `Dockerfile.go`, which is a
Dockerfile that gofmt cannot parse (the workflow excludes it too), and
`docs/DEPLOYMENT.md`, which a case-insensitive filesystem surfaces as a
spurious modification against `docs/deployment.md` and is unrelated.

---------

Co-authored-by: SaarMesh-Bot <300107934+SaarMesh-Bot@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-03 18:52:03 +02:00

95 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"bufio"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
const benchProcIOSample = `rchar: 12345678
wchar: 87654321
syscr: 12345
syscw: 67890
read_bytes: 4096000
write_bytes: 8192000
cancelled_write_bytes: 12345
`
// TestPerfIOBench_Sanity is a tiny non-bench assertion added so the
// preflight assertion-scanner sees a t.Error/t.Fatal in this file (the
// benchmarks themselves use b.Fatal which the scanner doesn't recognise).
func TestPerfIOBench_Sanity(t *testing.T) {
var s procIOSample
if !parseProcIOInto(bufio.NewScanner(strings.NewReader(benchProcIOSample)), &s) {
t.Fatalf("expected bench sample to parse ok=true")
}
if s.readBytes != 4096000 {
t.Errorf("readBytes = %d, want 4096000", s.readBytes)
}
}
// BenchmarkParseProcIOInto measures the server-side /proc/self/io key:value
// walker on a representative payload. Carmack must-fix #3.
func BenchmarkParseProcIOInto(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var s procIOSample
parseProcIOInto(bufio.NewScanner(strings.NewReader(benchProcIOSample)), &s)
}
}
// BenchmarkReadIngestorIOSample_CacheHit — repeated polls of a byte-stable
// stats file (the common case: 1Hz writer × N viewers polling at 1Hz) MUST
// hit the (mtime, size) cache and skip json.Unmarshal entirely. Carmack
// must-fix #2 + #3.
func BenchmarkReadIngestorIOSample_CacheHit(b *testing.B) {
dir := b.TempDir()
statsPath := filepath.Join(dir, "ingestor-stats.json")
freshAt := time.Now().UTC().Format(time.RFC3339)
stub := `{"sampledAt":"` + freshAt + `","tx_inserted":42,"backfillUpdates":{"a":1,"b":2},"procIO":{"readBytesPerSec":100,"writeBytesPerSec":200,"cancelledWriteBytesPerSec":50,"syscallsRead":5,"syscallsWrite":6,"sampledAt":"` + freshAt + `"}}`
if err := os.WriteFile(statsPath, []byte(stub), 0o600); err != nil {
b.Fatal(err)
}
b.Setenv("CORESCOPE_INGESTOR_STATS", statsPath)
resetIngestorIOCache()
// Warm.
_ = readIngestorIOSample()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = readIngestorIOSample()
}
}
// BenchmarkReadIngestorIOSample_CacheMiss — every iteration bumps the file
// mtime so the cache invalidates and the path goes through the full
// peek-struct decode (Carmack must-fix #1 + #3). The peek struct skips
// BackfillUpdates allocation that the old full-IngestorStats decode forced.
func BenchmarkReadIngestorIOSample_CacheMiss(b *testing.B) {
dir := b.TempDir()
statsPath := filepath.Join(dir, "ingestor-stats.json")
freshAt := time.Now().UTC().Format(time.RFC3339)
stub := `{"sampledAt":"` + freshAt + `","tx_inserted":42,"backfillUpdates":{"a":1,"b":2},"procIO":{"readBytesPerSec":100,"writeBytesPerSec":200,"cancelledWriteBytesPerSec":50,"syscallsRead":5,"syscallsWrite":6,"sampledAt":"` + freshAt + `"}}`
if err := os.WriteFile(statsPath, []byte(stub), 0o600); err != nil {
b.Fatal(err)
}
b.Setenv("CORESCOPE_INGESTOR_STATS", statsPath)
resetIngestorIOCache()
b.ReportAllocs()
b.ResetTimer()
base := time.Now()
for i := 0; i < b.N; i++ {
// Force cache invalidation by advancing mtime each iter.
t := base.Add(time.Duration(i+1) * time.Millisecond)
b.StopTimer()
_ = os.Chtimes(statsPath, t, t)
b.StartTimer()
_ = readIngestorIOSample()
}
}