Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions ingest/loadtest/apply_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package loadtest

import (
"context"
_ "embed"
"fmt"
"io"
"os"
Expand All @@ -23,9 +24,18 @@ import (
"github.com/stellar/go-stellar-sdk/xdr"
)

//go:embed configs/default-apply-load.cfg
var defaultConfig []byte

// DefaultConfig returns the contents of the default apply-load configuration
// shipped with this package.
func DefaultConfig() []byte {
return append([]byte(nil), defaultConfig...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can return defaultConfig without creating a copy because I don't think the slice is modified

}

type Options struct {
// inputs
ConfigPath string
Config []byte // raw bytes of config
OutputPath string
FixturesPath string
CoreBinaryPath string // optional, will be looked up in PATH if not set
Expand All @@ -42,8 +52,8 @@ type Results struct {
// ApplyLoad runs stellar-core's apply-load against the supplied config and writes
// benchmark ledgers + fixture entries to OutputPath / FixturesPath.
//
// Required: ConfigPath, OutputPath, FixturesPath. Optional: CoreBinaryPath
// (looked up in PATH), Logger, WorkDirPath (temp dir if unspecified).
// Required: OutputPath, FixturesPath. Optional: CoreBinaryPath (looked up in PATH),
// Logger, WorkDirPath (temp dir if unspecified), Config (defaultConfig if unspecified).
// The supplied config's [HISTORY] commands must publish to a `history/`
// subdirectory of the work dir.
func ApplyLoad(ctx context.Context, opts Options) (Results, error) {
Expand All @@ -60,7 +70,7 @@ func ApplyLoad(ctx context.Context, opts Options) (Results, error) {
}
}()
}
cfg, err := parseConfig(opts.ConfigPath)
cfg, err := parseConfig(opts.Config)
if err != nil {
return Results{}, fmt.Errorf("failed to parse config: %w", err)
}
Expand Down Expand Up @@ -93,11 +103,8 @@ func ApplyLoad(ctx context.Context, opts Options) (Results, error) {

// resolveOptions checks that required options are set and valid, and fills in defaults for optional ones.
func resolveOptions(opts *Options) error {
if opts.ConfigPath == "" {
return fmt.Errorf("configPath is required")
}
if opts.OutputPath == "" || opts.FixturesPath == "" {
return fmt.Errorf("both outputPath and fixturesPath are required")
return fmt.Errorf("outputPath and fixturesPath are required")
}

if opts.CoreBinaryPath == "" {
Expand All @@ -118,9 +125,12 @@ func resolveOptions(opts *Options) error {
if opts.Logger == nil {
opts.Logger = log.New()
}
if len(opts.Config) == 0 {
opts.Config = DefaultConfig()
opts.Logger.Infof("no config provided, using default config")
}

opts.Logger.Infof("Using stellar-core: %s %s", opts.CoreBinaryPath, coreVersion)
opts.Logger.Infof("Using config: %s", opts.ConfigPath)

if opts.WorkDirPath == "" {
var err error
Expand All @@ -136,7 +146,7 @@ func resolveOptions(opts *Options) error {
func run(ctx context.Context, opts Options, cfg applyLoadConfig) (uint32, error) {
// Copy config to work dir (apply-load writes files relative to config location)
destConfigPath := filepath.Join(opts.WorkDirPath, "apply-load.cfg")
if err := copyFile(opts.ConfigPath, destConfigPath); err != nil {
if err := os.WriteFile(destConfigPath, opts.Config, 0o644); err != nil {
return 0, err
}

Expand Down Expand Up @@ -484,15 +494,12 @@ type applyLoadConfig struct {
HistoryArchiveName string
}

func parseConfig(configPath string) (applyLoadConfig, error) {
data, err := os.ReadFile(configPath)
if err != nil {
return applyLoadConfig{}, err
func parseConfig(config []byte) (applyLoadConfig, error) {
if len(config) == 0 {
return applyLoadConfig{}, fmt.Errorf("config is required")
}

var raw map[string]any
err = toml.Unmarshal(data, &raw)
if err != nil {
if err := toml.Unmarshal(config, &raw); err != nil {
return applyLoadConfig{}, err
Comment thread
cjonas9 marked this conversation as resolved.
}

Expand Down Expand Up @@ -549,14 +556,3 @@ func openCheckpointReader(

return checkpointReader, nil
}

func copyFile(src, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
if err := os.WriteFile(dst, data, 0o644); err != nil {
return err
}
return nil
}
13 changes: 4 additions & 9 deletions ingest/loadtest/apply_load_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package loadtest

import (
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -59,7 +57,7 @@ func TestParsePreBenchmarkCheckpoint(t *testing.T) {
// shipped with this package. Updating default-apply-load.cfg without
// updating the expected values here will fail this test.
func TestParseConfig_DefaultCfg(t *testing.T) {
got, err := parseConfig("testdata/default-apply-load.cfg")
got, err := parseConfig(DefaultConfig())
require.NoError(t, err)
assert.Equal(t, applyLoadConfig{
NetworkPassphrase: "load test network",
Expand Down Expand Up @@ -115,10 +113,7 @@ get = "cp history/{0} {1}"
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "apply-load.cfg")
require.NoError(t, os.WriteFile(path, []byte(tt.contents), 0o644))

_, err := parseConfig(path)
_, err := parseConfig([]byte(tt.contents))
require.Error(t, err)
if tt.wantErr != "" {
assert.Contains(t, err.Error(), tt.wantErr)
Expand All @@ -127,7 +122,7 @@ get = "cp history/{0} {1}"
}
}

func TestParseConfig_FileNotFound(t *testing.T) {
_, err := parseConfig(filepath.Join(t.TempDir(), "does-not-exist.cfg"))
func TestParseConfig_NoConfigFound(t *testing.T) {
_, err := parseConfig([]byte{})
require.Error(t, err)
}
Loading