Skip to content

Commit

Permalink
feat: clean
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed Oct 4, 2023
1 parent 4c1cbd9 commit 37b0c51
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 241 deletions.
22 changes: 3 additions & 19 deletions tests/benchmarks/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,17 @@ tasks:
cmds:
- xk6 build --with extension=$(pwd)/extension --with github.com/grafana/[email protected]

run:v1:
run:
deps:
# - tools
- tools
- build:scripts
- build:k6
cmds:
- PGPASSWORD=ledger psql -h 127.0.0.1 -U ledger -c "DROP DATABASE IF EXISTS ledgerv1;"
- PGPASSWORD=ledger psql -h 127.0.0.1 -U ledger -c "CREATE DATABASE ledgerv1;"
- TEST_ID=$(date +%s)-v1 K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM=true K6_PROMETHEUS_RW_SERVER_URL=http://127.0.0.1:9090/api/v1/write ./k6 run --summary-trend-stats="avg,min,med,max,p(90),p(95),p(99)" --out xk6-prometheus-rw scripts/dist/ledger-v1.js
env:
POSTGRES_DSN: postgresql://ledger:ledger@postgres:5432/ledgerv1?sslmode=disable
OTLP_ENDPOINT: otel:4317
DOCKER_NETWORK: tools_benchmarks

run:v2:
deps:
# - tools
- build:scripts
- build:k6
cmds:
# - PGPASSWORD=ledger psql -h 127.0.0.1 -U ledger -c "DROP DATABASE IF EXISTS ledgerv2;"
- PGPASSWORD=ledger psql -h 127.0.0.1 -U ledger -c "DROP DATABASE IF EXISTS ledgerv2;"
- PGPASSWORD=ledger psql -h 127.0.0.1 -U ledger -c "CREATE DATABASE ledgerv2;"
- TEST_ID=$(date +%s)-v2 K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM=true K6_PROMETHEUS_RW_SERVER_URL=http://127.0.0.1:9090/api/v1/write ./k6 run --summary-trend-stats="avg,min,med,max,p(90),p(95),p(99)" --out xk6-prometheus-rw scripts/dist/ledger-v2.js
#- TEST_ID=$(date +%s)-v2 ./k6 run --summary-trend-stats="avg,min,med,max,p(90),p(95),p(99)" scripts/dist/ledger-v2.js
env:
POSTGRES_DSN: postgresql://ledger:ledger@postgres:5432/ledgerv2?sslmode=disable
OTLP_ENDPOINT: otel:4317
DOCKER_NETWORK: tools_benchmarks

cleanup:
Expand Down
164 changes: 10 additions & 154 deletions tests/benchmarks/extension/extension.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package extension

import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"time"

"github.com/ory/dockertest/v3"
"github.com/google/uuid"
_ "github.com/lib/pq"
dockertest "github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/modules"
"golang.org/x/mod/semver"
"net/http"
"os"
)

type OTLP struct {
Endpoint string `json:"endpoint"`
}

