Files
yetone 89521b03d6 cli, gateway: magpie cpu records the running magpie's CPU profile for 30s, for a report of high CPU (@LeiCheng42 on X)
@LeiCheng42 on X reports magpie using the most CPU while Codex runs. As
with magpie heap (e085d6e92), Activity Monitor and `sample` show Go's
frames as ???, so `magpie cpu [<file>] [--seconds N]` asks the magpie
serving the gateway for GET /v1/magpie/cpu?seconds=N: runtime/pprof's CPU
profile over 30s (5..120; else 400), gzipped protobuf, with the pid,
version, uptime, CPU time used over the window (getrusage / GetProcessTimes)
and since start, cores, GOMAXPROCS and goroutines in X-Magpie-CPU-Stats. It
tells the user to use magpie as when busy (run Codex) while it records,
writes <file> else magpie-cpu-<date>-<time>.pb.gz (0600), refuses a file
that is there before recording, and says "is magpie running?" at once when
nothing answers. --help writes nothing; an unknown flag, "-", a second file
or --seconds out of range are refused before anything is asked.

Only the profiling request waits: the gateway serves everything else
meanwhile. pprof keeps one CPU profile at a time, so a second ask gets 409
(cpuRecording), and a caller that hangs up stops the recording. The route
takes heap's guard: heapRefused's checks move into profileRefused, shared
by cpuRefused, so only a loopback, unproxied, browserless call to a
loopback Host gets it, with or without a gateway key or lanGuard.

