prometheus / jmx_exporter

A process for exposing JMX Beans via HTTP for Prometheus consumption
Apache License 2.0
3.06k stars 1.2k forks source link

Improve performance of JmxScraper #917

Closed nicktelford closed 9 months ago

nicktelford commented 9 months ago

JmxScraper#scrapeBean() uses ObjectName#toString() twice per-MBean attribute to check the MBean name against several special-cases.

ObjectName#toString() is quite expensive, because it allocates a new char[] and copies the name data into it, on each call.

Consequently, when there are a large number of number of MBean attributes, these calls compound and can create performance problems.

Caching the result of ObjectName#toString() outside this loop ensures we only call it once per-MBean.

nicktelford commented 9 months ago

See attached flame graph for an illustration of the allocations caused by these ObjectName#toString() calls: flame_graph

dhoard commented 9 months ago

@nicktelford Good catch! Thanks for the PR. I'll merge it