ApplicationInsights-dotnet icon indicating copy to clipboard operation
ApplicationInsights-dotnet copied to clipboard

Improve channel performance by serializing telemetry directly to stream to be used in Transmission

Open cijothomas opened this issue 6 years ago • 5 comments

The JsonSerializer used to serialize Telemetry currently uses a MemoryStream to write serialized data to. This is then converted to a byte[], and stored inside Transmission. https://github.com/Microsoft/ApplicationInsights-dotnet/blob/develop/src/Microsoft.ApplicationInsights/Extensibility/Implementation/JsonSerializer.cs#L64

This byte[] is then converted again to a new MemoryStream, for writing to network. https://github.com/Microsoft/ApplicationInsights-dotnet/blob/develop/src/Microsoft.ApplicationInsights/Channel/Transmission.cs#L165

Proposing to avoid this conversions back and forth - modify JsonSerializer to write to just a MemoryStream and modify Transmission to use the same stream to write to network. This should avoid unwanted memory allocations/GC etc and boost performance.

cijothomas avatar Jan 16 '19 17:01 cijothomas

@cijothomas , based on the milestones mentioned above, can we assume that v2.14 already fixes that? Thanks

johnib avatar Sep 02 '20 10:09 johnib

The milestone for this is "Future", means this is not done.

cijothomas avatar Sep 02 '20 14:09 cijothomas

This also leads to GC pressure when used in server applications. We are currently going through a process of optimising memory allocations to reduces numbers of GCs and are now at the point that all our own memory allocations are dwarfed by Application Insights allocations. Some screen shots from JetBrains dotMemory. This is after just logging like 1000 events.

System.Byte[] allocations: image image image

System.Char[] allocations: image

msvinth avatar Nov 13 '21 11:11 msvinth

This issue is stale because it has been open 300 days with no activity. Remove stale label or this will be closed in 7 days. Commenting will instruct the bot to automatically remove the label.

github-actions[bot] avatar Sep 13 '22 00:09 github-actions[bot]

Looks like links are broken, but the usage of MemoryStream is still there. https://github.com/microsoft/ApplicationInsights-dotnet/blob/19a3ecf77bebd7bbcffb6ca553b920016a412ed4/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/JsonSerializer.cs#L53

https://github.com/microsoft/ApplicationInsights-dotnet/blob/19a3ecf77bebd7bbcffb6ca553b920016a412ed4/BASE/src/Microsoft.ApplicationInsights/Channel/Transmission.cs#L175

Should I try to fix this ?

TrayanZapryanov avatar Sep 13 '22 08:09 TrayanZapryanov

This issue is stale because it has been open 300 days with no activity. Remove stale label or this will be closed in 7 days. Commenting will instruct the bot to automatically remove the label.

github-actions[bot] avatar Jul 12 '23 00:07 github-actions[bot]