Skip to content

Commit

Permalink
Add config generation benchmark (#6180)
Browse files Browse the repository at this point in the history
(cherry picked from commit b3bb590)

# Conflicts:
#	internal/pkg/agent/application/coordinator/coordinator_test.go
  • Loading branch information
swiatekm authored and mergify[bot] committed Dec 9, 2024
1 parent 96f2b9f commit 0011acb
Show file tree
Hide file tree
Showing 3 changed files with 83,434 additions and 4 deletions.
58 changes: 54 additions & 4 deletions internal/pkg/agent/application/coordinator/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ import (
"testing"
"time"

<<<<<<< HEAD
"google.golang.org/protobuf/types/known/structpb"
=======
"gopkg.in/yaml.v3"

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

otelmanager "github.com/elastic/elastic-agent/internal/pkg/otel/manager"
>>>>>>> b3bb590324 (Add config generation benchmark (#6180))

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -80,7 +88,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 @@ -836,6 +844,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 @@ -865,7 +915,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 @@ -922,7 +972,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 @@ -1152,7 +1202,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 0011acb

Please sign in to comment.