feat: Initial release
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
37d5c2b1f7
commit
d1e3da5189
13 changed files with 442 additions and 0 deletions
53
benchmark.go
Normal file
53
benchmark.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Benchmark to store benchmark state
|
||||
type Benchmark struct {
|
||||
duration time.Duration
|
||||
connections int
|
||||
databases []*Database
|
||||
}
|
||||
|
||||
// NewBenchmark connects to the database then creates a Benchmark struct
|
||||
func NewBenchmark(connections int, duration time.Duration, driver string, dsn string, query string, reconnect bool) (*Benchmark, error) {
|
||||
var databases []*Database
|
||||
for i := 0; i < connections; i++ {
|
||||
database, err := NewDatabase(driver, dsn, query, reconnect)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
databases = append(databases, database)
|
||||
}
|
||||
return &Benchmark{
|
||||
duration: duration,
|
||||
connections: connections,
|
||||
databases: databases,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Run performs the benchmark by runing queries for a duration
|
||||
func (b *Benchmark) Run() {
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(b.connections)
|
||||
for _, database := range b.databases {
|
||||
go database.Run(b.duration, wg)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// Queries returns the number of executed queries during the benchmark
|
||||
func (b *Benchmark) Queries() (queries float64) {
|
||||
for _, database := range b.databases {
|
||||
queries = queries + database.Queries()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// QueriesPerSecond returns the number of executed queries per second during the benchmark
|
||||
func (b *Benchmark) QueriesPerSecond() float64 {
|
||||
return b.Queries() / b.duration.Seconds()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue