2020-01-03 10:13:16 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2020-01-01 04:36:46 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Simple E2E Benchmark testing with no scheduled tasks and
|
|
|
|
// no retries.
|
|
|
|
func BenchmarkEndToEndSimple(b *testing.B) {
|
|
|
|
const count = 100000
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
b.StopTimer() // begin setup
|
|
|
|
r := setup(b)
|
|
|
|
client := NewClient(r)
|
|
|
|
bg := NewBackground(r, &Config{
|
|
|
|
Concurrency: 10,
|
|
|
|
RetryDelayFunc: func(n int, err error, t *Task) time.Duration {
|
|
|
|
return time.Second
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// Create a bunch of tasks
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
t := Task{Type: fmt.Sprintf("task%d", i), Payload: Payload{"data": i}}
|
2020-01-04 12:24:18 +08:00
|
|
|
client.Schedule(&t, time.Now())
|
2020-01-01 04:36:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(count)
|
|
|
|
handler := func(t *Task) error {
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b.StartTimer() // end setup
|
|
|
|
|
|
|
|
bg.start(HandlerFunc(handler))
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
b.StopTimer() // begin teardown
|
|
|
|
bg.stop()
|
|
|
|
b.StartTimer() // end teardown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// E2E benchmark with scheduled tasks and retries.
|
|
|
|
func BenchmarkEndToEnd(b *testing.B) {
|
|
|
|
const count = 100000
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
b.StopTimer() // begin setup
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
r := setup(b)
|
|
|
|
client := NewClient(r)
|
|
|
|
bg := NewBackground(r, &Config{
|
|
|
|
Concurrency: 10,
|
|
|
|
RetryDelayFunc: func(n int, err error, t *Task) time.Duration {
|
|
|
|
return time.Second
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// Create a bunch of tasks
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
t := Task{Type: fmt.Sprintf("task%d", i), Payload: Payload{"data": i}}
|
2020-01-04 12:24:18 +08:00
|
|
|
client.Schedule(&t, time.Now())
|
2020-01-01 04:36:46 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
t := Task{Type: fmt.Sprintf("scheduled%d", i), Payload: Payload{"data": i}}
|
2020-01-04 12:24:18 +08:00
|
|
|
client.Schedule(&t, time.Now().Add(time.Second))
|
2020-01-01 04:36:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(count * 2)
|
|
|
|
handler := func(t *Task) error {
|
|
|
|
// randomly fail 1% of tasks
|
|
|
|
if rand.Intn(100) == 1 {
|
|
|
|
return fmt.Errorf(":(")
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b.StartTimer() // end setup
|
|
|
|
|
|
|
|
bg.start(HandlerFunc(handler))
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
b.StopTimer() // begin teardown
|
|
|
|
bg.stop()
|
|
|
|
b.StartTimer() // end teardown
|
|
|
|
}
|
|
|
|
}
|