From 17712f857a24ad5f55cd34ca3643d6c66059e3a7 Mon Sep 17 00:00:00 2001 From: YAMASAKI Masahide Date: Thu, 18 May 2017 23:30:00 +0900 Subject: [PATCH] Improved poor memory efficiency of awslogs Signed-off-by: YAMASAKI Masahide Upstream-commit: 524f30634018ce619da61aa0a13dad245e098226 Component: engine --- .../daemon/logger/awslogs/cloudwatchlogs.go | 6 +++--- .../logger/awslogs/cloudwatchlogs_test.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/components/engine/daemon/logger/awslogs/cloudwatchlogs.go b/components/engine/daemon/logger/awslogs/cloudwatchlogs.go index dc1cd5113a..d44f09d6a0 100644 --- a/components/engine/daemon/logger/awslogs/cloudwatchlogs.go +++ b/components/engine/daemon/logger/awslogs/cloudwatchlogs.go @@ -590,9 +590,9 @@ func (slice byTimestamp) Swap(i, j int) { } func unwrapEvents(events []wrappedEvent) []*cloudwatchlogs.InputLogEvent { - cwEvents := []*cloudwatchlogs.InputLogEvent{} - for _, input := range events { - cwEvents = append(cwEvents, input.inputLogEvent) + cwEvents := make([]*cloudwatchlogs.InputLogEvent, len(events)) + for i, input := range events { + cwEvents[i] = input.inputLogEvent } return cwEvents } diff --git a/components/engine/daemon/logger/awslogs/cloudwatchlogs_test.go b/components/engine/daemon/logger/awslogs/cloudwatchlogs_test.go index 655b99c7a4..e3862ffebe 100644 --- a/components/engine/daemon/logger/awslogs/cloudwatchlogs_test.go +++ b/components/engine/daemon/logger/awslogs/cloudwatchlogs_test.go @@ -1034,3 +1034,20 @@ func TestCreateTagSuccess(t *testing.T) { t.Errorf("Expected LogStreamName to be %s", "test-container/container-abcdefghijklmnopqrstuvwxyz01234567890") } } + +func BenchmarkUnwrapEvents(b *testing.B) { + events := make([]wrappedEvent, maximumLogEventsPerPut) + for i := 0; i < maximumLogEventsPerPut; i++ { + mes := strings.Repeat("0", maximumBytesPerEvent) + events[i].inputLogEvent = &cloudwatchlogs.InputLogEvent{ + Message: &mes, + } + } + + as := assert.New(b) + b.ResetTimer() + for i := 0; i < b.N; i++ { + res := unwrapEvents(events) + as.Len(res, maximumLogEventsPerPut) + } +}