From cf2dc4225fa1f9f03799ee0786ccc049490b35a0 Mon Sep 17 00:00:00 2001 From: matteo Date: Mon, 20 Apr 2026 01:05:30 +0800 Subject: [PATCH] ui --- internal/biz/proxy.go | 59 +++++++++++++++++++++---------------- internal/service/handler.go | 40 +++++++++++++++++-------- web/static/i18n/en.json | 16 ++++++++++ web/static/i18n/zh.json | 16 ++++++++++ web/template/index.html | 36 +++++++++++----------- 5 files changed, 112 insertions(+), 55 deletions(-) diff --git a/internal/biz/proxy.go b/internal/biz/proxy.go index ecae329..4757f19 100644 --- a/internal/biz/proxy.go +++ b/internal/biz/proxy.go @@ -8,7 +8,9 @@ import ( "net/http" "net/http/httputil" "net/url" + "strconv" "strings" + "sync" ) type pp struct { @@ -24,7 +26,9 @@ type cp struct { type px struct { } -type replaceBody func(body string, currPath string) string +type replaceBody func(body []byte, currPath string) []byte + +var proxyBufPool = sync.Pool{New: func() any { return new(bytes.Buffer) }} type Uri struct { Path string Dir string @@ -54,21 +58,23 @@ func (uc *Usecase) Proxy(u Uri, rw http.ResponseWriter, req *http.Request) (err } proxy := &httputil.ReverseProxy{Director: director} proxy.ModifyResponse = func(res *http.Response) error { - if res.StatusCode == 200 { - oldPayload, err := io.ReadAll(res.Body) - if err != nil { - return err - } - newPayLoadStr := string(oldPayload) - newPayLoadStr = u.ReBody(newPayLoadStr, currPath) - newPayLoad := []byte(newPayLoadStr) - res.Body = io.NopCloser(bytes.NewBuffer(newPayLoad)) - res.ContentLength = int64(len(newPayLoad)) - res.Header.Set("Content-Length", fmt.Sprint(len(newPayLoad))) - return nil - } else { + if res.StatusCode != 200 { return nil } + buf := proxyBufPool.Get().(*bytes.Buffer) + buf.Reset() + if _, err := buf.ReadFrom(res.Body); err != nil { + proxyBufPool.Put(buf) + return err + } + transformed := u.ReBody(buf.Bytes(), currPath) + result := make([]byte, len(transformed)) + copy(result, transformed) + proxyBufPool.Put(buf) + res.Body = io.NopCloser(bytes.NewReader(result)) + res.ContentLength = int64(len(result)) + res.Header.Set("Content-Length", strconv.Itoa(len(result))) + return nil } proxy.ServeHTTP(rw, req) return nil @@ -101,19 +107,22 @@ func (uc *Usecase) ProxyDiff(u Uri, rw http.ResponseWriter, req *http.Request) ( } proxy := &httputil.ReverseProxy{Director: director} proxy.ModifyResponse = func(res *http.Response) error { - if res.StatusCode == 200 { - oldPayload, err := io.ReadAll(res.Body) - if err != nil { - return err - } - newPayLoadStr := string(oldPayload) - newPayLoadStr = u.ReBody(newPayLoadStr, currPath) - newPayLoad := []byte(newPayLoadStr) - res.Body = io.NopCloser(bytes.NewBuffer(newPayLoad)) - res.ContentLength = int64(len(newPayLoad)) - res.Header.Set("Content-Length", fmt.Sprint(len(newPayLoad))) + if res.StatusCode != 200 { return nil } + buf := proxyBufPool.Get().(*bytes.Buffer) + buf.Reset() + if _, err := buf.ReadFrom(res.Body); err != nil { + proxyBufPool.Put(buf) + return err + } + transformed := u.ReBody(buf.Bytes(), currPath) + result := make([]byte, len(transformed)) + copy(result, transformed) + proxyBufPool.Put(buf) + res.Body = io.NopCloser(bytes.NewReader(result)) + res.ContentLength = int64(len(result)) + res.Header.Set("Content-Length", strconv.Itoa(len(result))) return nil } proxy.ServeHTTP(rw, req) diff --git a/internal/service/handler.go b/internal/service/handler.go index e85f2a9..d79bc09 100644 --- a/internal/service/handler.go +++ b/internal/service/handler.go @@ -165,6 +165,21 @@ func injectPlugins(body string) string { return body } +var pluginLoaderTagBytes = []byte(pluginLoaderTag) +var headCloseTag = []byte("") + +func injectPluginsBytes(body []byte) []byte { + idx := bytes.Index(body, headCloseTag) + if idx < 0 { + return body + } + out := make([]byte, 0, len(body)+len(pluginLoaderTagBytes)) + out = append(out, body[:idx]...) + out = append(out, pluginLoaderTagBytes...) + out = append(out, body[idx:]...) + return out +} + func newUri(c *gin.Context) biz.Uri { return biz.Uri{ Path: c.Request.URL.Path, @@ -177,9 +192,9 @@ func (s *Service) PprofProxy(c *gin.Context) { u := newUri(c) u.Route = RoutePprof u.ProxyBasePath = "/ui/" - u.ReBody = func(body string, currPath string) string { - body = strings.ReplaceAll(body, "href=\"./", "href=\""+currPath) - body = injectPlugins(body) + u.ReBody = func(body []byte, currPath string) []byte { + body = bytes.ReplaceAll(body, []byte(`href="./`), []byte(`href="`+currPath)) + body = injectPluginsBytes(body) return body } s.log.Debugf("%#v", u) @@ -194,9 +209,9 @@ func (s *Service) PprofDiffProxy(c *gin.Context) { u.Base = c.Param("base") u.Route = RoutePprofDiff u.ProxyBasePath = "/ui/" - u.ReBody = func(body string, currPath string) string { - body = strings.ReplaceAll(body, "href=\"./", "href=\""+currPath) - body = injectPlugins(body) + u.ReBody = func(body []byte, currPath string) []byte { + body = bytes.ReplaceAll(body, []byte(`href="./`), []byte(`href="`+currPath)) + body = injectPluginsBytes(body) return body } s.log.Debugf("%#v", u) @@ -210,12 +225,13 @@ func (s *Service) TraceProxy(c *gin.Context) { u := newUri(c) u.Route = RouteTrace u.ProxyBasePath = "/" - u.ReBody = func(body string, currPath string) string { - body = strings.ReplaceAll(body, "href=\"/", "href=\""+currPath) - body = strings.ReplaceAll(body, "src=\"/", "src=\""+currPath) - body = strings.ReplaceAll(body, "action=\"/", "action=\""+currPath) - body = strings.ReplaceAll(body, "getJSON('/", "getJSON('"+currPath) - body = strings.ReplaceAll(body, "url = '/", "url = '"+currPath) + u.ReBody = func(body []byte, currPath string) []byte { + cp := []byte(currPath) + body = bytes.ReplaceAll(body, []byte(`href="/`), append([]byte(`href="`), cp...)) + body = bytes.ReplaceAll(body, []byte(`src="/`), append([]byte(`src="`), cp...)) + body = bytes.ReplaceAll(body, []byte(`action="/`), append([]byte(`action="`), cp...)) + body = bytes.ReplaceAll(body, []byte("getJSON('/"), append([]byte("getJSON('"), cp...)) + body = bytes.ReplaceAll(body, []byte("url = '/"), append([]byte("url = '"), cp...)) return body } s.log.Debugf("%#v", u) diff --git a/web/static/i18n/en.json b/web/static/i18n/en.json index a2b9117..d0befad 100644 --- a/web/static/i18n/en.json +++ b/web/static/i18n/en.json @@ -53,25 +53,41 @@ "goroutine.modeLabel": "Output Mode", "goroutine.modePprof": "pprof format", + "goroutine.modePprofDesc": "— goroutine (flame graph)", "goroutine.modeText": "Text summary", + "goroutine.modeTextDesc": "— goroutine?debug=1 (total + stack distribution)", "goroutine.modeFull": "Full dump", + "goroutine.modeFullDesc": "— goroutine?debug=2", "goroutine.stwWarn": "⚠ Triggers Stop-The-World, use with caution for large goroutine counts", "heap.modeLabel": "Collection Mode", "heap.snapshot": "Snapshot", + "heap.snapshotDesc": "— heap (current heap)", "heap.gcSnapshot": "GC + Snapshot", + "heap.gcSnapshotDesc": "— heap?gc=1 (live objects only, more accurate)", "heap.delta": "Delta", + "heap.deltaDesc": "s — heap?seconds=N", "allocs.modeLabel": "Collection Mode", "allocs.snapshot": "Snapshot", + "allocs.snapshotDesc": "— allocs (cumulative allocations)", "allocs.delta": "Delta", + "allocs.deltaDesc": "s — allocs?seconds=N", "profile.durationLabel": "Duration", + "profile.durationDesc": "s → profile?seconds=N", "trace.durationLabel": "Duration", + "trace.durationDesc": "s → trace?seconds=N", "trace.hint": "Note: view result with go tool trace, not pprof", "block.prereqWarn": "⚠️ Prerequisite: target must call runtime.SetBlockProfileRate(rate), otherwise empty result", + "block.snapshot": "Snapshot → block", + "block.delta": "Delta", + "block.deltaDesc": "s → block?seconds=N", "mutex.prereqWarn": "⚠️ Prerequisite: target must call runtime.SetMutexProfileFraction(rate), otherwise empty result", + "mutex.snapshot": "Snapshot → mutex", + "mutex.delta": "Delta", + "mutex.deltaDesc": "s → mutex?seconds=N", "threadcreate.hint": "Snapshot → threadcreate (OS thread creation stacks, usually small)", "cmdline.hint": "No params, returns command-line arguments", diff --git a/web/static/i18n/zh.json b/web/static/i18n/zh.json index 6681f12..2df9a7e 100644 --- a/web/static/i18n/zh.json +++ b/web/static/i18n/zh.json @@ -53,25 +53,41 @@ "goroutine.modeLabel": "输出模式", "goroutine.modePprof": "pprof 格式", + "goroutine.modePprofDesc": "— goroutine(flame graph)", "goroutine.modeText": "文本摘要", + "goroutine.modeTextDesc": "— goroutine?debug=1(总数 + 栈分布)", "goroutine.modeFull": "完整 dump", + "goroutine.modeFullDesc": "— goroutine?debug=2", "goroutine.stwWarn": "⚠ 会触发 Stop-The-World,goroutine 数量大时请谨慎", "heap.modeLabel": "采集模式", "heap.snapshot": "快照", + "heap.snapshotDesc": "— heap(当前堆内存)", "heap.gcSnapshot": "GC 后快照", + "heap.gcSnapshotDesc": "— heap?gc=1(真实存活对象,更准确)", "heap.delta": "Delta", + "heap.deltaDesc": "秒 — heap?seconds=N", "allocs.modeLabel": "采集模式", "allocs.snapshot": "快照", + "allocs.snapshotDesc": "— allocs(历史累计分配)", "allocs.delta": "Delta", + "allocs.deltaDesc": "秒 — allocs?seconds=N", "profile.durationLabel": "采集时长", + "profile.durationDesc": "秒 → profile?seconds=N", "trace.durationLabel": "采集时长", + "trace.durationDesc": "秒 → trace?seconds=N", "trace.hint": "提示:结果用 go tool trace 查看,不同于 pprof", "block.prereqWarn": "⚠️ 前置条件:目标程序需调用 runtime.SetBlockProfileRate(rate),否则采集结果为空文件", + "block.snapshot": "快照 → block", + "block.delta": "Delta", + "block.deltaDesc": "秒 → block?seconds=N", "mutex.prereqWarn": "⚠️ 前置条件:目标程序需调用 runtime.SetMutexProfileFraction(rate),否则采集结果为空文件", + "mutex.snapshot": "快照 → mutex", + "mutex.delta": "Delta", + "mutex.deltaDesc": "秒 → mutex?seconds=N", "threadcreate.hint": "快照 → threadcreate(OS 线程创建调用栈,通常较小)", "cmdline.hint": "无参数,直接采集命令行参数", diff --git a/web/template/index.html b/web/template/index.html index f361a39..247b8b3 100644 --- a/web/template/index.html +++ b/web/template/index.html @@ -782,75 +782,75 @@ goroutine: [ '
' + t('goroutine.modeLabel') + '
', '
', - '
', + '', '
', - '
', + '', '
', - '
', + '', '' ].join(""), heap: [ '
' + t('heap.modeLabel') + '
', '
', - '
', + '', '
', - '
', + '', '
', '', '', '', - '秒 — heap?seconds=N
' + '' + t('heap.deltaDesc') + '' ].join(""), allocs: [ '
' + t('allocs.modeLabel') + '
', '
', - '
', + '', '
', '', '', '', - '秒 — allocs?seconds=N
' + '' + t('allocs.deltaDesc') + '' ].join(""), profile: [ '
' + t('profile.durationLabel') + '
', '
', '', - '秒 → profile?seconds=N
' + '' + t('profile.durationDesc') + '' ].join(""), trace: [ '
' + t('trace.durationLabel') + '
', '
', '', - '秒 → trace?seconds=N
', + '' + t('trace.durationDesc') + '', '
' + t('trace.hint') + '
' ].join(""), block: [ - '
' + t('block.prereqWarn') + ' runtime.SetBlockProfileRate(rate)
', + '
' + t('block.prereqWarn') + '
', '
' + t('heap.modeLabel') + '
', '
', - '
', + '', '
', '', - '', + '', '', - '秒 → block?seconds=N
' + '' + t('block.deltaDesc') + '' ].join(""), mutex: [ - '
' + t('mutex.prereqWarn') + ' runtime.SetMutexProfileFraction(rate)
', + '
' + t('mutex.prereqWarn') + '
', '
' + t('heap.modeLabel') + '
', '
', - '
', + '', '
', '', - '', + '', '', - '秒 → mutex?seconds=N
' + '' + t('mutex.deltaDesc') + '' ].join(""), threadcreate: '
' + t('threadcreate.hint') + '
',