mimir icon indicating copy to clipboard operation
mimir copied to clipboard

frontend: use `mimirpb.Sample` custom JSON encoder func

Open ortuman opened this issue 1 year ago • 0 comments

Signed-off-by: Miguel Ángel Ortuño [email protected]

What this PR does

Use custom mimir.Sample JSON serializer hook to minimize allocation when encoding prometheus response in frontend component. This is functionally equivalent and about ~50% faster in my testing.

This behavior has been inspired/copied from Prometheus, but applying it to mimir.Sample instead of promql.Point.

The following commands can be run to verify the benchmark results:

Previous

Comment this line to avoid registering the hook and run the following command:

go test -cpu 1 -count=10 -run=XXX -benchmem -bench=BenchmarkPrometheusCodec_EncodeResponse ./pkg/frontend/querymiddleware | tee old.txt

Current

Uncomment the previous line and run:

go test -cpu 1 -count=10 -run=XXX -benchmem -bench=BenchmarkPrometheusCodec_EncodeResponse ./pkg/frontend/querymiddleware | tee new.txt

Comparison:

benchstat old.txt new.txt
name                            old time/op    new time/op    delta
PrometheusCodec_EncodeResponse     190ms ± 1%      93ms ± 1%  -51.16%  (p=0.000 n=9+8)

name                            old alloc/op   new alloc/op   delta
PrometheusCodec_EncodeResponse     201MB ± 2%     100MB ±23%  -50.08%  (p=0.000 n=8+10)

name                            old allocs/op  new allocs/op  delta
PrometheusCodec_EncodeResponse     2.01M ± 0%     0.01M ± 0%  -99.50%  (p=0.000 n=9+10)

Which issue(s) this PR fixes or relates to

Fixes N/A

Checklist

  • [X] Tests updated
  • [ ] Documentation added
  • [x] CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

ortuman avatar Jul 18 '22 14:07 ortuman