From afe447ada7890e54c3b1638b8ce8064f8b06f246 Mon Sep 17 00:00:00 2001 From: bjdgyc Date: Fri, 30 Jul 2021 16:24:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96payload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/handler/link_cstp.go | 1 + server/handler/link_dtls.go | 5 ++-- server/handler/pool_test.go | 57 +++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 server/handler/pool_test.go diff --git a/server/handler/link_cstp.go b/server/handler/link_cstp.go index 83ab68f..fda35be 100644 --- a/server/handler/link_cstp.go +++ b/server/handler/link_cstp.go @@ -69,6 +69,7 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) { copy(pl.Data, pl.Data[8:8+dataLen]) // 更新切片长度 pl.Data = pl.Data[:dataLen] + // pl.Data = append(pl.Data[:0], pl.Data[8:8+dataLen]...) if payloadIn(cSess, pl) { return } diff --git a/server/handler/link_dtls.go b/server/handler/link_dtls.go index 09c82f2..f45d951 100644 --- a/server/handler/link_dtls.go +++ b/server/handler/link_dtls.go @@ -68,9 +68,10 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) { // base.Debug("recv DPD-RESP", cSess.IpAddr) case 0x00: // DATA // 去除数据头 - copy(pl.Data, pl.Data[1:n]) + // copy(pl.Data, pl.Data[1:n]) // 更新切片长度 - pl.Data = pl.Data[:n-1] + // pl.Data = pl.Data[:n-1] + pl.Data = append(pl.Data[:0], pl.Data[1:n]...) if payloadIn(cSess, pl) { return } diff --git a/server/handler/pool_test.go b/server/handler/pool_test.go new file mode 100644 index 0000000..ffb9bbf --- /dev/null +++ b/server/handler/pool_test.go @@ -0,0 +1,57 @@ +package handler + +import ( + "testing" +) + +// go test -bench=. -benchmem + +// Strings written to buf +var strs = []string{ + "Lorem ipsum dolor sit amet, consectetur adipiscing elit", + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua", + `Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris + nisi ut aliquip ex ea commodo consequat. + Duis aute irure dolor in reprehenderit in voluptate velit esse cillum + dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, + sunt in culpa qui officia deserunt mollit anim id est laborum`, + "Sed ut perspiciatis", + "sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt", + "Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit", + "laboriosam, nisi ut aliquid ex ea commodi consequatur", + "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur", + "vel illum qui dolorem eum fugiat quo voluptas nulla pariatur", +} + +// 去除数据头 +func BenchmarkHeaderCopy(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, v := range strs { + pl := getPayload() + // 初始化数据 + pl.Data = append(pl.Data[:0], v...) + + dataLen := len(v) - 8 + copy(pl.Data, pl.Data[8:8+dataLen]) + // 更新切片长度 + pl.Data = pl.Data[:dataLen] + + putPayload(pl) + } + } +} + +func BenchmarkHeaderAppend(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, v := range strs { + pl := getPayload() + // 初始化数据 + pl.Data = append(pl.Data[:0], v...) + + dataLen := len(v) - 8 + pl.Data = append(pl.Data[:0], pl.Data[:8+dataLen]...) + + putPayload(pl) + } + } +}