@@ -1229,7 +1229,7 @@ func Test_MemoryUsage_SlidingWindow_vs_NoWindow(t *testing.T) {
1229
1229
1230
1230
const logLines = 100000
1231
1231
const bufferSize = 5000
1232
- largeLogContent := strings .Repeat ("log line with some content\n " , logLines )
1232
+ largeLogContent := strings .Repeat ("log line with some content\n " , logLines - 1 ) + "final log line"
1233
1233
1234
1234
testServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
1235
1235
w .WriteHeader (http .StatusOK )
@@ -1246,6 +1246,10 @@ func Test_MemoryUsage_SlidingWindow_vs_NoWindow(t *testing.T) {
1246
1246
ctx := context .Background ()
1247
1247
1248
1248
debug .SetGCPercent (- 1 )
1249
+ defer debug .SetGCPercent (100 )
1250
+
1251
+ runtime .GC ()
1252
+ runtime .GC ()
1249
1253
profile1 , err1 := profiler .ProfileFuncWithMetrics (ctx , "sliding_window" , func () (int , int64 , error ) {
1250
1254
resp1 , err := http .Get (testServer .URL )
1251
1255
if err != nil {
@@ -1257,30 +1261,55 @@ func Test_MemoryUsage_SlidingWindow_vs_NoWindow(t *testing.T) {
1257
1261
})
1258
1262
require .NoError (t , err1 )
1259
1263
1264
+ runtime .GC ()
1260
1265
runtime .GC ()
1261
1266
profile2 , err2 := profiler .ProfileFuncWithMetrics (ctx , "no_window" , func () (int , int64 , error ) {
1262
1267
resp2 , err := http .Get (testServer .URL )
1263
1268
if err != nil {
1264
1269
return 0 , 0 , err
1265
1270
}
1266
1271
defer resp2 .Body .Close () //nolint:bodyclose // Response body is closed in downloadLogContent, but we need to return httpResp
1267
- content , err := io .ReadAll (resp2 .Body )
1272
+
1273
+ allContent , err := io .ReadAll (resp2 .Body )
1268
1274
if err != nil {
1269
1275
return 0 , 0 , err
1270
1276
}
1271
- lines := strings .Split (string (content ), "\n " )
1272
- if len (lines ) > bufferSize {
1273
- lines = lines [len (lines )- bufferSize :]
1277
+
1278
+ allLines := strings .Split (string (allContent ), "\n " )
1279
+ var nonEmptyLines []string
1280
+ for _ , line := range allLines {
1281
+ if line != "" {
1282
+ nonEmptyLines = append (nonEmptyLines , line )
1283
+ }
1284
+ }
1285
+ totalLines := len (nonEmptyLines )
1286
+
1287
+ var resultLines []string
1288
+ if totalLines > bufferSize {
1289
+ resultLines = nonEmptyLines [totalLines - bufferSize :]
1290
+ } else {
1291
+ resultLines = nonEmptyLines
1274
1292
}
1275
- result := strings .Join (lines , "\n " )
1276
- return len (strings .Split (string (content ), "\n " )), int64 (len (result )), nil
1293
+
1294
+ result := strings .Join (resultLines , "\n " )
1295
+ return totalLines , int64 (len (result )), nil
1277
1296
})
1278
1297
require .NoError (t , err2 )
1279
- debug .SetGCPercent (100 )
1280
1298
1281
1299
assert .Greater (t , profile2 .MemoryDelta , profile1 .MemoryDelta ,
1282
1300
"Sliding window should use less memory than reading all into memory" )
1283
1301
1302
+ assert .Equal (t , profile1 .LinesCount , profile2 .LinesCount ,
1303
+ "Both approaches should count the same number of input lines" )
1304
+ assert .InDelta (t , profile1 .BytesCount , profile2 .BytesCount , 100 ,
1305
+ "Both approaches should produce similar output sizes (within 100 bytes)" )
1306
+
1307
+ memoryReduction := float64 (profile2 .MemoryDelta - profile1 .MemoryDelta ) / float64 (profile2 .MemoryDelta ) * 100
1308
+ t .Logf ("Memory reduction: %.1f%% (%.2f MB vs %.2f MB)" ,
1309
+ memoryReduction ,
1310
+ float64 (profile2 .MemoryDelta )/ 1024 / 1024 ,
1311
+ float64 (profile1 .MemoryDelta )/ 1024 / 1024 )
1312
+
1284
1313
t .Logf ("Sliding window: %s" , profile1 .String ())
1285
1314
t .Logf ("No window: %s" , profile2 .String ())
1286
1315
}
0 commit comments