From 870092f8b519ceff0d1f1b0299644043433a5d44 Mon Sep 17 00:00:00 2001 From: caffix Date: Sat, 21 Oct 2023 01:49:11 -0400 Subject: [PATCH] added benchmark tests --- queue.go | 2 +- queue_test.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/queue.go b/queue.go index a1a888f..78faf14 100644 --- a/queue.go +++ b/queue.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2017-2022. All rights reserved. +// Copyright © by Jeff Foley 2017-2023. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 diff --git a/queue_test.go b/queue_test.go index 04d7e9f..b29cafc 100644 --- a/queue_test.go +++ b/queue_test.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2017-2022. All rights reserved. +// Copyright © by Jeff Foley 2017-2023. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 @@ -164,9 +164,12 @@ func TestLen(t *testing.T) { func BenchmarkAppend(b *testing.B) { q := NewQueue() + b.StartTimer() for i := 0; i < b.N; i++ { q.Append("testing") } + b.StopTimer() + if e, _ := q.Next(); e != "testing" { b.Errorf("the element was appended as %s instead of 'testing'", e.(string)) } @@ -187,7 +190,9 @@ func BenchmarkAppendPriority(b *testing.B) { {"valueHigh", PriorityHigh}, {"valueCritical", PriorityCritical}, } + topIdx := -1 + b.StartTimer() for i := 0; i < b.N; i++ { idx := i % len(values) q.AppendPriority(values[idx].token, values[idx].priority) @@ -195,6 +200,8 @@ func BenchmarkAppendPriority(b *testing.B) { topIdx = idx } } + b.StopTimer() + if e, _ := q.Next(); topIdx > -1 && e != values[topIdx].token { b.Errorf("the element was appended as %s instead of %s", e.(string), values[topIdx].token) } @@ -202,3 +209,37 @@ func BenchmarkAppendPriority(b *testing.B) { b.Errorf("expected %d elements left on the queue, got %d", want, have) } } + +func BenchmarkNext(b *testing.B) { + q := NewQueue() + for i := 0; i < b.N; i++ { + q.Append("testing") + } + + b.StartTimer() + for i := 0; i < b.N; i++ { + _, _ = q.Next() + } + b.StopTimer() + + if have := q.Len(); have != 0 { + b.Errorf("expected 0 elements left on the queue, got %d", have) + } +} + +func BenchmarkProcess(b *testing.B) { + q := NewQueue() + for i := 0; i < b.N; i++ { + q.Append("testing") + } + + b.StartTimer() + for i := 0; i < b.N; i++ { + q.Process(func(e interface{}) {}) + } + b.StopTimer() + + if have := q.Len(); have != 0 { + b.Errorf("expected 0 elements left on the queue, got %d", have) + } +}