Skip to content

Commit

Permalink
[chore] Add benchmark for new XML crud functions (open-telemetry#35493)
Browse files Browse the repository at this point in the history
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 open-telemetry#35471
  • Loading branch information
djaglowski authored and jriguera committed Oct 4, 2024
1 parent 93d89ca commit 6432cb0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,3 +1094,37 @@ func fillSpanOne(span ptrace.Span) {
span.SetSpanID(spanID)
span.SetTraceID(traceID)
}

func Benchmark_XML_Functions(b *testing.B) {
testXML := `<Data><From><Test>1</Test><Test>2</Test></From><To></To></Data>`
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 "<Test>1</Test><Test>2</Test>"
// 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)))
}

0 comments on commit 6432cb0

Please sign in to comment.