Run at bad4508c4 + this: gofmt -l (nothing); go vet ./..., GOOS=linux go
vet -tags nogui ./..., GOOS=windows go vet ./... (clean); go test -tags
nogui -timeout 45m ./... under a sandbox HOME: every package ok except
internal/agent (45m timeout, #2544; again alone, in TestPortMovesEveryAgent)
and internal/gateway (TestTranslatedStreamKeepsClientAlive/chat_to_responses,
sibling of #2169/#2711, filed as #2737; alone with -timeout 45m only
/chat_to_anthropic, #2169; the test passes 5/5 alone). TestCPU* -race
-count=3 ok. Broken by hand: cpuRefused returning "" (19 failures in
TestCPUProfileOnlyToThisComputer); no one-at-a-time guard and no hang-up
stop (TestCPUProfileOneAtATime red each); no seconds bounds, no pre-check
of the file, no is-it-running check, no help case (each CLI test red).
Rebased on 2042123f4: go build ./..., gofmt -l, GOOS=windows and
GOOS=linux vet of internal/gateway and internal/cli clean; go test -run
'CPU|Cpu|Heap|Help' ./internal/cli ./internal/gateway ok.

Real use: the built binary's `magpie serve` under a sandbox HOME on
127.0.0.1:3493; `magpie cpu --seconds 5` saved a 0600 profile that go tool
pprof -top reads and printed the numbers; a concurrent second one got the
409 while GET / answered in ms; Origin: null and a rebound Host got 403;
--help wrote nothing; an existing file was refused. Not tried: the GUI
app's process, or the Windows box.
2026-10-17 20:52:13 +08:00

167 lines
5.3 KiB
Go

package cli
import (
"bytes"
"compress/gzip"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
)
// --help and its other forms print magpie cpu's usage, ask nothing and
// write nothing, here or under the home; a flag it doesn't take, "-", a
// second file, or --seconds out of 5..120 are refused the same way,
// before the gateway is asked.
func TestCPUHelpAndBadArgsWriteNothing(t *testing.T) {
root, _ := heapDir(t)
asked := false
gw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { asked = true }))
defer gw.Close()
t.Setenv("MAGPIE_ADDR", strings.TrimPrefix(gw.URL, "http://"))
before := tree(t, root)
for _, args := range [][]string{{"cpu", "--help"}, {"cpu", "-h"}, {"cpu", "help"}, {"cpu", "out.pb.gz", "--help"}, {"cpu", "--seconds", "10", "-h"}} {
out, err := runOut(t, args...)
if err != nil || !strings.Contains(out, "usage: magpie cpu [<file>] [--seconds N]") {
t.Errorf("magpie %s: %v %q", strings.Join(args, " "), err, out)
}
}
for _, args := range [][]string{
{"cpu", "--out", "x"}, {"cpu", "-o"}, {"cpu", "-s", "10"}, {"cpu", "-"}, {"cpu", "a.pb.gz", "b.pb.gz"}, {"cpu", " "},
{"cpu", "--seconds"}, {"cpu", "--seconds", "4"}, {"cpu", "--seconds", "121"}, {"cpu", "--seconds=0"}, {"cpu", "--seconds", "x"},
{"cpu", "--seconds", "30s"}, {"cpu", "--seconds=-5"}, {"cpu", "--seconds", "10", "--seconds", "20"}, {"cpu", "x.pb.gz", "--seconds"},
} {
if _, err := runOut(t, args...); err == nil || !strings.Contains(err.Error(), "nothing was recorded") {
t.Errorf("magpie %s: %v", strings.Join(args, " "), err)
}
}
if asked {
t.Error("the gateway was asked")
}
if got := tree(t, root); !reflect.DeepEqual(got, before) {
t.Fatalf("written:\nbefore %v\nafter %v", before, got)
}
}
// --seconds N and --seconds=N are read, in any place among the file.
func TestCPUArgs(t *testing.T) {
for _, c := range []struct {
args []string
file string
secs int
}{
{nil, "", gateway.CPUSeconds},
{[]string{"a.pb.gz"}, "a.pb.gz", 30},
{[]string{"--seconds", "5"}, "", 5},
{[]string{"--seconds=120", "a.pb.gz"}, "a.pb.gz", 120},
{[]string{"a.pb.gz", "--seconds", "60"}, "a.pb.gz", 60},
} {
file, secs, err := cpuArgs(c.args)
if err != nil || file != c.file || secs != c.secs {
t.Errorf("%q: %q %d %v", c.args, file, secs, err)
}
}
}
// A file that is there already is refused before anything is recorded,
// and is kept as it was.
func TestCPUKeepsAFileThere(t *testing.T) {
_, dir := heapDir(t)
asked := false
gw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { asked = true }))
defer gw.Close()
t.Setenv("MAGPIE_ADDR", strings.TrimPrefix(gw.URL, "http://"))
file := filepath.Join(dir, "mine.pb.gz")
if err := os.WriteFile(file, []byte("mine"), 0o644); err != nil {
t.Fatal(err)
}
if _, err := runOut(t, "cpu", "mine.pb.gz"); err == nil || !strings.Contains(err.Error(), "there already") {
t.Fatalf("written over: %v", err)
}
if b, _ := os.ReadFile(file); string(b) != "mine" {
t.Fatalf("the file changed: %q", b)
}
if asked {
t.Error("the gateway was asked")
}
}
// Against a running gateway (its Handler, on loopback), magpie cpu says
// it records and for how long, then writes the gzipped profile into the
// working folder and prints its path, the CPU used and the goroutines.
// It records the 5 seconds asked for.
func TestCPUWritesTheRunningGatewaysProfile(t *testing.T) {
_, dir := heapDir(t)
gw := httptest.NewServer(gateway.New().Handler())
defer gw.Close()
t.Setenv("MAGPIE_ADDR", strings.TrimPrefix(gw.URL, "http://"))
out, err := runOut(t, "cpu", "--seconds", "5")
if err != nil {
t.Fatal(err)
}
m, _ := filepath.Glob(filepath.Join(dir, "magpie-cpu-*.pb.gz"))
if len(m) != 1 {
t.Fatalf("no default file: %v", m)
}
b, err := os.ReadFile(m[0])
if err != nil {
t.Fatal(err)
}
zr, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}
if raw, err := io.ReadAll(zr); err != nil || !bytes.Contains(raw, []byte("samples")) || !bytes.Contains(raw, []byte("nanoseconds")) {
t.Fatalf("not a CPU profile: %v", err)
}
if info, _ := os.Stat(m[0]); info.Mode().Perm() != 0o600 && os.PathSeparator == '/' {
t.Errorf("mode %v", info.Mode())
}
for _, want := range []string{"Recording", "for 5s", "run Codex", filepath.Base(m[0]), "process", "CPU ", "of one core", "goroutines"} {
if !strings.Contains(out, want) {
t.Errorf("printed no %s: %q", want, out)
}
}
}
// With no magpie answering, magpie cpu says so at once and writes
// nothing.
func TestCPUWithNoGateway(t *testing.T) {
_, dir := heapDir(t)
// a port kept by the test that hangs up on every call: one freed
// could be handed to anything next
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
c.Close()
}
}()
t.Setenv("MAGPIE_ADDR", ln.Addr().String())
out, err := runOut(t, "cpu", "x.pb.gz")
if err == nil || !strings.Contains(err.Error(), "is magpie running") {
t.Fatalf("%v", err)
}
if strings.Contains(out, "Recording") {
t.Errorf("said it records: %q", out)
}
if ents, _ := os.ReadDir(dir); len(ents) != 0 {
t.Fatalf("written: %v", ents)
}
}