hanahmily commented on code in PR #806:
URL: 
https://github.com/apache/skywalking-banyandb/pull/806#discussion_r2430812884


##########
test/stress/trace-streaming/memory_monitor.go:
##########
@@ -0,0 +1,310 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package tracestreaming
+
+import (
+       "context"
+       "encoding/csv"
+       "fmt"
+       "io"
+       "net/http"
+       "os"
+       "path/filepath"
+       "runtime"
+       "sync"
+       "time"
+
+       "github.com/apache/skywalking-banyandb/pkg/cgroups"
+)
+
+// MemorySnapshot represents a point-in-time memory measurement.
+type MemorySnapshot struct {
+       Timestamp    time.Time
+       HeapAlloc    uint64
+       HeapSys      uint64
+       HeapInuse    uint64
+       HeapIdle     uint64
+       RSS          uint64
+       NumGC        uint32
+       GCPauseNs    uint64
+       NumGoroutine int
+}
+
+// MemoryMonitor tracks memory usage over time.
+type MemoryMonitor struct {
+       pprofURL     string
+       profileDir   string
+       snapshots    []MemorySnapshot
+       pollInterval time.Duration
+       mu           sync.RWMutex
+}
+
+// NewMemoryMonitor creates a new memory monitor.
+func NewMemoryMonitor(pollInterval time.Duration, pprofURL string, profileDir 
string) *MemoryMonitor {
+       return &MemoryMonitor{
+               snapshots:    make([]MemorySnapshot, 0),
+               pollInterval: pollInterval,
+               pprofURL:     pprofURL,
+               profileDir:   profileDir,
+       }
+}
+
+// Start begins monitoring memory usage.
+func (m *MemoryMonitor) Start(ctx context.Context) {
+       ticker := time.NewTicker(m.pollInterval)
+       defer ticker.Stop()
+
+       // Create profile directory if it doesn't exist
+       if m.profileDir != "" {
+               _ = os.MkdirAll(m.profileDir, 0o755)
+       }
+
+       // Capture initial snapshot
+       m.captureSnapshot()
+
+       // Start periodic heap profiling (every 30 seconds)
+       profileTicker := time.NewTicker(30 * time.Second)
+       defer profileTicker.Stop()
+
+       for {
+               select {
+               case <-ctx.Done():
+                       return
+               case <-ticker.C:
+                       m.captureSnapshot()
+               case <-profileTicker.C:
+                       if m.pprofURL != "" && m.profileDir != "" {
+                               _ = m.captureHeapProfile()
+                       }
+               }
+       }
+}
+
+// captureSnapshot captures a memory snapshot.
+func (m *MemoryMonitor) captureSnapshot() {
+       var memStats runtime.MemStats
+       runtime.ReadMemStats(&memStats)
+
+       // Get memory limit from cgroups if available
+       // We use HeapSys as a proxy for RSS in containerized environments
+       rss := memStats.HeapSys
+       if limit, err := cgroups.MemoryLimit(); err == nil && limit > 0 {
+               // In containers, use sys memory as RSS approximation
+               rss = memStats.Sys
+       }
+
+       snapshot := MemorySnapshot{
+               Timestamp:    time.Now(),
+               HeapAlloc:    memStats.HeapAlloc,
+               HeapSys:      memStats.HeapSys,
+               HeapInuse:    memStats.HeapInuse,
+               HeapIdle:     memStats.HeapIdle,
+               RSS:          rss,
+               NumGC:        memStats.NumGC,
+               GCPauseNs:    memStats.PauseNs[(memStats.NumGC+255)%256],
+               NumGoroutine: runtime.NumGoroutine(),
+       }
+
+       m.mu.Lock()
+       m.snapshots = append(m.snapshots, snapshot)
+       m.mu.Unlock()
+}
+
+// captureHeapProfile captures a heap profile via pprof HTTP endpoint.
+func (m *MemoryMonitor) captureHeapProfile() error {
+       url := fmt.Sprintf("%s/debug/pprof/heap", m.pprofURL)
+       resp, err := http.Get(url) // #nosec G107

Review Comment:
   This is only for test purpose



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to