Skip to content

Commit

Permalink
feat(benchmarks): build transaction provider in each benchmark routin…
Browse files Browse the repository at this point in the history
…e to keep a context
  • Loading branch information
gfyrag committed Oct 24, 2024
1 parent a842805 commit efae577
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
25 changes: 22 additions & 3 deletions test/performance/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,25 @@ func (fn TransactionProviderFn) Get(iteration int) (string, map[string]string) {
return fn(iteration)
}

type TransactionProviderFactory interface {
Create() TransactionProvider
}

type TransactionProviderFactoryFn func() TransactionProvider

func (fn TransactionProviderFactoryFn) Create() TransactionProvider {
return fn()
}

type TransparentTransactionProviderFactory TransactionProviderFn

func (f TransparentTransactionProviderFactory) Create() TransactionProvider {
return TransactionProviderFn(f)
}

type Benchmark struct {
EnvFactory EnvFactory
Scenarios map[string]TransactionProvider
Scenarios map[string]TransactionProviderFactory

reports map[string]map[string]*report
b *testing.B
Expand Down Expand Up @@ -71,10 +87,13 @@ func (benchmark *Benchmark) Run(ctx context.Context) map[string][]Result {
b.SetParallelism(int(parallelism))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {

transactionProvider := benchmark.Scenarios[scenario].Create()

for pb.Next() {
iteration := int(cpt.Add(1))

script, vars := benchmark.Scenarios[scenario].Get(iteration)
script, vars := transactionProvider.Get(iteration)
now := time.Now()

_, err := benchmark.createTransaction(ctx, env.Client(), l, script, vars)
Expand Down Expand Up @@ -173,7 +192,7 @@ func (benchmark *Benchmark) createTransaction(
}, nil
}

func New(b *testing.B, envFactory EnvFactory, scenarios map[string]TransactionProvider) *Benchmark {
func New(b *testing.B, envFactory EnvFactory, scenarios map[string]TransactionProviderFactory) *Benchmark {
return &Benchmark{
b: b,
EnvFactory: envFactory,
Expand Down
10 changes: 5 additions & 5 deletions test/performance/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"testing"
)

var scripts = map[string]TransactionProvider{
"world->bank": TransactionProviderFn(worldToBank),
"world->any": TransactionProviderFn(worldToAny),
"any(unbounded)->any": TransactionProviderFn(anyUnboundedToAny),
"any(bounded)->any": TransactionProviderFn(anyBoundedToAny),
var scripts = map[string]TransactionProviderFactory{
"world->bank": TransparentTransactionProviderFactory(worldToBank),
"world->any": TransparentTransactionProviderFactory(worldToAny),
"any(unbounded)->any": TransparentTransactionProviderFactory(anyUnboundedToAny),
"any(bounded)->any": TransparentTransactionProviderFactory(anyBoundedToAny),
}

func worldToBank(_ int) (string, map[string]string) {
Expand Down

0 comments on commit efae577

Please sign in to comment.