-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OneCollector] Add benchmark project (#1088)
- Loading branch information
1 parent
5f587d4
commit 8f8d153
Showing
5 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
...Telemetry.Exporter.OneCollector.Benchmarks/LogRecordCommonSchemaJsonHttpPostBenchmarks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// <copyright file="LogRecordCommonSchemaJsonHttpPostBenchmarks.cs" company="OpenTelemetry Authors"> | ||
// 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 | ||
// | ||
// http://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. | ||
// </copyright> | ||
|
||
using System.Diagnostics; | ||
using System.Net; | ||
using System.Reflection; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry.Logs; | ||
|
||
namespace OpenTelemetry.Exporter.OneCollector.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
public class LogRecordCommonSchemaJsonHttpPostBenchmarks | ||
{ | ||
private static readonly MethodInfo LogRecordSetScopeProviderMethodInfo = typeof(LogRecord).GetProperty("ScopeProvider", BindingFlags.Instance | BindingFlags.NonPublic)?.SetMethod | ||
?? throw new InvalidOperationException("LogRecord.ScopeProvider.Set could not be found reflectively."); | ||
|
||
private LogRecord[]? logRecords; | ||
private OneCollectorExporter<LogRecord>? exporter; | ||
|
||
[Params(1, 10, 100)] | ||
public int NumberOfBatches { get; set; } | ||
|
||
[Params(1000, 10_000)] | ||
public int NumberOfLogRecordsPerBatch { get; set; } | ||
|
||
[Params(true, false)] | ||
public bool EnableCompression { get; set; } | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
this.logRecords = new LogRecord[this.NumberOfLogRecordsPerBatch]; | ||
|
||
for (int i = 0; i < this.NumberOfLogRecordsPerBatch; i++) | ||
{ | ||
this.logRecords[i] = CreateLogRecord(i); | ||
} | ||
|
||
var exporterOptions = new OneCollectorLogExporterOptions | ||
{ | ||
ConnectionString = "InstrumentationKey=token-extrainformation", | ||
}; | ||
|
||
exporterOptions.Validate(); | ||
|
||
var transportOptions = exporterOptions.TransportOptions; | ||
|
||
transportOptions.HttpCompression = this.EnableCompression | ||
? OneCollectorExporterHttpTransportCompressionType.Deflate | ||
: OneCollectorExporterHttpTransportCompressionType.None; | ||
|
||
var sink = new WriteDirectlyToTransportSink<LogRecord>( | ||
new LogRecordCommonSchemaJsonSerializer( | ||
new EventNameManager(exporterOptions.DefaultEventNamespace, exporterOptions.DefaultEventName), | ||
exporterOptions.TenantToken!, | ||
exporterOptions.SerializationOptions.ExceptionStackTraceHandling, | ||
transportOptions.MaxPayloadSizeInBytes == -1 ? int.MaxValue : transportOptions.MaxPayloadSizeInBytes, | ||
transportOptions.MaxNumberOfItemsPerPayload == -1 ? int.MaxValue : transportOptions.MaxNumberOfItemsPerPayload), | ||
new HttpJsonPostTransport( | ||
exporterOptions.InstrumentationKey!, | ||
transportOptions.Endpoint, | ||
transportOptions.HttpCompression, | ||
new NoopHttpClient())); | ||
|
||
this.exporter = new OneCollectorExporter<LogRecord>(sink); | ||
} | ||
|
||
[GlobalCleanup] | ||
public void GlobalCleanup() | ||
{ | ||
this.exporter?.Dispose(); | ||
} | ||
|
||
[Benchmark] | ||
public void Export() | ||
{ | ||
for (int i = 0; i < this.NumberOfBatches; i++) | ||
{ | ||
this.exporter!.Export(new Batch<LogRecord>(this.logRecords!, this.logRecords!.Length)); | ||
} | ||
} | ||
|
||
private static LogRecord CreateLogRecord(int index) | ||
{ | ||
var logRecord = (LogRecord)Activator.CreateInstance(typeof(LogRecord), nonPublic: true)!; | ||
|
||
logRecord.Timestamp = DateTime.UtcNow; | ||
logRecord.CategoryName = typeof(LogRecordCommonSchemaJsonHttpPostBenchmarks).FullName; | ||
logRecord.LogLevel = LogLevel.Information; | ||
|
||
if (index % 2 == 0) | ||
{ | ||
if (index % 4 == 0) | ||
{ | ||
logRecord.EventId = new EventId(2, "MyEvent"); | ||
} | ||
else | ||
{ | ||
logRecord.EventId = new EventId(1); | ||
} | ||
|
||
logRecord.StateValues = new List<KeyValuePair<string, object?>> | ||
{ | ||
new KeyValuePair<string, object?>("userId", 18), | ||
new KeyValuePair<string, object?>("greeting", "hello world"), | ||
new KeyValuePair<string, object?>("{OriginalFormat}", "Structured logging {userId} {greeting}"), | ||
}; | ||
|
||
if (index % 3 == 0) | ||
{ | ||
var scopeProvider = new ScopeProvider( | ||
new List<KeyValuePair<string, object?>> { new KeyValuePair<string, object?>("scope1Key1", "scope1Value1"), new KeyValuePair<string, object?>("scope1Key2", "scope1Value2") }, | ||
new List<KeyValuePair<string, object?>> { new KeyValuePair<string, object?>("scope2Key1", "scope2Value1") }); | ||
|
||
LogRecordSetScopeProviderMethodInfo.Invoke(logRecord, new object[] { scopeProvider }); | ||
} | ||
} | ||
else | ||
{ | ||
logRecord.FormattedMessage = "Non-structured log message"; | ||
} | ||
|
||
if (index % 3 == 0) | ||
{ | ||
logRecord.TraceId = ActivityTraceId.CreateRandom(); | ||
logRecord.SpanId = ActivitySpanId.CreateRandom(); | ||
|
||
if (index % 6 == 0) | ||
{ | ||
logRecord.TraceFlags = ActivityTraceFlags.None; | ||
} | ||
else | ||
{ | ||
logRecord.TraceFlags = ActivityTraceFlags.Recorded; | ||
} | ||
} | ||
|
||
if (index % 9 == 0) | ||
{ | ||
logRecord.Exception = new InvalidOperationException(); | ||
} | ||
|
||
return logRecord; | ||
} | ||
|
||
private sealed class NoopHttpClient : IHttpClient | ||
{ | ||
public HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
return new HttpResponseMessage | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
}; | ||
} | ||
} | ||
|
||
private sealed class ScopeProvider : IExternalScopeProvider | ||
{ | ||
private readonly List<KeyValuePair<string, object?>>[] scopes; | ||
|
||
public ScopeProvider(params List<KeyValuePair<string, object?>>[] scopes) | ||
{ | ||
this.scopes = scopes; | ||
} | ||
|
||
public void ForEachScope<TState>(Action<object, TState> callback, TState state) | ||
{ | ||
foreach (var scope in this.scopes) | ||
{ | ||
callback(scope, state); | ||
} | ||
} | ||
|
||
public IDisposable Push(object state) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ry.Exporter.OneCollector.Benchmarks/OpenTelemetry.Exporter.OneCollector.Benchmarks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<Description>Benchmark project for OpenTelemetry .NET OneCollectorExporter.</Description> | ||
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. --> | ||
<TargetFrameworks>net7.0;net6.0</TargetFrameworks> | ||
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net48;net472;net471;net47;net462</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
<EnableAnalysis>true</EnableAnalysis> | ||
<ImplicitUsings>true</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="$(BenchmarkDotNetPkgVer)" /> | ||
<PackageReference Include="System.Net.Http" Version="$(SystemNetHttp)" Condition="'$(TargetFramework)' != 'net6.0' AND '$(TargetFramework)' != 'net7.0'" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.OneCollector\OpenTelemetry.Exporter.OneCollector.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
49 changes: 49 additions & 0 deletions
49
test/OpenTelemetry.Exporter.OneCollector.Benchmarks/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// <copyright file="Program.cs" company="OpenTelemetry Authors"> | ||
// 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 | ||
// | ||
// http://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. | ||
// </copyright> | ||
|
||
using System.Diagnostics; | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace OpenTelemetry.Exporter.OneCollector.Benchmarks; | ||
|
||
internal static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
if (Debugger.IsAttached) | ||
{ | ||
// Note: This block is so you can start the project with debugger | ||
// attached and step through the code. It is helpful when working on | ||
// it. | ||
var benchmarks = new LogRecordCommonSchemaJsonHttpPostBenchmarks | ||
{ | ||
NumberOfBatches = 10_000, | ||
NumberOfLogRecordsPerBatch = 1000, | ||
EnableCompression = true, | ||
}; | ||
|
||
benchmarks.GlobalSetup(); | ||
|
||
benchmarks.Export(); | ||
|
||
benchmarks.GlobalCleanup(); | ||
} | ||
else | ||
{ | ||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); | ||
} | ||
} | ||
} |