Brijesh's Git Server — toolkit @ 07f7705d52a615e47c3440f848a64abd85da22b1

my attempt at building my own web framework so I feel more confident when using chi

benchmark/benchmark.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
package benchmark

import (
	"fmt"
	"time"
)

func BenchmarkFunction(fn func(), count int, rounds int) {
	var totalDuration time.Duration

	for r := 0; r < rounds; r++ {
		start := time.Now()

		for i := 0; i < count; i++ {
			fn()
		}

		duration := time.Since(start)
		totalDuration += duration
	}

	averageDuration := totalDuration / time.Duration(rounds)
	fmt.Printf("\nAverage time over %d rounds: %v\n", rounds, averageDuration)
	fmt.Printf("Average time per operation: %v\n", averageDuration/time.Duration(count))
}