1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-26 22:45:43 -05:00
mumble-discord-bridge/test/timing_test.go

198 lines
4.2 KiB
Go
Raw Normal View History

2021-04-15 00:43:12 -04:00
package main
import (
"fmt"
2021-04-18 17:17:16 -04:00
"math"
2021-04-15 00:43:12 -04:00
"math/rand"
2021-04-18 17:17:16 -04:00
"sort"
2021-04-15 00:43:12 -04:00
"sync"
"testing"
"time"
2021-04-19 22:07:36 -04:00
"github.com/stieneee/mumble-discord-bridge/pkg/sleepct"
2021-04-19 22:07:36 -04:00
"github.com/stieneee/tickerct"
2021-04-15 00:43:12 -04:00
)
const testCount int64 = 1000
2021-04-15 00:43:12 -04:00
const maxSleepInterval time.Duration = 15 * time.Millisecond
const tickerInterval time.Duration = 10 * time.Millisecond
const testDuration time.Duration = time.Duration(testCount * 10 * int64(time.Millisecond))
func testTickerBaseCase(wg *sync.WaitGroup, test *testing.T) {
2021-04-18 17:39:25 -04:00
wg.Add(1)
go func(interval time.Duration) {
now := time.Now()
start := now
// start the ticker
t := time.NewTicker(interval)
var i int64
for i = 0; i < testCount; i++ {
now = <-t.C
// fmt.Println(now)
}
t.Stop()
fmt.Println("Ticker (unloaded) after", testDuration, "drifts", time.Since(start)-testDuration)
wg.Done()
}(tickerInterval)
}
func TestTickerBaseCase(t *testing.T) {
wg := sync.WaitGroup{}
testTickerBaseCase(&wg, t)
2021-04-18 17:39:25 -04:00
wg.Wait()
}
func testTickerLoaded(wg *sync.WaitGroup) {
2021-04-15 00:43:12 -04:00
wg.Add(1)
go func(interval time.Duration) {
now := time.Now()
start := now
// start the ticker
t := time.NewTicker(interval)
var i int64
for i = 0; i < testCount; i++ {
if i+1 < testCount {
time.Sleep(time.Duration(float64(maxSleepInterval) * rand.Float64()))
}
now = <-t.C
// fmt.Println(now)
}
t.Stop()
2021-04-18 17:39:25 -04:00
fmt.Println("Ticker (loaded) after", testDuration, "drifts", time.Since(start)-testDuration)
2021-04-15 00:43:12 -04:00
wg.Done()
}(tickerInterval)
}
func TestTicker(t *testing.T) {
wg := sync.WaitGroup{}
2021-04-18 17:39:25 -04:00
testTickerLoaded(&wg)
2021-04-15 00:43:12 -04:00
wg.Wait()
}
2021-04-18 17:39:25 -04:00
func testTickerCT(wg *sync.WaitGroup) {
2021-04-15 00:43:12 -04:00
wg.Add(1)
go func(interval time.Duration) {
now := time.Now()
start := now
// start the ticker
2021-04-19 22:07:36 -04:00
t := tickerct.NewTickerCT(interval)
2021-04-15 00:43:12 -04:00
var i int64
for i = 0; i < testCount; i++ {
if i+1 < testCount {
time.Sleep(time.Duration(float64(maxSleepInterval) * rand.Float64()))
}
now = <-t.C
// fmt.Println(now)
}
t.Stop()
2021-04-18 17:39:25 -04:00
fmt.Println("TickerCT (loaded) after", testDuration, "drifts", time.Since(start)-testDuration)
2021-04-15 00:43:12 -04:00
wg.Done()
}(tickerInterval)
}
func TestTickerCT(t *testing.T) {
wg := sync.WaitGroup{}
2021-04-18 17:39:25 -04:00
testTickerCT(&wg)
2021-04-15 00:43:12 -04:00
wg.Wait()
}
2021-04-18 00:30:27 -04:00
func testSleepCT(wg *sync.WaitGroup) {
wg.Add(1)
go func(interval time.Duration) {
now := time.Now()
start := now
// start the ticker
s := sleepct.SleepCT{}
s.Start(interval)
2021-04-18 00:30:27 -04:00
var i int64
for i = 0; i < testCount; i++ {
if i+1 < testCount {
time.Sleep(time.Duration(float64(maxSleepInterval) * rand.Float64()))
}
s.SleepNextTarget(false)
2021-04-18 00:30:27 -04:00
}
2021-04-18 17:39:25 -04:00
fmt.Println("SleepCT (loaded) after", testDuration, "drifts", time.Since(start)-testDuration)
2021-04-18 00:30:27 -04:00
wg.Done()
}(tickerInterval)
}
func TestSleepCT(t *testing.T) {
wg := sync.WaitGroup{}
testSleepCT(&wg)
wg.Wait()
}
2021-04-18 17:17:16 -04:00
func testSleepCTPause(wg *sync.WaitGroup) {
wg.Add(1)
go func(interval time.Duration) {
now := time.Now()
start := now
// start the ticker
s := sleepct.SleepCT{}
s.Start(interval)
var i int64
for i = 0; i < testCount; i++ {
if i+1 < testCount {
time.Sleep(time.Duration(float64(maxSleepInterval) * rand.Float64()))
}
s.Notify()
s.SleepNextTarget(true)
}
fmt.Println("SleepCT Pause (loaded) after", testDuration, "drifts", time.Since(start)-testDuration)
wg.Done()
}(tickerInterval)
}
func TestSleepCTPause(t *testing.T) {
wg := sync.WaitGroup{}
testSleepCTPause(&wg)
wg.Wait()
}
2021-04-18 17:17:16 -04:00
func TestIdleJitter(t *testing.T) {
wg := sync.WaitGroup{}
const testSize = 100000
const sleepTarget = time.Millisecond
res := make([]time.Duration, testSize)
for i := 0; i < testSize; i++ {
start := time.Now()
target := start.Add(sleepTarget)
time.Sleep(sleepTarget)
res[i] = time.Since(target)
}
sort.Slice(res, func(i, j int) bool {
return res[i] < res[j]
})
var total float64 = 0
for i := 0; i < testSize; i++ {
total += float64(res[i])
}
avg := time.Duration(total / testSize)
nineFive := int64(math.Round(testSize * 0.95))
nineNine := int64(math.Round(testSize * 0.99))
nineNineNine := int64(math.Round(testSize * 0.999))
fmt.Println("IdleJitter test", testSize, sleepTarget)
fmt.Println("IdleJitter results min/avg/95/99/99.9/max", res[0], avg, res[nineFive], res[nineNine], res[nineNineNine], res[testSize-1])
wg.Wait()
}