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

addiing parallelism to speed up performace issue #6111 #6139

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
20 changes: 16 additions & 4 deletions pkg/testutils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,30 @@ func (b *Buffer) Write(p []byte) (int, error) {
return b.Buffer.Write(p)
}

// LogMatcher is a helper func that returns true if the subStr appears more than 'occurrences' times in the logs.
var LogMatcher = func(occurrences int, subStr string, logs []string) (bool, string) {
errMsg := fmt.Sprintf("subStr '%s' does not occur %d time(s) in %v", subStr, occurrences, logs)
if len(logs) < occurrences {
return false, errMsg
}

// Count occurrences in parallel
Copy link
Member

Choose a reason for hiding this comment

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

why?

Copy link
Author

Choose a reason for hiding this comment

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

Initially I thought that as log volume increases , concurrent checking for the occurrences would help. But now that I checked the function calls I see that none of the calls affect the testing time of the command that was mentioned in the issue. I have removed the changes to my code locally . Should I submit a new PR? Are there any more changes that I need to do ?
Thanks.

var count int
var wg sync.WaitGroup
var mu sync.Mutex

for _, log := range logs {
if strings.Contains(log, subStr) {
count++
}
wg.Add(1)
go func(log string) {
defer wg.Done()
if strings.Contains(log, subStr) {
mu.Lock()
count++
mu.Unlock()
}
}(log)
}
wg.Wait()

if count >= occurrences {
return true, ""
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/testutils/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

func TestNewLogger(t *testing.T) {
t.Parallel()
logger, log := NewLogger()
logger.Warn("hello", zap.String("x", "y"))

Expand All @@ -26,11 +27,13 @@ func TestNewLogger(t *testing.T) {
}

func TestNewEchoLogger(t *testing.T) {
t.Parallel()
logger, _ := NewEchoLogger(t)
logger.Warn("hello", zap.String("x", "y"))
}

func TestJSONLineError(t *testing.T) {
t.Parallel()
log := &Buffer{}
log.WriteString("bad-json\n")
_, ok := log.JSONLine(0)["error"]
Expand Down Expand Up @@ -64,6 +67,7 @@ func TestRaceCondition(*testing.T) {
}

func TestLogMatcher(t *testing.T) {
t.Parallel()
tests := []struct {
occurrences int
subStr string
Expand All @@ -80,6 +84,7 @@ func TestLogMatcher(t *testing.T) {
for i, tt := range tests {
test := tt
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
t.Parallel()
match, errMsg := LogMatcher(test.occurrences, test.subStr, test.logs)
assert.Equal(t, test.expected, match)
assert.Equal(t, test.errMsg, errMsg)
Expand Down