package internal

import (
	"time"
)

const runningTime = 3 * time.Second

// SingleThreadTester 单线程测试f在固定时间内执行次数。返回执行函数f的次数和时间
func SingleThreadTester(f func()) (int, time.Duration) {
	var start, end time.Time
	var count int

	timer := time.NewTimer(runningTime)
	start = time.Now()
loop:
	for {
		select {
		case <-timer.C:
			end = time.Now()
			break loop
		default:
			f()
			count++
		}
	}
	return count, end.Sub(start)
}

func Rate(count int, d time.Duration) float64 {
	return float64(count) / d.Seconds()
}
