From a37f9848717411c1e6e60809383c5dd50b2c0bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 22 Jan 2024 10:46:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BF=E9=97=AE=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=86=85=E5=AD=98=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_access_log_queue.go | 5 +- internal/nodes/http_access_log_queue_test.go | 52 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/internal/nodes/http_access_log_queue.go b/internal/nodes/http_access_log_queue.go index 3c7ed32..fe0f0ae 100644 --- a/internal/nodes/http_access_log_queue.go +++ b/internal/nodes/http_access_log_queue.go @@ -67,7 +67,8 @@ func (this *HTTPAccessLogQueue) Push(accessLog *pb.HTTPAccessLog) { // 上传访问日志 func (this *HTTPAccessLogQueue) loop() error { - var accessLogs = []*pb.HTTPAccessLog{} + const maxLen = 2000 + var accessLogs = make([]*pb.HTTPAccessLog, 0, maxLen) var count = 0 Loop: @@ -78,7 +79,7 @@ Loop: count++ // 每次只提交 N 条访问日志,防止网络拥堵 - if count > 2000 { + if count >= maxLen { break Loop } default: diff --git a/internal/nodes/http_access_log_queue_test.go b/internal/nodes/http_access_log_queue_test.go index 224e810..ac9126f 100644 --- a/internal/nodes/http_access_log_queue_test.go +++ b/internal/nodes/http_access_log_queue_test.go @@ -178,3 +178,55 @@ func BenchmarkHTTPAccessLogQueue_ToValidUTF8String(b *testing.B) { _ = strings.ToValidUTF8(s, "") } } + +func BenchmarkAppendAccessLogs(b *testing.B) { + b.ReportAllocs() + + var stat1 = &runtime.MemStats{} + runtime.ReadMemStats(stat1) + + const count = 20000 + var a = make([]*pb.HTTPAccessLog, 0, count) + for i := 0; i < b.N; i++ { + a = append(a, &pb.HTTPAccessLog{ + RequestPath: "/hello/world", + Host: "example.com", + RequestBody: bytes.Repeat([]byte{'A'}, 1024), + }) + if len(a) == count { + a = make([]*pb.HTTPAccessLog, 0, count) + } + } + + _ = len(a) + + var stat2 = &runtime.MemStats{} + runtime.ReadMemStats(stat2) + b.Log((stat2.TotalAlloc-stat1.TotalAlloc)>>20, "MB") +} + +func BenchmarkAppendAccessLogs2(b *testing.B) { + b.ReportAllocs() + + var stat1 = &runtime.MemStats{} + runtime.ReadMemStats(stat1) + + const count = 20000 + var a = []*pb.HTTPAccessLog{} + for i := 0; i < b.N; i++ { + a = append(a, &pb.HTTPAccessLog{ + RequestPath: "/hello/world", + Host: "example.com", + RequestBody: bytes.Repeat([]byte{'A'}, 1024), + }) + if len(a) == count { + a = []*pb.HTTPAccessLog{} + } + } + + _ = len(a) + + var stat2 = &runtime.MemStats{} + runtime.ReadMemStats(stat2) + b.Log((stat2.TotalAlloc-stat1.TotalAlloc)>>20, "MB") +}