Skip to content

Commit

Permalink
Add config generation benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Dec 2, 2024
1 parent 4123e5e commit d132329
Show file tree
Hide file tree
Showing 3 changed files with 83,430 additions and 4 deletions.
54 changes: 50 additions & 4 deletions internal/pkg/agent/application/coordinator/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"testing"
"time"

"gopkg.in/yaml.v3"

"github.com/elastic/elastic-agent-libs/mapstr"

otelmanager "github.com/elastic/elastic-agent/internal/pkg/otel/manager"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/status"
Expand Down Expand Up @@ -83,7 +87,7 @@ var (
// returns true, up to the given timeout duration, and reports a test failure
// if it doesn't arrive.
func waitForState(
t *testing.T,
t testing.TB,
stateChan chan State,
stateCallback func(State) bool,
timeout time.Duration,
Expand Down Expand Up @@ -915,6 +919,48 @@ func TestCoordinator_UpgradeDetails(t *testing.T) {
require.Equal(t, expectedErr.Error(), coord.state.UpgradeDetails.Metadata.ErrorMsg)
}

func BenchmarkCoordinator_generateComponentModel(b *testing.B) {
// load variables
varsMaps := []map[string]any{}
varsMapsBytes, err := os.ReadFile("./testdata/variables.yaml")
require.NoError(b, err)
err = yaml.Unmarshal(varsMapsBytes, &varsMaps)
require.NoError(b, err)
vars := make([]*transpiler.Vars, len(varsMaps))
for i, vm := range varsMaps {
vars[i], err = transpiler.NewVars(fmt.Sprintf("%d", i), vm, mapstr.M{})
require.NoError(b, err)
}

// load config
cfgMap := map[string]any{}
cfgMapBytes, err := os.ReadFile("./testdata/config.yaml")
require.NoError(b, err)
err = yaml.Unmarshal(cfgMapBytes, &cfgMap)
require.NoError(b, err)
cfg, err := config.NewConfigFrom(cfgMap)
require.NoError(b, err)
cfgMap, err = cfg.ToMapStr()
require.NoError(b, err)
cfgAst, err := transpiler.NewAST(cfgMap)
require.NoError(b, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

coord, _, _ := createCoordinator(b, ctx)

coord.ast = cfgAst
coord.vars = vars

b.ResetTimer()
for i := 0; i < b.N; i++ {
err = coord.generateComponentModel()
require.NoError(b, err)
}

}

type createCoordinatorOpts struct {
managed bool
upgradeManager UpgradeManager
Expand Down Expand Up @@ -944,7 +990,7 @@ func WithComponentInputSpec(spec component.InputSpec) CoordinatorOpt {
// createCoordinator creates a coordinator that using a fake config manager and a fake vars manager.
//
// The runtime specifications is set up to use the fake component.
func createCoordinator(t *testing.T, ctx context.Context, opts ...CoordinatorOpt) (*Coordinator, *fakeConfigManager, *fakeVarsManager) {
func createCoordinator(t testing.TB, ctx context.Context, opts ...CoordinatorOpt) (*Coordinator, *fakeConfigManager, *fakeVarsManager) {
t.Helper()

o := &createCoordinatorOpts{
Expand Down Expand Up @@ -1002,7 +1048,7 @@ func getComponentState(states []runtime.ComponentComponentState, componentID str
return nil
}

func newErrorLogger(t *testing.T) *logger.Logger {
func newErrorLogger(t testing.TB) *logger.Logger {
t.Helper()

loggerCfg := logger.DefaultLoggingConfig()
Expand Down Expand Up @@ -1271,7 +1317,7 @@ func (r *fakeRuntimeManager) PerformComponentDiagnostics(_ context.Context, _ []
return nil, nil
}

func testBinary(t *testing.T, name string) string {
func testBinary(t testing.TB, name string) string {
t.Helper()

var err error
Expand Down
Loading

0 comments on commit d132329

Please sign in to comment.