gtq/taskqueue_test.go

62 lines
1.1 KiB
Go
Raw Normal View History

2019-04-11 14:21:27 +00:00
package gtq
import (
"runtime"
"sync"
"testing"
"time"
)
var numTestPrios = 10
var tq *TaskQueue
func testTQAdd(n int) {
workers := runtime.GOMAXPROCS(0)
var wg sync.WaitGroup
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
for i := 0; i < n; i++ {
// simplest possible task
tq.Add(func() {
time.Sleep(0)
}, uint(i%numTestPrios))
}
wg.Done()
}()
}
wg.Wait()
}
func TestTaskQueueAdd(t *testing.T) {
tq = NewTaskQueue(0)
testTQAdd(1000000)
expected := 1000000 * runtime.GOMAXPROCS(0)
added := tq.Length()
if uint(expected) != added {
t.Error("expected to add", expected, "but added", added)
} else {
t.Log("added", added)
}
}
func BenchmarkSimpleScheduler(b *testing.B) {
2019-04-11 14:21:27 +00:00
tq = NewTaskQueue(0)
testTQAdd(b.N / (runtime.GOMAXPROCS(0)))
tq.Start(SimpleScheduler)
for tq.Length() > 0 {
time.Sleep(10 * time.Millisecond)
}
tq.Stop()
}
func BenchmarkTimeSliceScheduler(b *testing.B) {
tq = NewTaskQueue(0)
testTQAdd(b.N / (runtime.GOMAXPROCS(0)))
tq.Start(TimeSliceScheduler)
2019-04-11 14:21:27 +00:00
for tq.Length() > 0 {
time.Sleep(10 * time.Millisecond)
}
tq.Stop()
}