Skip to content

Commit

Permalink
feat: make benchmarks compiled binary not embedding the full server
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 24, 2024
1 parent 9158511 commit f170ccc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 52 deletions.
2 changes: 1 addition & 1 deletion test/performance/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ IMPORT github.com/formancehq/earthly:tags/v0.16.2 AS core
run:
LOCALLY
ARG args=""
RUN go test -bench="Write" -run ^$ -tags it -report.file ./report/report.json -timeout 60m $args
RUN go test -bench="Write" -run ^$ -tags it,local -report.file ./report/report.json -timeout 60m $args

generate-graphs:
FROM core+base-image
Expand Down
5 changes: 1 addition & 4 deletions test/performance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ The benchmarks also test the minimal set of features and the full set of feature

Refer to [features](../../CONTRIBUTING.md/#features) for more information about features.

Three types of script are actually tested:
* world->bank : A transaction from `@world` to `@bank`
* world->any : A transaction from `@world` to `@dst:<iteration>`
* any(unbounded)->any : A transaction from `@src:<iteration>` to `@dst:<iteration>`
Scripts can be found in directory [scripts](./scripts).

## Run locally

Expand Down
9 changes: 0 additions & 9 deletions test/performance/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ import (
ledger "github.com/formancehq/ledger/internal"
)

type TransactionExecutor interface {
ExecuteScript(context.Context, string, map[string]string) (*ledger.Transaction, error)
}
type TransactionExecutorFn func(context.Context, string, map[string]string) (*ledger.Transaction, error)

func (fn TransactionExecutorFn) ExecuteScript(ctx context.Context, script string, vars map[string]string) (*ledger.Transaction, error) {
return fn(ctx, script, vars)
}

type Env interface {
Client() *ledgerclient.Formance
URL() string
Expand Down
29 changes: 22 additions & 7 deletions test/performance/env_testserver_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
//go:build it
//go:build it && local

package performance_test

import (
"context"
"github.com/formancehq/go-libs/v2/logging"
"github.com/formancehq/go-libs/v2/otlp/otlpmetrics"
"github.com/formancehq/go-libs/v2/testing/docker"
ledgerclient "github.com/formancehq/ledger/pkg/client"
"io"
"os"
"sync"
"testing"

"github.com/formancehq/go-libs/v2/pointer"
Expand Down Expand Up @@ -40,12 +43,22 @@ func (e *TestServerEnv) Stop(ctx context.Context) error {
var _ Env = (*TestServerEnv)(nil)

type TestServerEnvFactory struct {
pgServer *pgtesting.PostgresServer
dockerPool *docker.Pool

once sync.Once
}

func (f *TestServerEnvFactory) Create(ctx context.Context, b *testing.B, ledger ledger.Ledger) Env {

db := f.pgServer.NewDatabase(b)
f.once.Do(func() {
// Configure the environment to run benchmarks locally.
// Start a docker connection
f.dockerPool = docker.NewPool(b, logging.Testing())
})

pgServer := pgtesting.CreatePostgresServer(b, f.dockerPool, pgtesting.WithPGCrypto())

db := pgServer.NewDatabase(b)
b.Logf("database: %s", db.Name())
connectionOptions := db.ConnectionOptions()
connectionOptions.MaxOpenConns = 100
Expand Down Expand Up @@ -89,8 +102,10 @@ func (f *TestServerEnvFactory) Create(ctx context.Context, b *testing.B, ledger

var _ EnvFactory = (*TestServerEnvFactory)(nil)

func NewTestServerEnvFactory(pgServer *pgtesting.PostgresServer) *TestServerEnvFactory {
return &TestServerEnvFactory{
pgServer: pgServer,
}
func NewTestServerEnvFactory() *TestServerEnvFactory {
return &TestServerEnvFactory{}
}

func init() {
envFactory = NewTestServerEnvFactory()
}
39 changes: 8 additions & 31 deletions test/performance/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"context"
"crypto/tls"
"flag"
"github.com/formancehq/go-libs/v2/logging"
"github.com/formancehq/go-libs/v2/testing/docker"
"github.com/formancehq/go-libs/v2/testing/platform/pgtesting"
. "github.com/formancehq/go-libs/v2/testing/utils"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
Expand All @@ -17,9 +14,6 @@ import (
)

var (
dockerPool *docker.Pool
pgServer *Deferred[*pgtesting.PostgresServer]

authClientID string
authClientSecret string

Expand Down Expand Up @@ -50,14 +44,7 @@ func TestMain(m *testing.M) {
flag.Parse()

WithTestMain(func(t *TestingTForMain) int {
selectedEnv := 0
if stackURL != "" {
selectedEnv++
}
if ledgerURL != "" {
selectedEnv++
}
if selectedEnv > 1 {
if stackURL != "" && ledgerURL != "" {
t.Errorf("Cannot specify both --stack.url and --ledger.url")
t.FailNow()
}
Expand All @@ -67,23 +54,13 @@ func TestMain(m *testing.M) {
envFactory = NewRemoteLedgerEnvFactory(getHttpClient(stackURL+"/api/auth"), stackURL+"/api/ledger")
case ledgerURL != "":
envFactory = NewRemoteLedgerEnvFactory(getHttpClient(authIssuerURL), ledgerURL)
default:
// Configure the environment to run benchmarks locally.
// Start a docker connection and create a new postgres server.
dockerPool = docker.NewPool(t, logging.Testing())

pgServer = NewDeferred[*pgtesting.PostgresServer]()
pgServer.LoadAsync(func() *pgtesting.PostgresServer {
return pgtesting.CreatePostgresServer(
t,
dockerPool,
pgtesting.WithPGCrypto(),
)
})

Wait(pgServer)

envFactory = NewTestServerEnvFactory(pgServer.GetValue())
}

testing.Verbose()

if envFactory == nil {
t.Errorf("no env selected, you need to specify either --stack.url or --ledger.url\n")
t.FailNow()
}

return m.Run()
Expand Down

0 comments on commit f170ccc

Please sign in to comment.