title: How Ruby benchmark calculates total cpu time inside a thread
body: <pre><code>N.times { Thread.new { puts Benchmark.measure { /* code */ } } }
  </code></pre>
  
  <p>Does the benchmark show the time actually spent executing the code in the thread?</p>
  
  <p>Or does it show the total time the ruby interpreter has been running on any thread while the thread we are considering is alive (even if it is paused because another one is running)?</p>
  
  <p>For example suppose that a thread A has been in execution for 1ms, then MRI switches to the thread B and stays there for 3ms. Finally thread A is executed again and after another 1ms it terminates.</p>
  
  <p>For thread A does the benchmark shows 2ms or 5ms as the total time? (<strong>not</strong> real time)</p>
  
  <p><strong>UPDATE:</strong> I think that a single Sidekiq process spawns multiple Ruby threads. So it's equivalent to talk about Sidekiq jobs instead of threads. I have some Sidekiq  jobs that take much more <em>cpu time</em> when Sidekiq is executing some other heavy jobs. This leads me to think that the benchmarking is including the time spent on other jobs. However maybe @mudasobwa is right and cpu time doesn't include the time spent on other threads/jobs. In that case the only explanation I can give is that the system connectivity/bandwidth is a bottleneck (and my lightweight jobs performance are affected by the other heavy jobs that consume a lot of bandwidth). </p>