A user on X (FatimaHill74421) reports the macOS app at 3.95 GB physical
footprint after ~12h (v0.1.1273, after 8a1b5916f's #2278 fix), and a macOS
`sample` shows Go's frames as ???. `magpie heap [<file>]` asks the magpie
that serves the gateway for GET /v1/magpie/heap: runtime/pprof's heap
profile after a GC (gzipped protobuf, go tool pprof reads it), with
HeapAlloc, live heap, HeapInuse, HeapSys, StackSys, Sys, NumGC and the
goroutine count in X-Magpie-Heap-Stats. It writes <file>, else
magpie-heap-<date>-<time>.pb.gz here (0600, never over a file), and prints
the path, the pid and the numbers. --help writes nothing; an unknown flag,
"-" or a second file is refused before anything is asked.
The route answers only a loopback, unproxied caller with no browser marks
calling localhost or a loopback IP. heapRefused checks this in the handler
itself, so a gateway key never lets in another machine, a listed page,
Origin null, a localhost page or a rebound Host, and it holds where Handler
is served without lanGuard.
Run at dccfefdf7: gofmt -l (nothing); go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./... (clean); go build for darwin,
linux (nogui) and windows; go test -tags nogui -timeout 45m ./... under a
sandbox HOME: every package ok (gateway 1218s, cli 613s). Broken by hand:
heapRefused returning "" turns TestHeapProfileOnlyToThisComputer red
(19 failures).
Real use: the built binary's `magpie serve` under a sandbox HOME on
127.0.0.1:3481; `magpie heap` saved a 10 KB profile (0600) and printed the
numbers, go tool pprof -top read it, --help wrote nothing, and a curl with
Origin: null got 403. Not tried: the GUI app's own process, or the
Windows box.
139 lines
4.1 KiB
Go
139 lines
4.1 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"
|
|
)
|
|
|
|
// heapDir is a home of its own and a working folder in it, the one magpie
|
|
// heap writes its file to.
|
|
func heapDir(t *testing.T) (root, dir string) {
|
|
t.Helper()
|
|
root = helpHome(t)
|
|
dir = filepath.Join(root, "work")
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Chdir(dir)
|
|
return root, dir
|
|
}
|
|
|
|
// --help and its other forms print magpie heap's usage, ask nothing and
|
|
// write nothing, here or under the home; a flag it doesn't take, "-", or
|
|
// a second file are refused the same way, before the gateway is asked.
|
|
func TestHeapHelpAndBadArgsWriteNothing(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{{"heap", "--help"}, {"heap", "-h"}, {"heap", "help"}, {"heap", "out.pb.gz", "--help"}} {
|
|
out, err := runOut(t, args...)
|
|
if err != nil || !strings.Contains(out, "usage: magpie heap [<file>]") {
|
|
t.Errorf("magpie %s: %v %q", strings.Join(args, " "), err, out)
|
|
}
|
|
}
|
|
for _, args := range [][]string{{"heap", "--out", "x"}, {"heap", "-o"}, {"heap", "-"}, {"heap", "a.pb.gz", "b.pb.gz"}, {"heap", " "}} {
|
|
if _, err := runOut(t, args...); err == nil || !strings.Contains(err.Error(), "nothing was written") {
|
|
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)
|
|
}
|
|
}
|
|
|
|
// Against a running gateway (its Handler, on loopback), magpie heap
|
|
// writes the gzipped profile into the working folder, prints its path and
|
|
// the memory numbers, and never writes over a file there.
|
|
func TestHeapWritesTheRunningGatewaysProfile(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, "heap", "mem.pb.gz")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file := filepath.Join(dir, "mem.pb.gz")
|
|
b, err := os.ReadFile(file)
|
|
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("inuse_space")) {
|
|
t.Fatalf("not a heap profile: %v", err)
|
|
}
|
|
if info, _ := os.Stat(file); info.Mode().Perm() != 0o600 && os.PathSeparator == '/' {
|
|
t.Errorf("mode %v", info.Mode())
|
|
}
|
|
for _, want := range []string{"mem.pb.gz", "HeapAlloc", "HeapInuse", "HeapSys", "Sys ", "NumGC", "goroutines", "process"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("printed no %s: %q", want, out)
|
|
}
|
|
}
|
|
|
|
// the same name again: refused, the first file kept
|
|
if _, err := runOut(t, "heap", "mem.pb.gz"); err == nil || !strings.Contains(err.Error(), "there already") {
|
|
t.Fatalf("written over: %v", err)
|
|
}
|
|
if again, _ := os.ReadFile(file); !bytes.Equal(again, b) {
|
|
t.Fatal("the first profile changed")
|
|
}
|
|
|
|
// no name: magpie-heap-<date>-<time>.pb.gz here
|
|
if _, err := runOut(t, "heap"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m, _ := filepath.Glob(filepath.Join(dir, "magpie-heap-*.pb.gz")); len(m) != 1 {
|
|
t.Fatalf("no default file: %v", m)
|
|
}
|
|
}
|
|
|
|
// With no magpie answering, magpie heap says so and writes nothing.
|
|
func TestHeapWithNoGateway(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())
|
|
if _, err := runOut(t, "heap", "x.pb.gz"); err == nil || !strings.Contains(err.Error(), "is magpie running") {
|
|
t.Fatalf("%v", err)
|
|
}
|
|
if ents, _ := os.ReadDir(dir); len(ents) != 0 {
|
|
t.Fatalf("written: %v", ents)
|
|
}
|
|
}
|