2021-03-06 07:20:56 +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.
|
|
|
|
|
|
|
|
package rdb
|
|
|
|
|
|
|
|
import (
|
2021-11-16 08:34:26 +08:00
|
|
|
"context"
|
2021-03-06 07:20:56 +08:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hibiken/asynq/internal/asynqtest"
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkEnqueue(b *testing.B) {
|
|
|
|
r := setup(b)
|
2021-11-16 08:34:26 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
msg := asynqtest.NewTaskMessage("task1", nil)
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
b.StartTimer()
|
|
|
|
|
2021-11-16 08:34:26 +08:00
|
|
|
if err := r.Enqueue(ctx, msg); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Enqueue failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEnqueueUnique(b *testing.B) {
|
|
|
|
r := setup(b)
|
2021-11-16 08:34:26 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
msg := &base.TaskMessage{
|
|
|
|
Type: "task1",
|
|
|
|
Payload: nil,
|
|
|
|
Queue: base.DefaultQueueName,
|
|
|
|
UniqueKey: base.UniqueKey("default", "task1", nil),
|
|
|
|
}
|
|
|
|
uniqueTTL := 5 * time.Minute
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
b.StartTimer()
|
|
|
|
|
2021-11-16 08:34:26 +08:00
|
|
|
if err := r.EnqueueUnique(ctx, msg, uniqueTTL); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("EnqueueUnique failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSchedule(b *testing.B) {
|
|
|
|
r := setup(b)
|
2021-11-16 08:34:26 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
msg := asynqtest.NewTaskMessage("task1", nil)
|
|
|
|
processAt := time.Now().Add(3 * time.Minute)
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
b.StartTimer()
|
|
|
|
|
2021-11-16 08:34:26 +08:00
|
|
|
if err := r.Schedule(ctx, msg, processAt); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Schedule failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkScheduleUnique(b *testing.B) {
|
|
|
|
r := setup(b)
|
2021-11-16 08:34:26 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
msg := &base.TaskMessage{
|
|
|
|
Type: "task1",
|
|
|
|
Payload: nil,
|
|
|
|
Queue: base.DefaultQueueName,
|
|
|
|
UniqueKey: base.UniqueKey("default", "task1", nil),
|
|
|
|
}
|
|
|
|
processAt := time.Now().Add(3 * time.Minute)
|
|
|
|
uniqueTTL := 5 * time.Minute
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
b.StartTimer()
|
|
|
|
|
2021-11-16 08:34:26 +08:00
|
|
|
if err := r.ScheduleUnique(ctx, msg, processAt, uniqueTTL); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("EnqueueUnique failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDequeueSingleQueue(b *testing.B) {
|
|
|
|
r := setup(b)
|
2021-11-16 08:34:26 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := asynqtest.NewTaskMessageWithQueue(
|
|
|
|
fmt.Sprintf("task%d", i), nil, base.DefaultQueueName)
|
2021-11-16 08:34:26 +08:00
|
|
|
if err := r.Enqueue(ctx, m); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Enqueue failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
|
2022-02-14 06:34:38 +08:00
|
|
|
if _, _, err := r.Dequeue(base.DefaultQueueName); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Dequeue failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDequeueMultipleQueues(b *testing.B) {
|
|
|
|
qnames := []string{"critical", "default", "low"}
|
|
|
|
r := setup(b)
|
2021-11-16 08:34:26 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
for _, qname := range qnames {
|
|
|
|
m := asynqtest.NewTaskMessageWithQueue(
|
|
|
|
fmt.Sprintf("%s_task%d", qname, i), nil, qname)
|
2021-11-16 08:34:26 +08:00
|
|
|
if err := r.Enqueue(ctx, m); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Enqueue failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
|
2022-02-14 06:34:38 +08:00
|
|
|
if _, _, err := r.Dequeue(qnames...); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Dequeue failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDone(b *testing.B) {
|
|
|
|
r := setup(b)
|
|
|
|
m1 := asynqtest.NewTaskMessage("task1", nil)
|
|
|
|
m2 := asynqtest.NewTaskMessage("task2", nil)
|
|
|
|
m3 := asynqtest.NewTaskMessage("task3", nil)
|
|
|
|
msgs := []*base.TaskMessage{m1, m2, m3}
|
|
|
|
zs := []base.Z{
|
|
|
|
{Message: m1, Score: time.Now().Add(10 * time.Second).Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Add(20 * time.Second).Unix()},
|
|
|
|
{Message: m3, Score: time.Now().Add(30 * time.Second).Unix()},
|
|
|
|
}
|
2022-02-14 06:34:38 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
asynqtest.SeedActiveQueue(b, r.client, msgs, base.DefaultQueueName)
|
2022-02-16 22:27:08 +08:00
|
|
|
asynqtest.SeedLease(b, r.client, zs, base.DefaultQueueName)
|
2021-03-06 07:20:56 +08:00
|
|
|
b.StartTimer()
|
|
|
|
|
2022-02-14 06:34:38 +08:00
|
|
|
if err := r.Done(ctx, msgs[0]); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Done failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRetry(b *testing.B) {
|
|
|
|
r := setup(b)
|
|
|
|
m1 := asynqtest.NewTaskMessage("task1", nil)
|
|
|
|
m2 := asynqtest.NewTaskMessage("task2", nil)
|
|
|
|
m3 := asynqtest.NewTaskMessage("task3", nil)
|
|
|
|
msgs := []*base.TaskMessage{m1, m2, m3}
|
|
|
|
zs := []base.Z{
|
|
|
|
{Message: m1, Score: time.Now().Add(10 * time.Second).Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Add(20 * time.Second).Unix()},
|
|
|
|
{Message: m3, Score: time.Now().Add(30 * time.Second).Unix()},
|
|
|
|
}
|
2022-02-14 06:34:38 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
asynqtest.SeedActiveQueue(b, r.client, msgs, base.DefaultQueueName)
|
2022-02-16 22:27:08 +08:00
|
|
|
asynqtest.SeedLease(b, r.client, zs, base.DefaultQueueName)
|
2021-03-06 07:20:56 +08:00
|
|
|
b.StartTimer()
|
|
|
|
|
2022-02-14 06:34:38 +08:00
|
|
|
if err := r.Retry(ctx, msgs[0], time.Now().Add(1*time.Minute), "error", true /*isFailure*/); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Retry failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkArchive(b *testing.B) {
|
|
|
|
r := setup(b)
|
|
|
|
m1 := asynqtest.NewTaskMessage("task1", nil)
|
|
|
|
m2 := asynqtest.NewTaskMessage("task2", nil)
|
|
|
|
m3 := asynqtest.NewTaskMessage("task3", nil)
|
|
|
|
msgs := []*base.TaskMessage{m1, m2, m3}
|
|
|
|
zs := []base.Z{
|
|
|
|
{Message: m1, Score: time.Now().Add(10 * time.Second).Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Add(20 * time.Second).Unix()},
|
|
|
|
{Message: m3, Score: time.Now().Add(30 * time.Second).Unix()},
|
|
|
|
}
|
2022-02-14 06:34:38 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
asynqtest.SeedActiveQueue(b, r.client, msgs, base.DefaultQueueName)
|
2022-02-16 22:27:08 +08:00
|
|
|
asynqtest.SeedLease(b, r.client, zs, base.DefaultQueueName)
|
2021-03-06 07:20:56 +08:00
|
|
|
b.StartTimer()
|
|
|
|
|
2022-02-14 06:34:38 +08:00
|
|
|
if err := r.Archive(ctx, msgs[0], "error"); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Archive failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRequeue(b *testing.B) {
|
|
|
|
r := setup(b)
|
|
|
|
m1 := asynqtest.NewTaskMessage("task1", nil)
|
|
|
|
m2 := asynqtest.NewTaskMessage("task2", nil)
|
|
|
|
m3 := asynqtest.NewTaskMessage("task3", nil)
|
|
|
|
msgs := []*base.TaskMessage{m1, m2, m3}
|
|
|
|
zs := []base.Z{
|
|
|
|
{Message: m1, Score: time.Now().Add(10 * time.Second).Unix()},
|
|
|
|
{Message: m2, Score: time.Now().Add(20 * time.Second).Unix()},
|
|
|
|
{Message: m3, Score: time.Now().Add(30 * time.Second).Unix()},
|
|
|
|
}
|
2022-02-14 06:34:38 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 07:20:56 +08:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
asynqtest.SeedActiveQueue(b, r.client, msgs, base.DefaultQueueName)
|
2022-02-16 22:27:08 +08:00
|
|
|
asynqtest.SeedLease(b, r.client, zs, base.DefaultQueueName)
|
2021-03-06 07:20:56 +08:00
|
|
|
b.StartTimer()
|
|
|
|
|
2022-02-14 06:34:38 +08:00
|
|
|
if err := r.Requeue(ctx, msgs[0]); err != nil {
|
2021-03-06 07:20:56 +08:00
|
|
|
b.Fatalf("Requeue failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCheckAndEnqueue(b *testing.B) {
|
|
|
|
r := setup(b)
|
|
|
|
now := time.Now()
|
|
|
|
var zs []base.Z
|
|
|
|
for i := -100; i < 100; i++ {
|
|
|
|
msg := asynqtest.NewTaskMessage(fmt.Sprintf("task%d", i), nil)
|
|
|
|
score := now.Add(time.Duration(i) * time.Second).Unix()
|
|
|
|
zs = append(zs, base.Z{Message: msg, Score: score})
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
asynqtest.FlushDB(b, r.client)
|
|
|
|
asynqtest.SeedScheduledQueue(b, r.client, zs, base.DefaultQueueName)
|
|
|
|
b.StartTimer()
|
|
|
|
|
2021-03-13 08:23:08 +08:00
|
|
|
if err := r.ForwardIfReady(base.DefaultQueueName); err != nil {
|
|
|
|
b.Fatalf("ForwardIfReady failed: %v", err)
|
2021-03-06 07:20:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|