Optimize perfs and allocations
tbolon opened this issue · 3 comments
Hi,
We are using AppMetrics to send metrics to InfluxDB.
We have observed some places where performances can be enhanced (mostly regarding allocations).
I have created a branch to add benchmarks on this project (with the same structure as benchmarks in AppMetrics project). It will be used to detect any regression.
You can browse the changes in my fork.
For now, we have identified: (checked items are already fixed in my branch)
-
allocations in MetricSnapshotInfluxDbLineProtocolWriter. Dictionary are created for each data point and linq is used to merge field names and values. We can easily get rid of any allocation by keeping values passed as parameters.
-
allocations in InfluxDbMetricsReporter.FlushAsync. data are written to a memory stream then converted as a string to be written to the http request. We could certainly at least pass directly the stream to the WriteAsync method and prevent a string allocation.
I have kept all previous classes to generate baseline benchmarks.
I tried to made these changes not too disturbing, and prevented code duplication when possible.
Our plan is to reduce further the cost of writing points to prevent as much as possible garbage collection. I hope you will find these change useful. If so, I will create a PR so you could review the changes in detail.
Regards,
@tbolon sorry for the late reply, and thanks for taking the time.
Could you create a pull request please
I have created a PR and updated the issue.
All tests are green, and I will update the issue with a copy of the benchmark results soon.
Edit: you will have to wait tomorrow, since my laptop is not really benchmark friendly (my cpu is throttled) :(