Skip to content
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

JSON Serialization Performance #2541

Open
perhapsmaple opened this issue Feb 18, 2024 · 4 comments
Open

JSON Serialization Performance #2541

perhapsmaple opened this issue Feb 18, 2024 · 4 comments
Labels
help wanted Good for taking. Extra help will be provided by maintainers Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@perhapsmaple
Copy link
Contributor

I have been exploring the use of different json libraries to optimize the performance of my custom pipeline and exporter to write traces and logs to files. My primary concern was serialization speed, which the OtlpHttpExporter was struggling with while logging extensively. While the nlohmann::json library is intuitive and easy to use, it's not very fast for serializing. This is particularly evident when writing a lot of logs. The switch to the rapidjson library brought a significant 40-50% performance improvement to my logging system.

In order to benchmark the serialization impact, I modified the OtlpHttpClient to immediately return ExportResult::kSuccess after converting the proto message to json and dumping it to a string. I then modified example_otlp_http with the following:

...
constexpr uint64_t kMaxIterations = 1000000;
...

InitTracer();
 
auto start = std::chrono::high_resolution_clock::now();
 
for (uint64_t i = 0; i < kMaxIterations; i++)
  foo_library();
 
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
std::cout << "BM_JSON: " << duration.count() << " milliseconds" << std::endl;

CleanupTracer();

Result for nlohmann::json implementation: 127145 milliseconds
Result for rapidjson implementation : 82763 milliseconds

The code is available at: https://github.com/perhapsmaple/opentelemetry-cpp/tree/json-benchmark
Not final - I think some more changes could be made to make it a little bit more efficient

I think this is an easy avenue for improvement, and we should consider benchmarking more thoroughly with both libraries. Happy to hear your thoughts and feedback.

@github-actions github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Feb 18, 2024
@ThomsonTan ThomsonTan added triage/accepted Indicates an issue or PR is ready to be actively worked on. help wanted Good for taking. Extra help will be provided by maintainers and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Feb 21, 2024
Copy link

This issue is available for anyone to work on. Make sure to reference this issue in your pull request.
✨ Thank you for your contribution! ✨

@dufferzafar
Copy link

I don't think we'd end up depending on Boost libs, but just for the sake of it, I wanted to mention Boost::JSON as the natural "fast & clean" json library: https://230.jsondocs.prtest.cppalliance.org/libs/json/doc/html/json/benchmarks.html

@lalitb
Copy link
Member

lalitb commented Feb 27, 2024

One option could be for otel SDK to provide an abstract interface for JSON serialization, with default implementation for nlohmann-json. And let user bring their custom implementation which could internally use rapid JSON or Boost::JSON. This would be similar to how we provide HTTPClientFactory, with default implementation for curl.

Copy link

This issue was marked as stale due to lack of activity.

@github-actions github-actions bot added the Stale label Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Good for taking. Extra help will be provided by maintainers Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

4 participants