确定CPU,RAM或硬盘驱动器是否是Ruby脚本的瓶颈

时间:2012-05-07 05:36:55

标签: ruby performance

我打算尽快购买新的Mac桌面,我想知道CPU,RAM或我的硬盘是否是我脚本的瓶颈。

我在Ubuntu 12.04上使用Ruby 1.9.3运行主要单元测试并获得以下信息:

$ date; /usr/bin/time --verbose ruby1.9.1 test/test_all.rb ; date
Mon May  7 15:04:38 EST 2012
Run options: 

# Running tests:

[snip 705 dots]

Finished tests in 50.672999s, 13.9127 tests/s, 49.1781 assertions/s.

705 tests, 2492 assertions, 0 failures, 0 errors, 0 skips
    Command being timed: "ruby1.9.1 test/test_all.rb"
    User time (seconds): 29.25
    System time (seconds): 5.26
    Percent of CPU this job got: 67%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:51.01
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 238592
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 4180160
    Voluntary context switches: 31187
    Involuntary context switches: 12397
    Swaps: 0
    File system inputs: 0
    File system outputs: 224
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0
Mon May  7 15:05:29 EST 2012

由于用户加系统所花费的时间少于挂起时间,我认为CPU不是唯一的瓶颈。我怎样才能弄清楚瓶颈还有什么呢?

1 个答案:

答案 0 :(得分:2)

您可以使用valgrind's cachegrind工具分析程序的内存性能(即它如何利用缓存)。

$ valgrind --tool=cachegrind ruby ./hello.rb
==7082== Cachegrind, a cache and branch-prediction profiler.
==7082== Copyright (C) 2002-2008, and GNU GPL'd, by Nicholas Nethercote et al.
==7082== Using LibVEX rev 1884, a library for dynamic binary translation.
==7082== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==7082== Using valgrind-3.4.1-Debian, a dynamic binary instrumentation framework.
==7082== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==7082== For more details, rerun with: -v
==7082== 
hello world
==7082== 
==7082== I   refs:      14,529,000
==7082== I1  misses:        24,856
==7082== L2i misses:         6,707
==7082== I1  miss rate:       0.17%
==7082== L2i miss rate:       0.04%
==7082== 
==7082== D   refs:       7,110,663  (4,572,482 rd   + 2,538,181 wr)
==7082== D1  misses:        48,207  (   33,427 rd   +    14,780 wr)
==7082== L2d misses:        16,350  (    3,821 rd   +    12,529 wr)
==7082== D1  miss rate:        0.6% (      0.7%     +       0.5%  )
==7082== L2d miss rate:        0.2% (      0.0%     +       0.4%  )
==7082== 
==7082== L2 refs:           73,063  (   58,283 rd   +    14,780 wr)
==7082== L2 misses:         23,057  (   10,528 rd   +    12,529 wr)
==7082== L2 miss rate:         0.1% (      0.0%     +       0.4%  )

关于磁盘性能,我认为没有磁盘/ io使用的程序几乎完全在用户时间运行,这让我相信你的硬盘可能至少是你的瓶颈之一。也许有人可以推荐一个好的工具来分析程序的磁盘使用情况?