type LedgerConfiguration struct {
OTLP OTLP `json:"otlp"`
Version string `json:"version"`
PostgresDSN string `json:"postgresDSN"`
Network string `json:"network"`
Expand All @@ -38,13 +28,13 @@ func (c *LedgerConfiguration) resolve() error {
if c.TestID == "" {
c.TestID = os.Getenv("TEST_ID")
if c.TestID == "" {
return errors.New("missing test id")
c.TestID = uuid.NewString()
}
}
if c.Version == "" {
c.Version = os.Getenv("LEDGER_VERSION")
if c.Version == "" {
return errors.New("missing ledger version")
c.Version = "latest"
}
}
if c.PostgresDSN == "" {
Expand All @@ -53,12 +43,6 @@ func (c *LedgerConfiguration) resolve() error {
return errors.New("missing postgres dsn")
}
}
if c.OTLP.Endpoint == "" {
c.OTLP.Endpoint = os.Getenv("OTLP_ENDPOINT")
if c.OTLP.Endpoint == "" {
return errors.New("missing otlp endpoint")
}
}
if c.Network == "" {
c.Network = os.Getenv("DOCKER_NETWORK")
}
Expand All @@ -85,7 +69,7 @@ func (c *Extension) StartLedger(configuration LedgerConfiguration) *Ledger {

defer func() {
if e := recover(); e != nil {
if c.pool != nil {
if c.pool != nil && c.resource != nil {
if err := c.pool.Purge(c.resource); err != nil {
logger.Errorf("unable to clean docker resource: %s", err)
}
Expand All @@ -105,13 +89,6 @@ func (c *Extension) StartLedger(configuration LedgerConfiguration) *Ledger {
panic(err)
}

var envVars []string
if semver.IsValid(configuration.Version) && semver.Compare(configuration.Version, "v2.0.0") < 0 {
envVars = v1EnvVars(configuration.TestID, configuration)
} else {
envVars = v2EnvVars(configuration.TestID, configuration)
}

var networkID string
if configuration.Network != "" {
logger.Infof("Searching network named '%s'...", configuration.Network)
Expand All @@ -134,7 +111,7 @@ func (c *Extension) StartLedger(configuration LedgerConfiguration) *Ledger {
Name: fmt.Sprintf("ledger-%s", configuration.TestID),
Repository: "ghcr.io/formancehq/ledger",
Tag: configuration.Version,
Env: envVars,
Env: envVars(configuration),
NetworkID: networkID,
ExposedPorts: []string{"3068/tcp"},
}, options...)
Expand Down Expand Up @@ -186,134 +163,13 @@ func (c *Extension) StopLedger() {
c.logger.Infof("Ledger stopped!")
}

func (c *Extension) ExportResults() {
//client, err := api.NewClient(api.Config{
// Address: "http://localhost:9090", // TODO: Use env var
//})
//if err != nil {
// panic(err)
//}
//
//<-time.After(5 * time.Second) // TODO: Check if the delay is enough
//
//v1api := v1.NewAPI(client)
//ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
//defer cancel()
//
//labels, warnings, err := v1api.Series(ctx, []string{fmt.Sprintf(`{testID="%s"}`, c.testID)}, time.Now().Add(-3*time.Hour), time.Now())
//if err != nil {
// panic(err)
//}
//if len(warnings) > 0 {
// c.logger.Warnf("Warnings: %v", warnings)
//}
//
//visitedLabel := map[model.LabelValue]struct{}{}
//
//if err := os.MkdirAll(filepath.Join(".", "results"), 0755); err != nil {
// panic(err)
//}
//
//f, err := os.Create(filepath.Join("results", fmt.Sprintf("%s.txt", c.testID)))
//if err != nil {
// panic(err)
//}
//
//enc := json.NewEncoder(f)
//
//for _, labelSet := range labels {
// name := labelSet["__name__"]
// _, alreadyVisited := visitedLabel[name]
// if alreadyVisited {
// continue
// }
// visitedLabel[name] = struct{}{}
//
// timeSeries, warnings, err := v1api.Query(ctx, fmt.Sprintf(`%s{testID="%s"}[1h]`, name, c.testID), time.Time{}, v1.WithTimeout(5*time.Second))
// if err != nil {
// panic(err)
// }
// if len(warnings) > 0 {
// c.logger.Warnf("Warnings: %v", warnings)
// }
//
// if err := enc.Encode(timeSeries); err != nil {
// panic(err)
// }
//}
}

func (c *Extension) CheckPrometheusQuery(query string, queryResponse int64) {
c.logger.Infof("Check query in Prometheus...")
client, err := api.NewClient(api.Config{
Address: "http://127.0.0.1:9090",
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
os.Exit(1)
}

v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

r := v1.Range{
Start: time.Now().Add(-time.Hour),
End: time.Now(),
Step: time.Minute,
}

promResult, warnings, err := v1api.QueryRange(ctx, query, r, v1.WithTimeout(5*time.Second))
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
os.Exit(1)
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}

value, _ := strconv.ParseInt(promResult.String(), 10, 64)
if value == queryResponse {
c.logger.Infof("Prometheus query not respond as expected")
}
c.logger.Infof("Result Query:\n%v\n", promResult.String())
}

func v1EnvVars(testID string, configuration LedgerConfiguration) []string {
return []string{
"NUMARY_SERVER_HTTP_BIND_ADDRESS=:3068",
"NUMARY_STORAGE_DRIVER=postgres",
"NUMARY_STORAGE_POSTGRES_CONN_STRING=" + configuration.PostgresDSN,
"NUMARY_OTEL_RESOURCE_ATTRIBUTES=testid=" + testID,
"NUMARY_OTEL_TRACES=true",
"NUMARY_OTEL_TRACES_EXPORTER=otlp",
"NUMARY_OTEL_TRACES_EXPORTER_OTLP_ENDPOINT=" + configuration.OTLP.Endpoint,
"NUMARY_OTEL_TRACES_EXPORTER_OTLP_INSECURE=true",
"NUMARY_OTEL_METRICS=true",
"NUMARY_OTEL_METRICS_EXPORTER=otlp",
"NUMARY_OTEL_METRICS_EXPORTER_OTLP_ENDPOINT=" + configuration.OTLP.Endpoint,
"NUMARY_OTEL_METRICS_EXPORTER_OTLP_INSECURE=true",
}
}

func v2EnvVars(testID string, configuration LedgerConfiguration) []string {
func envVars(configuration LedgerConfiguration) []string {
return []string{
"BIND=:3068",
"STORAGE_DRIVER=postgres",
"STORAGE_POSTGRES_CONN_STRING=" + configuration.PostgresDSN,
"STORAGE_POSTGRES_MAX_OPEN_CONNS=50",
"STORAGE_POSTGRES_MAX_IDLE_CONNS=50",
//"OTEL_RESOURCE_ATTRIBUTES=testid=" + testID,
//"OTEL_TRACES=true",
//"OTEL_TRACES_EXPORTER=otlp",
//"OTEL_TRACES_EXPORTER_OTLP_ENDPOINT=" + configuration.OTLP.Endpoint,
//"OTEL_TRACES_EXPORTER_OTLP_INSECURE=true",
//"OTEL_METRICS=true",
//"OTEL_METRICS_EXPORTER=otlp",
//"OTEL_METRICS_EXPORTER_OTLP_ENDPOINT=" + configuration.OTLP.Endpoint,
//"OTEL_METRICS_EXPORTER_OTLP_INSECURE=true",
//"OTEL_METRICS_RUNTIME=true",
//"OTEL_METRICS_EXPORTER_PUSH_INTERVAL=1s",
"DEBUG=false",
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/benchmarks/extension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/formancehq/stack/tests/benchmarks/extension
go 1.20

require (
github.com/google/uuid v1.3.0
github.com/ory/dockertest/v3 v3.10.0
github.com/prometheus/client_golang v1.14.1-0.20221122130035-8b6e68085b10
github.com/sirupsen/logrus v1.9.3
Expand All @@ -14,7 +15,7 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/containerd/continuity v0.4.1 // indirect
github.com/dlclark/regexp2 v1.9.0 // indirect
github.com/docker/cli v24.0.4+incompatible // indirect
Expand Down
6 changes: 4 additions & 2 deletions tests/benchmarks/extension/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down Expand Up @@ -172,6 +172,8 @@ github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8q
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand Down
37 changes: 0 additions & 37 deletions tests/benchmarks/scripts/benchs/ledger-v1.js

This file was deleted.

13 changes: 6 additions & 7 deletions tests/benchmarks/scripts/benchs/ledger-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ import {ReadAccounts} from "../src/steps/account";
import {ReadBalances} from "../src/steps/balances";
import {ReadAggregateBalances} from "../src/steps/aggregateBalances";
import {K6Options} from "../src/options";
import {startLedger, stopLedger, exportResults} from 'k6/x/formancehq/benchmarks';
import {startLedger, stopLedger} from 'k6/x/formancehq/benchmarks';
import exec from 'k6/execution';

export function setup() {
return startLedger({
version: '15a430c40e95d38d864599dece235bc3964a3588',
version: 'latest',
});
}

export let options = K6Options();

const ledgerName = `/tests`;

export function read_transactions(ledger) {
export function readTransactions(ledger) {
const url = ledger.url + ledgerName
ReadTransactions(url);
}
export function read_accounts(ledger) {
export function readAccounts(ledger) {
const url = ledger.url + ledgerName
ReadAccounts(url);
}
export function read_balances(ledger) {
export function readBalances(ledger) {
const url = ledger.url + ledgerName
ReadBalances(url);
}
export function read_aggregatebalances(ledger) {
export function readAggregatedBalances(ledger) {
const url = ledger.url + ledgerName
ReadAggregateBalances(url);
}
Expand All @@ -41,5 +41,4 @@ export function write(ledger) {

export function teardown(data) {
stopLedger();
exportResults();
}
Loading

0 comments on commit 37b0c51

Please sign in to comment.