Ruby基准如何计算线程中的总CPU时间


N.times { Thread.new { puts Benchmark.measure { /* code */ } } }

基准测试是否显示了在线程中执行代码的时间?

或它显示了我们正在考虑的线程还活着的线程上的Ruby解释器一直在任何线程上运行的总时间(即使它由于另一个正在运行而暂停)?

例如,假设线程A在执行1ms中,然后MRI切换到线程B并在此停留3ms。最后,螺纹A再次执行,然后再执行1毫秒后。

对于线程A,基准测试是否显示为2ms或5ms作为总时间?(不是实时)

更新:我认为单个Sidekiq进程会产生多个Ruby线程。因此,谈论sidekiq作业而不是线程是相同的。当Sidekiq执行其他重大工作时,我有一些Sidekiq的作业需要更多的CPU时间。这使我认为基准测试包括花在其他工作上的时间。但是,也许@mudasobwa是正确的,CPU时间不包括其他线程/作业上花费的时间。在这种情况下,我唯一可以给出的解释是系统连接/带宽是瓶颈(而且我的轻量级工作表现受其他消耗大量带宽的沉重工作的影响)。

我想知道是否很难检查?

▶ Benchmark.measure { 3.times { |i| 
    Thread.new { puts Benchmark.measure { sleep i }.inspect } 
  } }
#⇒ #<Benchmark::Tms:0x000000018c2818 @label="", @real=8.5858e-05, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>
#⇒ #<Benchmark::Tms:0x000000018c3ab0 @cstime=0.0, @cutime=0.0, @label="", @real=0.000118425, @stime=0.0, @total=0.0, @utime=0.0>
#⇒ #<Benchmark::Tms:0x0000000183cc40 @label="", @real=1.000119122, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>
#⇒ #<Benchmark::Tms:0x0000000183c7e0 @label="", @real=2.000088775, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>

第二行显然是周围的总数。

好吧,这是一个带有唤醒线程的示例(最后是总计):

▶ Benchmark.measure { 3.times { |i| Thread.new { puts Benchmark.measure { (i * 100_000_000).times { 2000 << 2 } }.inspect } } }
#⇒ #<Benchmark::Tms:0x000000016c6438 @label="", @real=2.377e-06, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>
#⇒ #<Benchmark::Tms:0x000000016c5420 @label="", @real=9.034328202, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=9.040000000000001, @total=9.040000000000001>
#⇒ #<Benchmark::Tms:0x000000016c4a98 @label="", @real=13.769757073, @cstime=0.0, @cutime=0.0, @stime=0.010000000000000009, @utime=13.77, @total=13.78>
#⇒ #<Benchmark::Tms:0x000000016c6bb8 @cstime=0.0, @cutime=0.0, @label="", @real=5.1321e-05, @stime=0.0, @total=0.0, @utime=0.0>

最新更新