Skip to content

Commit 2eb2e16

Browse files
committed
improve test for non-sliding window implementation to not count empty lines
1 parent 10e1995 commit 2eb2e16

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

pkg/github/actions_test.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ func Test_MemoryUsage_SlidingWindow_vs_NoWindow(t *testing.T) {
12291229

12301230
const logLines = 100000
12311231
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"
12331233

12341234
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
12351235
w.WriteHeader(http.StatusOK)
@@ -1246,6 +1246,10 @@ func Test_MemoryUsage_SlidingWindow_vs_NoWindow(t *testing.T) {
12461246
ctx := context.Background()
12471247

12481248
debug.SetGCPercent(-1)
1249+
defer debug.SetGCPercent(100)
1250+
1251+
runtime.GC()
1252+
runtime.GC()
12491253
profile1, err1 := profiler.ProfileFuncWithMetrics(ctx, "sliding_window", func() (int, int64, error) {
12501254
resp1, err := http.Get(testServer.URL)
12511255
if err != nil {
@@ -1257,30 +1261,55 @@ func Test_MemoryUsage_SlidingWindow_vs_NoWindow(t *testing.T) {
12571261
})
12581262
require.NoError(t, err1)
12591263

1264+
runtime.GC()
12601265
runtime.GC()
12611266
profile2, err2 := profiler.ProfileFuncWithMetrics(ctx, "no_window", func() (int, int64, error) {
12621267
resp2, err := http.Get(testServer.URL)
12631268
if err != nil {
12641269
return 0, 0, err
12651270
}
12661271
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)
12681274
if err != nil {
12691275
return 0, 0, err
12701276
}
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
12741292
}
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
12771296
})
12781297
require.NoError(t, err2)
1279-
debug.SetGCPercent(100)
12801298

12811299
assert.Greater(t, profile2.MemoryDelta, profile1.MemoryDelta,
12821300
"Sliding window should use less memory than reading all into memory")
12831301

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+
12841313
t.Logf("Sliding window: %s", profile1.String())
12851314
t.Logf("No window: %s", profile2.String())
12861315
}

0 commit comments

Comments
 (0)