title: How do you find the CPU consumption for a piece of Python?
body: <h2>Background</h2>
  
  <p>I have a Django application, it works and responds pretty well on low load, but on high load like 100 users/sec, it consumes 100% CPU and then due to lack of CPU slows down.</p>
  
  <p>Problem:</p>
  
  <ul>
  <li>Profiling the application gives me time taken by functions.</li>
  <li>This time increases on high load.</li>
  <li>Time consumed may be due to complex calculation or for waiting for CPU.</li>
  </ul>
  
  <p><strong>So, how to find the CPU cycles consumed by a piece of code ?</strong></p>
  
  <p><strong>Since reducing the CPU consumption will increase the response time.</strong></p>
  
  <ul>
  <li>I might have written extremely efficient code and need to add more CPU power </li>
  </ul>
  
  <p>OR</p>
  
  <ul>
  <li>I might have some stupid code taking the CPU and causing the slow down ?</li>
  </ul>
  
  <h2>Update</h2>
  
  <ul>
  <li>I am using Jmeter to profile my web app, it gives me a throughput of 2 requests/sec. [ 100 users]</li>
  <li>I get a average time of 36 seconds on 100 request vs 1.25 sec time on 1 request.</li>
  </ul>
  
  <p><strong>More Info</strong></p>
  
  <ul>
  <li>Configuration Nginx + Uwsgi with 4 workers</li>
  <li>No database used, using a responses from a REST API</li>
  <li>On 1st hit the response of REST API gets cached, therefore doesn't makes a difference.</li>
  <li>Using <code>ujson</code> for json parsing.</li>
  </ul>
  
  <p><strong>Curious to know:</strong></p>
  
  <ul>
  <li>Python-Django is used by so many orgs for so many big sites, then there must be some high end Debug / Memory-CPU analysis tools.</li>
  <li>All those I found were casual snippets of code that perform profiling.</li>
  </ul>