-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmemory_usage_test.go
More file actions
168 lines (144 loc) · 4.9 KB
/
Copy pathmemory_usage_test.go
File metadata and controls
168 lines (144 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package rti
import (
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
// getProcessMemory returns RSS (Resident Set Size) in bytes
func getProcessMemory() (int64, error) {
pid := os.Getpid()
cmd := exec.Command("ps", "-o", "rss=", "-p", strconv.Itoa(pid))
output, err := cmd.Output()
if err != nil {
return 0, err
}
rssKB, err := strconv.ParseInt(strings.TrimSpace(string(output)), 10, 64)
if err != nil {
return 0, err
}
return rssKB * 1024, nil // Convert KB to bytes
}
// newMemoryTestConnector creates a connector using ShapeType (like README example)
func newMemoryTestConnector() (*Connector, error) {
_, curPath, _, _ := runtime.Caller(0)
xmlPath := path.Join(path.Dir(curPath), "./test/xml/MemoryTestShape.xml")
return NewConnector("MyParticipantLibrary::Zero", xmlPath)
}
// TestMemoryUsage tests RTI Connector with comprehensive memory profiling
func TestMemoryUsage(t *testing.T) {
iterations := 1
if iterStr := os.Getenv("MEMTEST_ITERATIONS"); iterStr != "" {
if parsed, err := strconv.Atoi(iterStr); err == nil && parsed > 0 {
iterations = parsed
}
}
// Get initial system memory
initialRSS, err := getProcessMemory()
if err != nil {
t.Logf("Warning: Could not get initial RSS: %v", err)
initialRSS = 0
}
var m1, m2 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m1)
t.Logf("Starting memory measurement:")
t.Logf(" Initial RSS: %d bytes (%.2f MB)", initialRSS, float64(initialRSS)/1024/1024)
t.Logf(" Initial Go heap: %d bytes (%.2f MB)", m1.HeapAlloc, float64(m1.HeapAlloc)/1024/1024)
for i := 0; i < iterations; i++ {
runConnectorExample(t, i)
// Sample memory after each iteration
if iterations > 1 && (i+1)%max(1, iterations/5) == 0 {
currentRSS, _ := getProcessMemory()
var currentMem runtime.MemStats
runtime.ReadMemStats(¤tMem)
t.Logf(" After iteration %d: RSS=%d bytes, Go heap=%d bytes",
i+1, currentRSS, currentMem.HeapAlloc)
}
}
runtime.GC()
time.Sleep(100 * time.Millisecond) // Give GC time to complete
runtime.ReadMemStats(&m2)
// Get final system memory
finalRSS, err := getProcessMemory()
if err != nil {
t.Logf("Warning: Could not get final RSS: %v", err)
finalRSS = initialRSS
}
allocDelta := m2.TotalAlloc - m1.TotalAlloc
heapDelta := int64(m2.HeapAlloc) - int64(m1.HeapAlloc)
rssDelta := finalRSS - initialRSS
t.Logf("\n=== Memory Analysis Results ===")
t.Logf("Iterations: %d", iterations)
t.Logf("\nGo Heap Memory:")
t.Logf(" Total allocations: %d bytes (%.2f KB)", allocDelta, float64(allocDelta)/1024)
t.Logf(" Heap delta: %d bytes (%.2f KB)", heapDelta, float64(heapDelta)/1024)
t.Logf(" Final heap size: %d bytes (%.2f MB)", m2.HeapAlloc, float64(m2.HeapAlloc)/1024/1024)
t.Logf(" GC runs: %d", m2.NumGC-m1.NumGC)
t.Logf("\nSystem Memory (RSS - includes C libraries):")
t.Logf(" Initial RSS: %d bytes (%.2f MB)", initialRSS, float64(initialRSS)/1024/1024)
t.Logf(" Final RSS: %d bytes (%.2f MB)", finalRSS, float64(finalRSS)/1024/1024)
t.Logf(" RSS delta: %d bytes (%.2f MB)", rssDelta, float64(rssDelta)/1024/1024)
t.Logf("\nMemory per Operation:")
if iterations > 0 {
t.Logf(" Go heap per op: %.2f bytes", float64(allocDelta)/float64(iterations))
t.Logf(" RSS per op: %.2f bytes", float64(rssDelta)/float64(iterations))
}
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func runConnectorExample(t *testing.T, iteration int) {
// Use ShapeType configuration like in the main README example
// This provides realistic memory usage measurements for the documented API
connector, err := newMemoryTestConnector()
if err != nil {
t.Fatalf("Iteration %d: Failed to create connector: %v", iteration, err)
}
defer connector.Delete()
// Get output (writer) and publish shape data
output, err := connector.GetOutput("MyPublisher::MySquareWriter")
if err != nil {
t.Fatalf("Iteration %d: Failed to get output: %v", iteration, err)
}
// Publish shape data (same as README example)
output.Instance.SetString("color", "BLUE")
output.Instance.SetInt("x", iteration*10)
output.Instance.SetInt("y", iteration*20)
output.Instance.SetInt("shapesize", 30)
err = output.Write()
if err != nil {
t.Fatalf("Iteration %d: Failed to write: %v", iteration, err)
}
if iteration%10 == 0 {
t.Logf("Completed iteration %d", iteration)
}
}
// BenchmarkConnectorMemory provides benchmark-based memory analysis using ShapeType
func BenchmarkConnectorMemory(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
connector, err := newMemoryTestConnector()
if err != nil {
b.Fatalf("Failed to create connector: %v", err)
}
output, err := connector.GetOutput("MyPublisher::MySquareWriter")
if err != nil {
connector.Delete()
b.Fatalf("Failed to get output: %v", err)
}
output.Instance.SetString("color", "BLUE")
output.Instance.SetInt("x", i*10)
output.Instance.SetInt("y", i*20)
output.Instance.SetInt("shapesize", 30)
output.Write()
connector.Delete()
}
}