Skip to content

Commit

Permalink
chore: make benchmarks not run (the main) when running go test ./... …
Browse files Browse the repository at this point in the history
…on root repo
  • Loading branch information
gfyrag committed Oct 24, 2024
1 parent f170ccc commit a1c41fa
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 93 deletions.
92 changes: 0 additions & 92 deletions test/performance/main_test.go

This file was deleted.

79 changes: 78 additions & 1 deletion test/performance/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,50 @@
package performance_test

import (
"context"
"crypto/tls"
"embed"
"encoding/json"
"flag"
"github.com/formancehq/go-libs/v2/logging"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
)

var scripts = map[string]TransactionProviderFactory{}
var (
authClientID string
authClientSecret string

// targeting a stack
stackURL string

// targeting a ledger
authIssuerURL string
ledgerURL string

parallelism int64
reportFile string

envFactory EnvFactory

scripts = map[string]TransactionProviderFactory{}
)

func init() {
flag.StringVar(&stackURL, "stack.url", "", "Stack URL")
flag.StringVar(&authClientID, "client.id", "", "Client ID")
flag.StringVar(&authClientSecret, "client.secret", "", "Client secret")
flag.StringVar(&ledgerURL, "ledger.url", "", "Ledger url")
flag.StringVar(&authIssuerURL, "auth.url", "", "Auth url (ignored if --stack.url is specified)")
flag.StringVar(&reportFile, "report.file", "", "Location to write report file")
flag.Int64Var(&parallelism, "parallelism", 1, "Parallelism (default 1). Values is multiplied by GOMAXPROCS")
}

//go:embed scripts
var scriptsDir embed.FS
Expand All @@ -39,8 +72,52 @@ func init() {
}
}

func getHttpClient(authUrl string) *http.Client {
httpClient := &http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
MaxConnsPerHost: 100,
MaxIdleConnsPerHost: 100,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
if authClientID != "" {
httpClient = (&clientcredentials.Config{
ClientID: authClientID,
ClientSecret: authClientSecret,
TokenURL: authUrl + "/oauth/token",
Scopes: []string{"ledger:read", "ledger:write"},
}).
Client(context.WithValue(context.Background(), oauth2.HTTPClient, httpClient))
}

return httpClient
}


func BenchmarkWrite(b *testing.B) {

if stackURL != "" && ledgerURL != "" {
b.Errorf("Cannot specify both --stack.url and --ledger.url")
b.FailNow()
}

switch {
case stackURL != "":
envFactory = NewRemoteLedgerEnvFactory(getHttpClient(stackURL+"/api/auth"), stackURL+"/api/ledger")
case ledgerURL != "":
envFactory = NewRemoteLedgerEnvFactory(getHttpClient(authIssuerURL), ledgerURL)
}

testing.Verbose()

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

// Execute benchmarks
reports := New(b, envFactory, scripts).Run(logging.TestingContext())

Expand Down

0 comments on commit a1c41fa

Please sign in to comment.