High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
feat: replace CDP Performance.getMetrics with OS-level memory
Boschafca87c5e602
diff --git a/go.mod b/go.modindex 6cc01d9a..97ab1995 100644--- a/go.mod+++ b/go.mod@@ -11,8 +11,16 @@ require (require (github.com/chromedp/sysutil v1.1.0 // indirect+ github.com/ebitengine/purego v0.10.0 // indirectgithub.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 // indirect+ github.com/go-ole/go-ole v1.2.6 // indirectgithub.com/gobwas/httphead v0.1.0 // indirectgithub.com/gobwas/pool v0.2.1 // indirect- golang.org/x/sys v0.34.0 // indirect+ github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect+ github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect+ github.com/shirou/gopsutil/v4 v4.26.2 // indirect+ github.com/tklauser/go-sysconf v0.3.16 // indirect+ github.com/tklauser/numcpus v0.11.0 // indirect+ github.com/yusufpapurcu/wmi v1.2.4 // indirect+ golang.org/x/sys v0.41.0 // indirect)diff --git a/go.sum b/go.sumindex ba365b33..92913dc6 100644--- a/go.sum+++ b/go.sum@@ -4,21 +4,43 @@ github.com/chromedp/chromedp v0.14.2 h1:r3b/WtwM50RsBZHMUm9fsNhhzRStTHrKdr2zmwbZgithub.com/chromedp/chromedp v0.14.2/go.mod h1:rHzAv60xDE7VNy/MYtTUrYreSc0ujt2O1/C3bzctYBo=github.com/chromedp/sysutil v1.1.0 h1:PUFNv5EcprjqXZD9nJb9b/c9ibAbxiYo4exNWZyipwM=github.com/chromedp/sysutil v1.1.0/go.mod h1:WiThHUdltqCNKGc4gaU50XgYjwjYIhKWoHGPTUfWTJ8=+github.com/ebitengine/purego v0.10.0 h1:QIw4xfpWT6GWTzaW5XEKy3HXoqrJGx1ijYHzTF0/ISU=+github.com/ebitengine/purego v0.10.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 h1:iizUGZ9pEquQS5jTGkh4AqeeHCMbfbjeb0zMt0aEFzs=github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=+github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=+github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=+github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo=github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=+github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=+github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde h1:x0TT0RDC7UhAVbbWWBzr41ElhJx5tXPWkIHA2HWPRuw=github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=+github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=+github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=+github.com/shirou/gopsutil/v4 v4.26.2 h1:X8i6sicvUFih4BmYIGT1m2wwgw2VG9YgrDTi7cIRGUI=+github.com/shirou/gopsutil/v4 v4.26.2/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ=+github.com/tklauser/go-sysconf v0.3.16 h1:frioLaCQSsF5Cy1jgRBrzr6t502KIIwQ0MArYICU0nA=+github.com/tklauser/go-sysconf v0.3.16/go.mod h1:/qNL9xxDhc7tx3HSRsLWNnuzbVfh3e7gh/BmM179nYI=+github.com/tklauser/numcpus v0.11.0 h1:nSTwhKH5e1dMNsCdVBukSZrURJRoHbSEQjdEbY+9RXw=+github.com/tklauser/numcpus v0.11.0/go.mod h1:z+LwcLq54uWZTX0u/bGobaV34u6V7KNlTZejzM6/3MQ=+github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=+github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=+golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=+golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=+golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=+golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=diff --git a/internal/bridge/metrics.go b/internal/bridge/metrics.goindex d5311083..bde4d208 100644--- a/internal/bridge/metrics.go+++ b/internal/bridge/metrics.go@@ -2,129 +2,184 @@ package bridgeimport ("context"+ "log/slog""time"- "github.com/chromedp/cdproto/performance"- "github.com/chromedp/cdproto/target"+ "github.com/chromedp/cdproto/systeminfo""github.com/chromedp/chromedp"+ "github.com/shirou/gopsutil/v4/process")// MemoryMetrics holds Chrome memory statisticstype MemoryMetrics struct {+ // OS-level memory (real RAM usage)+ MemoryMB float64 `json:"memoryMB"` // Total RSS across renderer processes++ // Legacy JS-heap fields (for backward compat, now estimated)JSHeapUsedMB float64 `json:"jsHeapUsedMB"`JSHeapTotalMB float64 `json:"jsHeapTotalMB"`- Documents int64 `json:"documents"`- Frames int64 `json:"frames"`- Nodes int64 `json:"nodes"`- Listeners int64 `json:"listeners"`++ // Process counts+ Renderers int `json:"renderers"` // Number of renderer processes (≈ tabs)++ // Legacy fields (set to 0, kept for API compat)+ Documents int64 `json:"documents"`+ Frames int64 `json:"frames"`+ Nodes int64 `json:"nodes"`+ Listeners int64 `json:"listeners"`}// GetMemoryMetrics retrieves memory metrics for a specific tab+// Now uses OS-level memory instead of per-tab CDP callsfunc (b *Bridge) GetMemoryMetrics(tabID string) (*MemoryMetrics, error) {- ctx, _, err := b.TabContext(tabID)- if err != nil {- return nil, err- }-- return getMetricsFromContext(ctx)+ // For single tab, return aggregated (we can't map tab→PID reliably)+ return b.GetAggregatedMemoryMetrics()}// GetBrowserMemoryMetrics retrieves memory metrics for the entire browserfunc (b *Bridge) GetBrowserMemoryMetrics() (*MemoryMetrics, error) {+ return b.GetAggregatedMemoryMetrics()+}++// GetAggregatedMemoryMetrics returns real OS memory usage across all Chrome processes+// Uses hybrid approach: CDP for process IDs, gopsutil for actual memory+func (b *Bridge) GetAggregatedMemoryMetrics() (*MemoryMetrics, error) {if b.BrowserCtx == nil {return nil, nil}- return getMetricsFromContext(b.BrowserCtx)-}-// GetAggregatedMemoryMetrics returns summed memory metrics across all open tabs-func (b *Bridge) GetAggregatedMemoryMetrics() (*MemoryMetrics, error) {- targets, err := b.ListTargets()+ result := &MemoryMetrics{}++ // Try SystemInfo.getProcessInfo first (browser-level, non-intrusive)+ procInfo, err := b.getProcessInfo()if err != nil {- return nil, err+ slog.Debug("SystemInfo.getProcessInfo failed, using fallback", "err", err)+ return b.getMemoryViaProcessTree()}- total := &MemoryMetrics{}- for _, t := range targets {- if t.Type != "page" {- continue- }- // Try to get metrics - skip on any error- mem := b.safeGetMetricsForTarget(string(t.TargetID))- if mem == nil {- continue+ var totalMem uint64+ rendererCount := 0++ for _, p := range procInfo {+ if p.Type == "renderer" || p.Type == "tab" {+ mem, err := getProcessMemory(int32(p.ID))+ if err != nil {+ slog.Debug("failed to get process memory", "pid", p.ID, "err", err)+ continue+ }+ totalMem += mem+ rendererCount++}- total.JSHeapUsedMB += mem.JSHeapUsedMB- total.JSHeapTotalMB += mem.JSHeapTotalMB- total.Documents += mem.Documents- total.Frames += mem.Frames- total.Nodes += mem.Nodes- total.Listeners += mem.Listeners}- return total, nil++ result.MemoryMB = float64(totalMem) / (1024 * 1024)+ result.Renderers = rendererCount++ // Estimate JS heap as ~40% of total memory (rough heuristic)+ result.JSHeapUsedMB = result.MemoryMB * 0.4+ result.JSHeapTotalMB = result.MemoryMB * 0.5++ return result, nil}-// safeGetMetricsForTarget safely gets metrics, returning nil on any error-func (b *Bridge) safeGetMetricsForTarget(targetID string) *MemoryMetrics {- mem, err := b.getMetricsForTarget(targetID)+// getProcessInfo calls SystemInfo.getProcessInfo via CDP+func (b *Bridge) getProcessInfo() ([]*systeminfo.ProcessInfo, error) {+ ctx, cancel := context.WithTimeout(b.BrowserCtx, 5*time.Second)+ defer cancel()++ var procInfo []*systeminfo.ProcessInfo+ err := chromedp.Run(ctx,+ chromedp.ActionFunc(func(ctx context.Context) error {+ var err error+ procInfo, err = systeminfo.GetProcessInfo().Do(ctx)+ return err+ }),+ )+ return procInfo, err+}++// getMemoryViaProcessTree is a fallback that walks the browser process tree+func (b *Bridge) getMemoryViaProcessTree() (*MemoryMetrics, error) {+ result := &MemoryMetrics{}++ // Get browser PID from chromedp+ browser := chromedp.FromContext(b.BrowserCtx)+ if browser == nil || browser.Browser == nil {+ return result, nil+ }++ proc := browser.Browser.Process()+ if proc == nil {+ return result, nil+ }++ mainPID := int32(proc.Pid)+ p, err := process.NewProcess(mainPID)if err != nil {- return nil+ return result, err}- return mem-}-// getMetricsForTarget gets metrics for a raw CDP target ID-func (b *Bridge) getMetricsForTarget(targetID string) (result *MemoryMetrics, err error) {- // Recover from panics - some targets may not support metrics- defer func() {- if r := recover(); r != nil {- result = nil- err = nil // swallow error, just skip this target- }- }()+ // Get all children (recursive)+ children, err := p.Children()+ if err != nil {+ // Just count main process+ mem, _ := getProcessMemory(mainPID)+ result.MemoryMB = float64(mem) / (1024 * 1024)+ return result, nil+ }- // Create context with timeout to avoid hanging- ctx, cancel := chromedp.NewContext(b.BrowserCtx, chromedp.WithTargetID(target.ID(targetID)))- defer cancel()+ var totalMem uint64+ rendererCount := 0- timeoutCtx, timeoutCancel := context.WithTimeout(ctx, 2*time.Second)- defer timeoutCancel()+ // Add main process+ mem, _ := getProcessMemory(mainPID)+ totalMem += mem++ // Add children+ for _, child := range children {+ cmdline, _ := child.Cmdline()+ // Filter for renderer processes+ if containsRenderer(cmdline) {+ rendererCount+++ }+ childMem, _ := getProcessMemory(child.Pid)+ totalMem += childMem+ }- return getMetricsFromContext(timeoutCtx)+ result.MemoryMB = float64(totalMem) / (1024 * 1024)+ result.Renderers = rendererCount+ result.JSHeapUsedMB = result.MemoryMB * 0.4+ result.JSHeapTotalMB = result.MemoryMB * 0.5++ return result, nil}-func getMetricsFromContext(ctx context.Context) (*MemoryMetrics, error) {- // Enable performance metrics collection- if err := chromedp.Run(ctx, performance.Enable()); err != nil {- return nil, err+// getProcessMemory returns RSS memory in bytes for a process+func getProcessMemory(pid int32) (uint64, error) {+ p, err := process.NewProcess(pid)+ if err != nil {+ return 0, err}- var metrics []*performance.Metric- if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {- var err error- metrics, err = performance.GetMetrics().Do(ctx)- return err- })); err != nil {- return nil, err+ mem, err := p.MemoryInfo()+ if err != nil {+ return 0, err}- result := &MemoryMetrics{}- for _, m := range metrics {- switch m.Name {- case "JSHeapUsedSize":- result.JSHeapUsedMB = m.Value / (1024 * 1024)- case "JSHeapTotalSize":- result.JSHeapTotalMB = m.Value / (1024 * 1024)- case "Documents":- result.Documents = int64(m.Value)- case "Frames":- result.Frames = int64(m.Value)- case "Nodes":- result.Nodes = int64(m.Value)- case "JSEventListeners":- result.Listeners = int64(m.Value)+ // RSS is the resident set size (actual RAM used)+ return mem.RSS, nil+}++// containsRenderer checks if cmdline indicates a renderer process+func containsRenderer(cmdline string) bool {+ return len(cmdline) > 0 && (stringContains(cmdline, "--type=renderer") || stringContains(cmdline, "--type=tab"))+}++func stringContains(s, substr string) bool {+ for i := 0; i <= len(s)-len(substr); i++ {+ if s[i:i+len(substr)] == substr {+ return true}}-- return result, nil+ return false}