From 2797fa05f2e14b909ae9c59855c692ac033ed44b Mon Sep 17 00:00:00 2001 From: Daniel Jaglowski Date: Tue, 1 Oct 2024 12:01:48 -0400 Subject: [PATCH] [chore] Add benchmark for new XML crud functions (#35493) The benchmark performs a round trip of operations, getting values from the document, inserting them elsewhere, then removing them, and ultimately overwriting the original value with the "new" (same) value. Resolves #35471 --- pkg/ottl/e2e/e2e_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pkg/ottl/e2e/e2e_test.go b/pkg/ottl/e2e/e2e_test.go index d1294b22bd96..592332e76585 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -1094,3 +1094,37 @@ func fillSpanOne(span ptrace.Span) { span.SetSpanID(spanID) span.SetTraceID(traceID) } + +func Benchmark_XML_Functions(b *testing.B) { + testXML := `12` + tCtxWithTestBody := func() ottllog.TransformContext { + resource := pcommon.NewResource() + scope := pcommon.NewInstrumentationScope() + logRecord := plog.NewLogRecord() + logRecord.Body().SetStr(testXML) + return ottllog.NewTransformContext(logRecord, scope, resource, plog.NewScopeLogs(), plog.NewResourceLogs()) + } + + settings := componenttest.NewNopTelemetrySettings() + logParser, err := ottllog.NewParser(ottlfuncs.StandardFuncs[ottllog.TransformContext](), settings) + assert.NoError(b, err) + + // Use a round trip composition to ensure each iteration of the benchmark is the same. + // GetXML(body, "/Data/From/Test") returns "12" + // InsertXML(body, "/Data/To", GetXML(...)) adds the two Test elements to the To element + // RemoveXML(InsertXML(...) "/Data/To/Test") removes the Test elements which were just added + // set overwrites the body, but the result should be the same as the original body + roundTrip := `set(body, RemoveXML(InsertXML(body, "/Data/To", GetXML(body, "/Data/From/Test")), "/Data/To/Test"))` + logStatements, err := logParser.ParseStatement(roundTrip) + assert.NoError(b, err) + + actualCtx := tCtxWithTestBody() + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _, _ = logStatements.Execute(context.Background(), actualCtx) + } + + // Ensure correctness + assert.NoError(b, plogtest.CompareResourceLogs(newResourceLogs(tCtxWithTestBody()), newResourceLogs(actualCtx))) +}