Skip to content

Commit

Permalink
added benchmark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Oct 21, 2023
1 parent bfb46ac commit 870092f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion queue.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
43 changes: 42 additions & 1 deletion queue_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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))
}
Expand All @@ -187,18 +190,56 @@ 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)
if topIdx < idx {
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)
}
if want, have := b.N-1, q.Len(); want != have {
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)
}
}

0 comments on commit 870092f

Please sign in to comment.