# High CPU usage with Python agent

**URL:** <https://discuss.elastic.co/t/high-cpu-usage-with-python-agent/133531>\
**Category:** APM\
**Created:** [May 28, 2018, 12:11pm UTC](https://discuss.elastic.co/t/high-cpu-usage-with-python-agent/133531 "2018-05-28T12:11:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Majsvaffla](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@Majsvaffla](https://discuss.elastic.co/u/Majsvaffla)\
**Post date:** [May 28, 2018, 12:11pm UTC](https://discuss.elastic.co/t/high-cpu-usage-with-python-agent/133531/1 "2018-05-28T12:11:56Z")

</div>

Hello!

I have a webserver running a Django application and have started to use the Elastic APM since Opbeat was shut down. I'm currently only doing instrumentation and I'm experiencing high CPU loads. I used Opbeat before migrating to Elastic APM without experiencing CPU loads as high as with Elastic APM.

The only configuration I've done is to setup SECRET\_TOKEN, SERVER\_URL, SERVICE\_VERSION and SERVICE\_NAME in the ELASTIC\_APM setting in Django settings.

I know about the transaction sample rate setting and it would presumably lower the CPU load but I wouldn't expect Elastic APM to perform that different from Opbeat.

Do you have any suggestions to what I might have misconfigured or can you help me debug this issue in any way?

---

<div class="post-metadata">

**Author:** ![beniwohli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beniwohli/32/21256_2.png) [@beniwohli](https://discuss.elastic.co/u/beniwohli)\
**Post date:** [May 28, 2018, 12:36pm UTC](https://discuss.elastic.co/t/high-cpu-usage-with-python-agent/133531/2 "2018-05-28T12:36:08Z")

</div>

Hi David

Thanks for raising this issue! In some cases, the Python agent can indeed use more CPU than the Opbeat agent. This is because the Opbeat agent did some aggressive optimization and caching that was later resolved in the Opbeat backend. Due to how Elastic APM works, these optimizations are no longer possible.

We benchmarked the Python agent a lot, and the largest hot spot tends to be collection of stack frames for spans. If your code generates a lot of short spans, one option is to disable stack frame collection for very short spans using the [`span_frames_min_duration`](https://www.elastic.co/guide/en/apm/agent/python/2.x/configuration.html#config-span-frames-min-duration-ms) setting, e.g. set it to `5` to not collect any span stack frames for spans shorter than 5 milliseconds.

Also have a look at our [Tuning and Overhead](https://www.elastic.co/guide/en/apm/agent/python/2.x/tuning-and-overhead.html) docs page if you haven't seen it yet.

---

<div class="post-metadata">

**Author:** ![Majsvaffla](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@Majsvaffla](https://discuss.elastic.co/u/Majsvaffla)\
**Post date:** [May 28, 2018, 1:43pm UTC](https://discuss.elastic.co/t/high-cpu-usage-with-python-agent/133531/3 "2018-05-28T13:43:55Z")

</div>

Yeah there's a large amount of short spans. Now with a span duration limit of 5ms the CPU-load is lower. I will try even higher limits and probably set the transaction sample rate as well. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![beniwohli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beniwohli/32/21256_2.png) [@beniwohli](https://discuss.elastic.co/u/beniwohli)\
**Post date:** [May 28, 2018, 2:00pm UTC](https://discuss.elastic.co/t/high-cpu-usage-with-python-agent/133531/4 "2018-05-28T14:00:06Z")

</div>

Good to hear it has an effect! Please keep us up to date on your findings, as this is a subject that is very important to us. We're e.g. considering to setting a default value for `span_frames_min_duration`, so it would definitely be interesting to hear how and on which value you settle!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 18, 2018, 10:08am UTC](https://discuss.elastic.co/t/high-cpu-usage-with-python-agent/133531/5 "2018-06-18T10:08:26Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
