Files
zip/verify_bench_test.go
zooqueenandHanzo Dev 0803549253 Start a plugin with no Sum from its own file, without copying it
openVerified copied every plugin binary into the instance's private
directory before exec, digest or not. The copy is what makes "what was
checked" and "what runs" one object, and it stays for a plugin with a
Sum. Without one nothing is checked, so the copy kept nothing equal:
the bytes at the path when it was taken are the bytes that ran. It cost
a full write of the binary on every lazy start (Hanzo's per-app
plugins are 24-145 MB), on whatever disk Dir sits on, and gave every
instance its own page cache.

A plugin with no Sum is now opened read-only and executed through that
descriptor. An embedded Bin is written once, under ForkLock (place),
and executed from there; before, it was written and then copied.

Benchmark_OpenBinary (the file handling alone, Apple M4 Max, APFS,
go1.27.1, -count=6, median):

  size     before     after
  16 MB    3.94 ms    0.026 ms
  48 MB   12.11 ms    0.026 ms
  144 MB  36.66 ms    0.026 ms

Benchmark_ColdStart (Load lazy, first request through the host, test
plugin padded to size, same host). On macOS the delta is far larger
than the copy itself: exec of a just-written file costs it more than
the bytes do. Expect the Linux gain to be the copy above, not this:

  binary     before      after
  15.8 MB   156.8 ms    7.8 ms
  47.8 MB   294.8 ms    7.1 ms
  143.8 MB  767.2 ms    7.3 ms

Co-authored-by: Hanzo Dev <[email protected]>
2026-10-15 05:43:43 -07:00

42 lines
1.0 KiB
Go

package zip
import (
"fmt"
"os"
"path/filepath"
"testing"
)
// Benchmark_OpenBinary prices what a start does with the plugin's file before
// it execs it, at the sizes a fleet ships (Hanzo's per-app plugins: 24-145 MB),
// for a plugin with no declared Sum — the shape every on-disk plugin takes.
//
// go test -run='^$' -bench=Benchmark_OpenBinary -count=6 .
func Benchmark_OpenBinary(b *testing.B) {
for _, mb := range []int{16, 48, 144} {
src := filepath.Join(b.TempDir(), "svc")
if err := os.WriteFile(src, make([]byte, mb<<20), 0o700); err != nil {
b.Fatal(err)
}
b.Run(fmt.Sprintf("size=%dMB", mb), func(b *testing.B) {
b.SetBytes(int64(mb) << 20)
for i := 0; i < b.N; i++ {
b.StopTimer()
priv, err := os.MkdirTemp(filepath.Dir(src), "zip-")
if err != nil {
b.Fatal(err)
}
b.StartTimer()
bin, err := openVerified(src, priv, "svc", "")
if err != nil {
b.Fatal(err)
}
b.StopTimer()
_ = bin.Close()
_ = os.RemoveAll(priv)
b.StartTimer()
}
})
}
}