From 5e319f7adf7b856631984f889ec7a7ebb9fd2121 Mon Sep 17 00:00:00 2001 From: Elijah Oyekunle Date: Thu, 1 Aug 2019 14:32:20 +0100 Subject: [PATCH 1/4] Use binary search in getLineIndex method --- src/app/backend/resource/logs/logs.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/app/backend/resource/logs/logs.go b/src/app/backend/resource/logs/logs.go index 6eb644ae72d6..5a4d579c07e3 100644 --- a/src/app/backend/resource/logs/logs.go +++ b/src/app/backend/resource/logs/logs.go @@ -15,6 +15,7 @@ package logs import ( + "sort" "strings" ) @@ -194,17 +195,18 @@ func (self LogLines) getLineIndex(logLineId *LogLineId) int { } logTimestamp := logLineId.LogTimestamp linesMatched := 0 - matchingStartedAt := 0 - for idx := range self { - if self[idx].Timestamp == logTimestamp { - if linesMatched == 0 { - matchingStartedAt = idx - } + matchingStartedAt := sort.Search(len(self), func(i int) bool { + return self[i].Timestamp == logTimestamp + }) + + if matchingStartedAt == len(self) { + matchingStartedAt = 0 + } else { + for self[matchingStartedAt+linesMatched].Timestamp == logTimestamp { linesMatched += 1 - } else if linesMatched > 0 { - break } } + var offset int if logLineId.LineNum < 0 { offset = linesMatched + logLineId.LineNum From 8a1ae40b3a847be248f66c418ada399992227757 Mon Sep 17 00:00:00 2001 From: Elijah Oyekunle Date: Thu, 1 Aug 2019 14:48:54 +0100 Subject: [PATCH 2/4] fix comparison function --- src/app/backend/resource/logs/logs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/backend/resource/logs/logs.go b/src/app/backend/resource/logs/logs.go index 5a4d579c07e3..b7663e3f2171 100644 --- a/src/app/backend/resource/logs/logs.go +++ b/src/app/backend/resource/logs/logs.go @@ -196,7 +196,7 @@ func (self LogLines) getLineIndex(logLineId *LogLineId) int { logTimestamp := logLineId.LogTimestamp linesMatched := 0 matchingStartedAt := sort.Search(len(self), func(i int) bool { - return self[i].Timestamp == logTimestamp + return self[i].Timestamp >= logTimestamp }) if matchingStartedAt == len(self) { From 858f98a2a82a18150d937ad11c79b7fcdb9d52d4 Mon Sep 17 00:00:00 2001 From: Elijah Oyekunle Date: Thu, 1 Aug 2019 17:13:33 +0100 Subject: [PATCH 3/4] fix bug --- src/app/backend/resource/logs/logs.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/backend/resource/logs/logs.go b/src/app/backend/resource/logs/logs.go index b7663e3f2171..2be378b028c5 100644 --- a/src/app/backend/resource/logs/logs.go +++ b/src/app/backend/resource/logs/logs.go @@ -194,15 +194,15 @@ func (self LogLines) getLineIndex(logLineId *LogLineId) int { return 0 } logTimestamp := logLineId.LogTimestamp - linesMatched := 0 matchingStartedAt := sort.Search(len(self), func(i int) bool { return self[i].Timestamp >= logTimestamp }) - if matchingStartedAt == len(self) { + linesMatched := 0 + if matchingStartedAt == len(self) { // match not found matchingStartedAt = 0 } else { - for self[matchingStartedAt+linesMatched].Timestamp == logTimestamp { + for (matchingStartedAt+linesMatched) < len(self) && self[matchingStartedAt+linesMatched].Timestamp == logTimestamp { linesMatched += 1 } } From e74db03b7ab3cc2d6e0a05a9ce426efb0d5fece0 Mon Sep 17 00:00:00 2001 From: Elijah Oyekunle Date: Thu, 1 Aug 2019 18:54:28 +0100 Subject: [PATCH 4/4] added benchmark function --- .../backend/resource/container/logs_test.go | 35 +++++++++++++++++++ src/app/backend/resource/logs/logs.go | 8 ++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/app/backend/resource/container/logs_test.go b/src/app/backend/resource/container/logs_test.go index af3898f2fef5..bbe957002bb7 100644 --- a/src/app/backend/resource/container/logs_test.go +++ b/src/app/backend/resource/container/logs_test.go @@ -15,6 +15,7 @@ package container import ( + "fmt" "reflect" "testing" @@ -47,6 +48,40 @@ var log5 = logs.LogLine{ Content: "log5", } +var details *logs.LogDetails + +func benchmarkGetLogDetails(lines int, timestamp string, b *testing.B) { + // Generate raw logs. + rawLogs := "" + for i := 0; i < lines; i++ { + rawLogs += fmt.Sprintf("%[1]d log%[1]d\n", i) + } + + selector := &logs.Selection{ + ReferencePoint: logs.LogLineId{ + LogTimestamp: logs.LogTimestamp(timestamp), + LineNum: 1, + }, + OffsetFrom: -2, + OffsetTo: 0, + } + + var d *logs.LogDetails + + b.ResetTimer() + for n := 0; n < b.N; n++ { + d = ConstructLogDetails("pod-1", rawLogs, "list", selector) + } + b.StopTimer() + + details = d +} + +func BenchmarkGetLogDetails1(b *testing.B) { benchmarkGetLogDetails(2000, "1999", b) } +func BenchmarkGetLogDetails2(b *testing.B) { benchmarkGetLogDetails(2000, "999", b) } +func BenchmarkGetLogDetails3(b *testing.B) { benchmarkGetLogDetails(2000, "99", b) } +func BenchmarkGetLogDetails4(b *testing.B) { benchmarkGetLogDetails(2000, "9", b) } + func TestGetLogs(t *testing.T) { // for the test cases, the line read limit is reduced to 10 lineReadLimit = int64(10) diff --git a/src/app/backend/resource/logs/logs.go b/src/app/backend/resource/logs/logs.go index 2be378b028c5..0804d23ea39a 100644 --- a/src/app/backend/resource/logs/logs.go +++ b/src/app/backend/resource/logs/logs.go @@ -194,14 +194,14 @@ func (self LogLines) getLineIndex(logLineId *LogLineId) int { return 0 } logTimestamp := logLineId.LogTimestamp - matchingStartedAt := sort.Search(len(self), func(i int) bool { + + matchingStartedAt := 0 + matchingStartedAt = sort.Search(len(self), func(i int) bool { return self[i].Timestamp >= logTimestamp }) linesMatched := 0 - if matchingStartedAt == len(self) { // match not found - matchingStartedAt = 0 - } else { + if matchingStartedAt < len(self) && self[matchingStartedAt].Timestamp == logTimestamp { // match found for (matchingStartedAt+linesMatched) < len(self) && self[matchingStartedAt+linesMatched].Timestamp == logTimestamp { linesMatched += 1 }