chore: improve /healthz
endpoint performance
#2014
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
👋 team. First and foremost: thank you for open sourcing and maintaining this project, it's been really useful. Today while working on an unrelated issue I started to investigate the
/healthz
endpoint in Atlantis and found that it could be improved in both speed and memory allocations.The first thing I did was adding a benchmark to have a baseline for comparing my changes:
Then I implemented my improvements. I noticed that the response body is always the following:
However, I noticed the body was built by defining an inline struct and calling
json.MarshalIndent
on it, checking if there was a marshaling error which should never happen, so I removed all of that code and simply define a variable with the expected body. The benchmarks results already show improvements:Lastly, I realized there was no need to always initialize this variable, so I moved it to a private package variable and ran the benchmarks again:
Most of all of the allocations were removed and the speedup is ~90%! In all steps I actually generated more benchmarks results using the following command:
With this I was able to later run
benchstat
to compare the results:Anyway, I hope you find this useful and worthy of contribution! 😸