From cc5274ce62c670f6e8f7a4d684c70e1bde80416f Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Sat, 23 Dec 2023 18:09:11 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20buffer.Unbounded?= =?UTF-8?q?=20=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/buffer/unbounded_benchmark_test.go | 27 +++++------------------- utils/buffer/unbounded_test.go | 17 +++++++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 utils/buffer/unbounded_test.go diff --git a/utils/buffer/unbounded_benchmark_test.go b/utils/buffer/unbounded_benchmark_test.go index 3d9b2bb..99c5803 100644 --- a/utils/buffer/unbounded_benchmark_test.go +++ b/utils/buffer/unbounded_benchmark_test.go @@ -6,29 +6,12 @@ import ( ) func BenchmarkUnboundedBuffer(b *testing.B) { - ub := buffer.NewUnboundedN[int]() + ub := buffer.NewUnbounded[int]() - b.Run("Put", func(b *testing.B) { - for i := 0; i < b.N; i++ { - ub.Put(i) - } - }) - - b.Run("Load", func(b *testing.B) { - for i := 0; i < b.N; i++ { - ub.Load() - } - }) - - // 先填充数据以防止 Get 被阻塞 - for i := 0; i < b.N; i++ { - ub.Put(i) - } - - b.Run("Get", func(b *testing.B) { - b.ResetTimer() - for i := 0; i < b.N; i++ { - ub.Put(i) + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + ub.Put(1) <-ub.Get() ub.Load() } diff --git a/utils/buffer/unbounded_test.go b/utils/buffer/unbounded_test.go new file mode 100644 index 0000000..ea8b75f --- /dev/null +++ b/utils/buffer/unbounded_test.go @@ -0,0 +1,17 @@ +package buffer_test + +import ( + "fmt" + "github.com/kercylan98/minotaur/utils/buffer" + "testing" +) + +func TestUnbounded_Get(t *testing.T) { + ub := buffer.NewUnbounded[int]() + for i := 0; i < 100; i++ { + ub.Put(i + 1) + fmt.Println(<-ub.Get()) + //<-ub.Get() + ub.Load() + } +}