Skip to content

Commit

Permalink
feat: add some benchmarking option
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Nov 23, 2024
1 parent d84cc02 commit 7300c7a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 24 deletions.
12 changes: 6 additions & 6 deletions internal/storage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/formancehq/go-libs/v2/collectionutils"
"github.com/formancehq/ledger/pkg/features"
"math/big"
"regexp"
Expand Down Expand Up @@ -254,15 +255,15 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e
return fmt.Errorf("failed to insert transaction: %w", err)
}

for _, address := range tx.InvolvedAccounts() {
err := s.UpsertAccounts(ctx, &ledger.Account{
err = s.UpsertAccounts(ctx, collectionutils.Map(tx.InvolvedAccounts(), func(address string) *ledger.Account {
return &ledger.Account{
Address: address,
FirstUsage: tx.Timestamp,
Metadata: make(metadata.Metadata),
})
if err != nil {
return fmt.Errorf("upserting account: %w", err)
}
})...)
if err != nil {
return fmt.Errorf("upserting accounts: %w", err)
}

if s.ledger.HasFeature(features.FeatureMovesHistory, "ON") {
Expand Down Expand Up @@ -302,7 +303,6 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e
}

if s.ledger.HasFeature(features.FeatureMovesHistoryPostCommitEffectiveVolumes, "SYNC") {
// todo: tx is inserted earlier!
tx.PostCommitEffectiveVolumes = moves.ComputePostCommitEffectiveVolumes()
}
}
Expand Down
58 changes: 53 additions & 5 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/formancehq/ledger/pkg/client/models/operations"
"github.com/google/uuid"
"math/big"
"os"
"path/filepath"
"time"
)

Expand Down Expand Up @@ -185,12 +187,24 @@ func (r Action) Apply(ctx context.Context, client *client.V2, l string) (*Result
return &Result{response.V2BulkResponse.Data[0]}, nil
}

type NextOptions struct {
Globals map[string]any
}

type NextOption func(options *NextOptions)

func WithNextGlobals(globals map[string]any) NextOption {
return func(options *NextOptions) {
options.Globals = globals
}
}

type Generator struct {
next func(int) (*Action, error)
next func(int, ...NextOption) (*Action, error)
}

func (g *Generator) Next(iteration int) (*Action, error) {
return g.next(iteration)
func (g *Generator) Next(iteration int, options ...NextOption) (*Action, error) {
return g.next(iteration, options...)
}

func NewGenerator(script string, opts ...Option) (*Generator, error) {
Expand Down Expand Up @@ -221,14 +235,41 @@ func NewGenerator(script string, opts ...Option) (*Generator, error) {
return nil, err
}

err = runtime.Set("read_file", func(path string) string {
fmt.Println("read file", path)
f, err := os.ReadFile(filepath.Join(cfg.rootPath, path))
if err != nil {
panic(err)
}

return string(f)
})
if err != nil {
return nil, err
}

var next func(int) map[string]any
err = runtime.ExportTo(runtime.Get("next"), &next)
if err != nil {
panic(err)
}

return &Generator{
next: func(i int) (*Action, error) {
next: func(i int, options ...NextOption) (*Action, error) {

nextOptions := NextOptions{}
for _, option := range options {
option(&nextOptions)
}

if nextOptions.Globals != nil {
for k, v := range nextOptions.Globals {
if err := runtime.Set(k, v); err != nil {
return nil, fmt.Errorf("failed to set global variable %s: %w", k, err)
}
}
}

ret := next(i)

var (
Expand Down Expand Up @@ -281,7 +322,8 @@ func NewGenerator(script string, opts ...Option) (*Generator, error) {
}

type config struct {
globals map[string]any
globals map[string]any
rootPath string
}

type Option func(*config)
Expand All @@ -291,3 +333,9 @@ func WithGlobals(globals map[string]any) Option {
c.globals = globals
}
}

func WithRootPath(path string) Option {
return func(c *config) {
c.rootPath = path
}
}
26 changes: 15 additions & 11 deletions test/performance/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
)

type ActionProvider interface {
Get(iteration int) (*generate.Action, error)
Get(globalIteration, iteration int) (*generate.Action, error)
}
type ActionProviderFn func(iteration int) (*generate.Action, error)
type ActionProviderFn func(globalIteration, iteration int) (*generate.Action, error)

func (fn ActionProviderFn) Get(iteration int) (*generate.Action, error) {
return fn(iteration)
func (fn ActionProviderFn) Get(globalIteration, iteration int) (*generate.Action, error) {
return fn(globalIteration, iteration)
}

type ActionProviderFactory interface {
Expand All @@ -36,15 +36,17 @@ func (fn ActionProviderFactoryFn) Create() (ActionProvider, error) {
return fn()
}

func NewJSActionProviderFactory(script string) ActionProviderFactoryFn {
func NewJSActionProviderFactory(rootPath, script string) ActionProviderFactoryFn {
return func() (ActionProvider, error) {
generator, err := generate.NewGenerator(script)
generator, err := generate.NewGenerator(script, generate.WithRootPath(rootPath))
if err != nil {
return nil, err
}

return ActionProviderFn(func(iteration int) (*generate.Action, error) {
return generator.Next(iteration)
return ActionProviderFn(func(globalIteration, iteration int) (*generate.Action, error) {
return generator.Next(iteration, generate.WithNextGlobals(map[string]any{
"iteration": globalIteration,
}))
}), nil
}
}
Expand Down Expand Up @@ -82,7 +84,7 @@ func (benchmark *Benchmark) Run(ctx context.Context) map[string][]Result {
Name: uuid.NewString()[:8],
}

cpt := atomic.Int64{}
globalIteration := atomic.Int64{}

env := envFactory.Create(ctx, b, l)
b.Logf("ledger: %s/%s", l.Bucket, l.Name)
Expand All @@ -94,11 +96,13 @@ func (benchmark *Benchmark) Run(ctx context.Context) map[string][]Result {

actionProvider, err := benchmark.Scenarios[scenario].Create()
require.NoError(b, err)
iteration := atomic.Int64{}

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

action, err := actionProvider.Get(iteration)
action, err := actionProvider.Get(globalIteration, iteration)
require.NoError(b, err)

now := time.Now()
Expand Down
10 changes: 8 additions & 2 deletions test/performance/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,19 @@ func BenchmarkWrite(b *testing.B) {
script, err := scriptsDir.ReadFile(filepath.Join("scripts", entry.Name()))
require.NoError(b, err)

scripts[strings.TrimSuffix(entry.Name(), ".js")] = NewJSActionProviderFactory(string(script))
rootPath, err := filepath.Abs("scripts")
require.NoError(b, err)

scripts[strings.TrimSuffix(entry.Name(), ".js")] = NewJSActionProviderFactory(rootPath, string(script))
}
} else {
file, err := os.ReadFile(scriptFlag)
require.NoError(b, err, "reading file "+scriptFlag)

scripts["provided"] = NewJSActionProviderFactory(string(file))
rootPath, err := filepath.Abs(filepath.Dir(scriptFlag))
require.NoError(b, err)

scripts["provided"] = NewJSActionProviderFactory(rootPath, string(file))
}

if envFactory == nil {
Expand Down

0 comments on commit 7300c7a

Please sign in to comment.