benchmark

package module
v0.0.0-...-2c76c52 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2019 License: MIT Imports: 7 Imported by: 0

README

Build Status gocover.run

Benchmark

A Go library to get benchmarks of a function (execution time). All requests/executions are done in parallel (Go routines).

How to use?

Any function with the signature fn() error can be used to run the benchmark. Please refer to the example provided in the test file or you can refer Godoc.

Sample output:

Benchmark run for HTTP GET request to https://kamaleshwar.com.

Duration              : 1s 
Total requests        : 750 
Wait time per request : 1.333333ms 
Show progess          : true , per 75 request(s) 
Start                 : 2017-04-25 03:56:18.917402373 +0530 IST


75  out of  750  done.  Success: 74  Errors: 0
150  out of  750  done.  Success: 149  Errors: 0
225  out of  750  done.  Success: 224  Errors: 0
300  out of  750  done.  Success: 299  Errors: 0
375  out of  750  done.  Success: 374  Errors: 0
450  out of  750  done.  Success: 449  Errors: 0
525  out of  750  done.  Success: 524  Errors: 0
600  out of  750  done.  Success: 599  Errors: 0
675  out of  750  done.  Success: 674  Errors: 0
750  out of  750  done.  Success: 748  Errors: 0

========================= Benchmark stats =========================
 
Done               : 2017-04-25 03:56:29.019980966 +0530 IST 
Time to complete   : 10.1025787s 
Total requests     : 750 
Requests completed : 750 
Success            : 748 (99.73333333333333%) 
Errors             : 2 (0.26666666666666666%)

Average time per successful request : 3.291541039s 
Fastest                             : 256.852232ms 
Slowest                             : 7.255390376s

Average time per failed request : 10.004558347s 
Fastest                         : 10.003726326s 
Slowest                         : 10.005390368s


Error messages (1)

 1. Get https://kamaleshwar.com: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
  Occurrences: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Benchmark

type Benchmark struct {

	//TotalRequests is the total number of requests to be fired.
	TotalRequests uint64
	//BenchDuration is the duration over which all the requests are fired, in milliseconds.
	BenchDuration uint64
	//WaitPerReq is the duration to wait before firing the consecutive request
	WaitPerReq time.Duration
	//ShowProgress is set to true if it should display current stat when the benchmark is running/in progress.
	ShowProgress bool

	//StatReqCount is the number of requests processed after which progress statistics is printed
	StatReqCount uint64
	// contains filtered or unexported fields
}

Benchmark holds all the parameters required to run the benchmark and respective methods

Example
package main

func calc(limit int) {
	// var by3 []int
	// var by5 []int
	// var by8 []int

	// by3 := make([]int, (limit/3)+1)
	// by5 := make([]int, (limit/5)+1)
	// by8 := make([]int, (limit/8)+1)

	by3 := make([]int, 0, (limit/3)+1)
	by5 := make([]int, 0, (limit/5)+1)
	by8 := make([]int, 0, (limit/8)+1)

	// by3 := make([]interface{}, (limit/3)+1)
	// by5 := make([]interface{}, (limit/5)+1)
	// by8 := make([]interface{}, (limit/8)+1)

	// by3 := make([]interface{}, 0, (limit/3)+1)
	// by5 := make([]interface{}, 0, (limit/5)+1)
	// by8 := make([]interface{}, 0, (limit/8)+1)

	// var by3 []interface{}
	// var by5 []interface{}
	// var by8 []interface{}

	for i := 0; i < limit; i++ {
		if i%3 == 0 {
			by3 = append(by3, i)
		}
		if i%5 == 0 {
			by5 = append(by5, i)
		}
		if i%8 == 0 {
			by8 = append(by8, i)
		}
	}

	// fmt.Println("By3:", len(by3), "\nby5:", len(by5), "\nby8:", len(by8))
}

func test() error {
	count, limit := 100, 999999
	for i := 0; i < count; i++ {
		calc(limit)
	}
	return nil
}

func main() {
	// Returns a new Benchmark pointer with all the defaults assigned
	b := New()
	// time to wait before firing the consequent request
	// WaitPerReq = time.Millisecond * 1
	// print available stats while the benchmark is running
	b.ShowProgress = true
	// Total number of requests to fire
	b.TotalRequests = 10
	// Duration in which all the requests have to be finished firing (in milliseconds).
	b.BenchDuration = 7300

	// Updates all the necessary fields according to the configuration provided
	b.Init()
	// Run the benchmark
	b.Run(test)
}
Output:

func New

func New() *Benchmark

New returns a new Benchmark pointer with the default values set

func (*Benchmark) Done

func (bA *Benchmark) Done(doneTime time.Duration, err error)

Done does all the operations & calculation required while ending a function call

func (*Benchmark) Final

func (bA *Benchmark) Final()

Final prints all the statistics of the benchmark The input to Final() is the time when you start the benchmark

func (*Benchmark) Init

func (bA *Benchmark) Init()

Init initializes all the fields with their respective values

func (*Benchmark) PrintStat

func (bA *Benchmark) PrintStat()

PrintStat prints the stats available based on the given input params and the global variable values

func (*Benchmark) Run

func (bA *Benchmark) Run(fn func() error)

Run runs the benchmark for the given function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL