Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions _example/example01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ func main() {
defer q.Release()

// assign tasks in queue
for i := 0; i < taskN; i++ {
go func(i int) {
for i := range taskN {
go func() {
if err := q.QueueTask(func(ctx context.Context) error {
rets <- fmt.Sprintf("Hi Gopher, handle the job: %02d", +i)
return nil
}); err != nil {
panic(err)
}
}(i)
}()
}

// wait until all tasks done
for i := 0; i < taskN; i++ {
for range taskN {
fmt.Println("message:", <-rets)
time.Sleep(20 * time.Millisecond)
}
Expand Down
8 changes: 4 additions & 4 deletions _example/example02/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ func main() {
defer q.Release()

// assign tasks in queue
for i := 0; i < taskN; i++ {
go func(i int) {
for i := range taskN {
go func() {
if err := q.Queue(&job{
Name: "Gopher",
Message: fmt.Sprintf("handle the job: %d", i+1),
}); err != nil {
log.Println(err)
}
}(i)
}()
}

// wait until all tasks done
for i := 0; i < taskN; i++ {
for range taskN {
fmt.Println("message:", <-rets)
time.Sleep(20 * time.Millisecond)
}
Expand Down
8 changes: 3 additions & 5 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ func BenchmarkQueueTask(b *testing.B) {
WithLogger(emptyLogger{}),
)
b.ReportAllocs()
b.ResetTimer()

m := job.NewTask(func(context.Context) error {
return nil
})

for n := 0; n < b.N; n++ {
for b.Loop() {
if err := q.queue(&m); err != nil {
b.Fatal(err)
}
Expand All @@ -73,13 +72,12 @@ func BenchmarkQueue(b *testing.B) {
WithLogger(emptyLogger{}),
)
b.ReportAllocs()
b.ResetTimer()

m := job.NewMessage(&mockMessage{
message: testMessageFoo,
})

for n := 0; n < b.N; n++ {
for b.Loop() {
if err := q.queue(&m); err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -129,7 +127,7 @@ func BenchmarkRingWithTask(b *testing.B) {
WithLogger(emptyLogger{}),
)

for n := 0; n < b.N; n++ {
for b.Loop() {
_ = q.run(&task)
}
}
3 changes: 1 addition & 2 deletions handle_performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func BenchmarkHandleRetry(b *testing.B) {
},
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
for b.Loop() {
attempts = 0
m.RetryCount = retries
if err := q.handle(&m); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions job/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func BenchmarkNewTask(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
NewTask(func(context.Context) error {
return nil
},
Expand All @@ -23,7 +23,7 @@ func BenchmarkNewTask(b *testing.B) {

func BenchmarkNewMessage(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = NewMessage(mockMessage{
message: "foo",
},
Expand All @@ -38,7 +38,7 @@ func BenchmarkNewMessage(b *testing.B) {

func BenchmarkNewOption(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = NewOptions(
AllowOption{
RetryCount: Int64(100),
Expand Down
3 changes: 1 addition & 2 deletions ring_performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func BenchmarkRingBoundary(b *testing.B) {
}
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
for b.Loop() {
if err := w.Queue(&m); err != nil {
b.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,7 @@ func BenchmarkRingQueue(b *testing.B) {
w := NewRing()
m := mockMessage{message: testMessageTest}

b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
for range 100 {
_ = w.Queue(&m)
}
Expand Down
16 changes: 8 additions & 8 deletions thread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@ import (
func TestRoutineGroupRun(t *testing.T) {
t.Run("execute single function", func(t *testing.T) {
g := newRoutineGroup()
var counter int32
var counter atomic.Int32

g.Run(func() {
atomic.AddInt32(&counter, 1)
counter.Add(1)
})

g.Wait()

if atomic.LoadInt32(&counter) != 1 {
t.Errorf("expected counter to be 1, got %d", counter)
if counter.Load() != 1 {
t.Errorf("expected counter to be 1, got %d", counter.Load())
}
})

t.Run("execute multiple functions", func(t *testing.T) {
g := newRoutineGroup()
var counter int32
var counter atomic.Int32
numRoutines := 10

for range numRoutines {
g.Run(func() {
atomic.AddInt32(&counter, 1)
counter.Add(1)
time.Sleep(10 * time.Millisecond)
})
}

g.Wait()

if atomic.LoadInt32(&counter) != int32(numRoutines) {
t.Errorf("expected counter to be %d, got %d", numRoutines, counter)
if counter.Load() != int32(numRoutines) {
t.Errorf("expected counter to be %d, got %d", numRoutines, counter.Load())
}
})
}
Loading