VictoriaMetrics/app/vmalert
Roman Khavronenko 70a94ea492
app/vmalert: update parsing for instant responses (#6859)
This change is made in attempt to reduce memory usage by vmalert when
parsing big instant responses from VM/Prometheus.

In
a5c427bac4
vmalert switched from std json lib to fastjson lib in order to reduce
amount of allocations, as according to highloaded profiles of vmalert
the CPU is mostly spent on GC.

But switching to fastjson resulted into excessive memory usage for cases
when vmalert has to parse long json lines, which usually happens when
instant response contains many `metric` objects.

In this change we do a mixed parsing:
1. Slice of `metric` objects is parsed with std lib to keep mem low
2. Each `metric` object is parsed with fastjson to reduce allocs

The benchmark results are the following:
```
pkg: github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource
BenchmarkParsePrometheusResponse/Instant_std+fastjson-10                    1760            668959 ns/op          280147 B/op       5781 allocs/op
MBs allocated at heap: 493.078392
mallocs: 18655472
BenchmarkParsePrometheusResponse/Instant_fastjson-10                        6109            198258 ns/op          172839 B/op       5548 allocs/op
MBs allocated at heap: 1056.384464
mallocs: 34457184
BenchmarkParsePrometheusResponse/Instant_std-10                             1287            950987 ns/op          451677 B/op       9619 allocs/op
MBs allocated at heap: 580.802976
mallocs: 13351636
```
The benchmark function code with mem measurement is available here
https://gist.github.com/hagen1778/b9c3ca7f8ca7d6b21aec9777112c5810

The benchmark contains 3 results:
1. Instant_std+fastjson is the implementation in this change
2. Instant_fastjson-10 is the implementation from
a5c427bac4
3. BenchmarkParsePrometheusResponse/Instant_std-10 is implementation
before
a5c427bac4

According to these results, this new implementation is slower than
previous, but faster than before switching to fastjson. It also has
lower number of allocations and roughly the same memory allocation on
heap with GC turned off.

---------

Other changes:
1. rm BenchmarkMetrics as it doesn't measure anything
2. simplify BenchmarkParsePrometheusResponse into
BenchmarkPromInstantUnmarshal

### Describe Your Changes

Please provide a brief description of the changes you made. Be as
specific as possible to help others understand the purpose and impact of
your modifications.

### Checklist

The following checks are **mandatory**:

- [ ] My change adheres [VictoriaMetrics contributing
guidelines](https://docs.victoriametrics.com/contributing/).

Signed-off-by: hagen1778 <roman@victoriametrics.com>
2024-08-22 17:36:11 +02:00
..
config app/vmalert: switch from table-driven tests to f-tests 2024-07-12 22:41:11 +02:00
datasource app/vmalert: update parsing for instant responses (#6859) 2024-08-22 17:36:11 +02:00
deployment app/vmalert: include it into the next release 2020-04-28 00:10:12 +03:00
multiarch deployment: build image for vmagent streamaggr benchmark (#6515) 2024-06-24 16:28:50 +02:00
notifier vmalert: add command line flag -notifier.headers (#6751) 2024-08-19 21:40:57 +02:00
remoteread lib/httputils: parse URL before creating HTTP transport (#6820) 2024-08-16 11:32:04 +02:00
remotewrite lib/httputils: parse URL before creating HTTP transport (#6820) 2024-08-16 11:32:04 +02:00
rule app/vmalert: rm unnecessary err check 2024-08-07 09:09:24 +02:00
static app/vmalert: fix links with anchors in vmalert's UI (#6146) 2024-04-22 15:02:10 +02:00
templates app/vmalert: switch from table-driven tests to f-tests 2024-07-12 22:41:11 +02:00
tpl app/vmalert: show on UI groups error after reload config (#4543) 2023-07-03 14:59:52 +02:00
utils app/vmalert: switch from table-driven tests to f-tests 2024-07-12 22:41:11 +02:00
main_test.go app/vmalert: switch from table-driven tests to f-tests 2024-07-12 22:41:11 +02:00
main.go vmalert: allow omitting -replay.timeTo in replay mode, default valu… (#6575) 2024-07-05 09:27:34 +02:00
Makefile vmalert: allow omitting -replay.timeTo in replay mode, default valu… (#6575) 2024-07-05 09:27:34 +02:00
manager_test.go app/vmalert: switch from table-driven tests to f-tests 2024-07-12 22:41:11 +02:00
manager.go app/vmalert: fix data race during hot-config reload (#5698) 2024-01-26 22:42:21 +01:00
README.md all: replace old https://docs.victoriametrics.com/vmalert.html url with the new one - https://docs.victoriametrics.com/vmalert/ 2024-04-18 01:44:12 +02:00
replay_test.go app/vmalert: switch from table-driven tests to f-tests 2024-07-12 22:41:11 +02:00
replay.go vmalert: allow omitting -replay.timeTo in replay mode, default valu… (#6575) 2024-07-05 09:27:34 +02:00
web_test.go app/vmalert: switch from table-driven tests to f-tests 2024-07-12 22:41:11 +02:00
web_types_test.go app/vmalert: switch from table-driven tests to f-tests 2024-07-12 22:41:11 +02:00
web_types.go all: consistently use 'any' instead of 'interface{}' 2024-07-10 00:20:37 +02:00
web.go lib/httpserver: skip basic auth check for additional request paths, which should call httpserver.CheckAuthFlag() 2024-07-16 01:00:45 +02:00
web.qtpl app/vmalert: follow-up after b60dcbe11f 2024-02-20 13:07:05 +01:00
web.qtpl.go app/vmalert: follow-up after b60dcbe11f 2024-02-20 13:07:05 +01:00

See vmalert docs here.

vmalert docs can be edited at docs/vmalert.md.