-
Notifications
You must be signed in to change notification settings - Fork 1k
perf(otlp-transformer): optimize toAnyValue with pre-allocated arrays #6287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,16 +64,24 @@ export function toAnyValue(value: unknown): IAnyValue { | |
| } | ||
| if (t === 'boolean') return { boolValue: value as boolean }; | ||
| if (value instanceof Uint8Array) return { bytesValue: value }; | ||
| if (Array.isArray(value)) | ||
| return { arrayValue: { values: value.map(toAnyValue) } }; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although v8 is normally great at handling I'm pretty sure the performance benefit from the object if case is just removing the usage of |
||
| if (t === 'object' && value != null) | ||
| return { | ||
| kvlistValue: { | ||
| values: Object.entries(value as object).map(([k, v]) => | ||
| toKeyValue(k, v) | ||
| ), | ||
| }, | ||
| }; | ||
| if (Array.isArray(value)) { | ||
| const values: IAnyValue[] = new Array(value.length); | ||
| for (let i = 0; i < value.length; i++) { | ||
| values[i] = toAnyValue(value[i]); | ||
| } | ||
| return { arrayValue: { values } }; | ||
| } | ||
| if (t === 'object' && value != null) { | ||
| const keys = Object.keys(value); | ||
| const values: IKeyValue[] = new Array(keys.length); | ||
| for (let i = 0; i < keys.length; i++) { | ||
| values[i] = { | ||
| key: keys[i], | ||
| value: toAnyValue((value as Record<string, unknown>)[keys[i]]), | ||
| }; | ||
| } | ||
| return { kvlistValue: { values } }; | ||
| } | ||
|
|
||
| return {}; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| const Benchmark = require('benchmark'); | ||
| const { toAnyValue } = require('../../../build/src/common/internal'); | ||
|
|
||
| const suite = new Benchmark.Suite(); | ||
|
|
||
| suite.on('cycle', event => { | ||
| console.log(String(event.target)); | ||
| }); | ||
|
|
||
| suite.add('toAnyValue string', function () { | ||
| toAnyValue('test-string-value'); | ||
| }); | ||
|
|
||
| suite.add('toAnyValue integer', function () { | ||
| toAnyValue(42); | ||
| }); | ||
|
|
||
| suite.add('toAnyValue array of strings', function () { | ||
| toAnyValue(['a', 'b', 'c', 'd', 'e']); | ||
| }); | ||
|
|
||
| suite.add('toAnyValue array of objects', function () { | ||
| toAnyValue([{ a: 1 }, { b: 2 }, { c: 3 }]); | ||
| }); | ||
|
|
||
| suite.add('toAnyValue nested object', function () { | ||
| toAnyValue({ level1: { level2: { level3: 'deep' } } }); | ||
| }); | ||
|
|
||
| suite.add('toAnyValue complex nested', function () { | ||
| toAnyValue({ | ||
| a: { b: { c: { d: 'value' } } }, | ||
| arr: [1, 2, 3], | ||
| mixed: { nested: [{ x: 1 }, { y: 2 }] }, | ||
| }); | ||
| }); | ||
|
|
||
| suite.run(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| const Benchmark = require('benchmark'); | ||
| const { | ||
| createExportTraceServiceRequest, | ||
| } = require('../../../build/src/trace/internal'); | ||
| const { BasicTracerProvider } = require('@opentelemetry/sdk-trace-base'); | ||
| const { ProtobufTraceSerializer } = require('../../../build/src'); | ||
|
|
||
| const tracerProvider = new BasicTracerProvider(); | ||
| const tracer = tracerProvider.getTracer('test'); | ||
|
|
||
| const suite = new Benchmark.Suite(); | ||
|
|
||
| const span = createSpan(); | ||
| const spans = []; | ||
| for (let i = 0; i < 100; i++) { | ||
| spans.push(createSpan()); | ||
| } | ||
|
|
||
| suite.on('cycle', event => { | ||
| console.log(String(event.target)); | ||
| }); | ||
|
|
||
| suite.add('transform 1 span', function () { | ||
| createExportTraceServiceRequest([span]); | ||
| }); | ||
|
|
||
| suite.add('transform 100 spans', function () { | ||
| createExportTraceServiceRequest(spans); | ||
| }); | ||
|
|
||
| suite.add('transform 100 spans to protobuf', function () { | ||
| ProtobufTraceSerializer.serializeRequest(spans); | ||
| }); | ||
|
|
||
| suite.run(); | ||
|
|
||
| function createSpan() { | ||
| const span = tracer.startSpan('span'); | ||
| span.setAttribute('aaaaaaaaaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaaaa'); | ||
| span.setAttribute('bbbbbbbbbbbbbbbbbbbb', 'bbbbbbbbbbbbbbbbbbbb'); | ||
| span.setAttribute('cccccccccccccccccccc', 'cccccccccccccccccccc'); | ||
| span.setAttribute('dddddddddddddddddddd', 'dddddddddddddddddddd'); | ||
| span.setAttribute('eeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeee'); | ||
| span.setAttribute('ffffffffffffffffffff', 'ffffffffffffffffffff'); | ||
| span.setAttribute('gggggggggggggggggggg', 'gggggggggggggggggggg'); | ||
| span.setAttribute('hhhhhhhhhhhhhhhhhhhh', 'hhhhhhhhhhhhhhhhhhhh'); | ||
| span.setAttribute('iiiiiiiiiiiiiiiiiiii', 'iiiiiiiiiiiiiiiiiiii'); | ||
| span.setAttribute('jjjjjjjjjjjjjjjjjjjj', 'jjjjjjjjjjjjjjjjjjjj'); | ||
| span.end(); | ||
|
|
||
| return span; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please move this to
experimental/CHANGELOG.md