Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use binary search in getLineIndex method #4151

Merged
merged 4 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/app/backend/resource/container/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package container

import (
"fmt"
"reflect"
"testing"

Expand Down Expand Up @@ -47,6 +48,40 @@ var log5 = logs.LogLine{
Content: "log5",
}

var details *logs.LogDetails

func benchmarkGetLogDetails(lines int, timestamp string, b *testing.B) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC benchmarks are not executed as part of the test spec by default (they need -bench arg). It's a nice addition though. Have you compared old implementation performance of getLineIndex with the new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@floreks Yes I compared the performance. The summary is here: #3900 (comment).

Overall, there's a negligible speedup.

// 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)
Expand Down
18 changes: 10 additions & 8 deletions src/app/backend/resource/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package logs

import (
"sort"
"strings"
)

Expand Down Expand Up @@ -193,18 +194,19 @@ func (self LogLines) getLineIndex(logLineId *LogLineId) int {
return 0
}
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
})

linesMatched := 0
if matchingStartedAt < len(self) && self[matchingStartedAt].Timestamp == logTimestamp { // match found
for (matchingStartedAt+linesMatched) < len(self) && self[matchingStartedAt+linesMatched].Timestamp == logTimestamp {
linesMatched += 1
} else if linesMatched > 0 {
break
}
}

var offset int
if logLineId.LineNum < 0 {
offset = linesMatched + logLineId.LineNum
Expand Down