识别运行缓慢的测试

发布于 2024-07-14 20:53:58 字数 123 浏览 13 评论 0原文

在 ruby​​ 的测试/单元中,软件指示测试运行需要多长时间,并且一系列的通过、错误和失败就像伪进度条一样。

除了使用代码分析工具或单独运行测试之外,是否有一种简单的方法可以判断哪些测试方法快,哪些测试方法慢?

In ruby's test/unit, the software indicates how long it takes for the tests to run, and the series of passes, errors and fails act like a pseudo-progress bar.

Apart from using code profiling tools or running tests individually, is there an easy way of telling which test methods are fast and which ones are slow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨光如昨 2024-07-21 20:53:58

当我需要在大型测试套件中执行此操作时,我会覆盖 Test::Unit::TestCase 设置和拆卸。 它不能提供精确的测量结果,但可以帮助评估相对速度。

module Test
  module Unit
    def setup
      @start_time = Time.now
    end

    def teardown
      puts "#{@method_name}: #{Time.now - @start_time}s"
    end
  end
end

When I need to do this in a large test suite, I override Test::Unit::TestCase setup and teardown. It doesn't give precise measurements, but it can help assess relative speed.

module Test
  module Unit
    def setup
      @start_time = Time.now
    end

    def teardown
      puts "#{@method_name}: #{Time.now - @start_time}s"
    end
  end
end
泪眸﹌ 2024-07-21 20:53:58

根据Sarah的回答,我更喜欢另一种解决方案:

像Sarah写的那样设置,但是Teardown应该在列表中添加测试名称和执行时间。
所以你可以评估这个列表,进行排序或其他任何操作。 我不懂Ruby,所以我不知道它是否有效。

这是 JUnit 的一些 Java 代码来解释我的想法......

public class ExecutionTimeTest {

  public static ArrayList<Double> executionTimes;

  public double start;

  @BeforeClass
  public static void initializeList() {
    executionTimes = new ArrayList<Double>();
  }

  @AfterClass
  public static void printExecutionTimes() {
    int i = 1;
    for (Double time : executionTimes) {
      System.out.println("Test " + (i++) + ": " + time);
    }
  }

  @Before
  public void startExecutionTime() {
    start = System.currentTimeMillis();
  }

  @After
  public void calculateExecutionTime() {
    executionTimes.add(System.currentTimeMillis() - start);
  }
}

According to Sarah's answer, I would prefer another solution:

Setup like Sarah wrote, but the Teardown should add the test name and the execution time in a list.
So you can evaluate this list, for sorting or whatever. I don't know Ruby, so I don't know if it would work.

Here is some Java code for JUnit to explain my thoughts...

public class ExecutionTimeTest {

  public static ArrayList<Double> executionTimes;

  public double start;

  @BeforeClass
  public static void initializeList() {
    executionTimes = new ArrayList<Double>();
  }

  @AfterClass
  public static void printExecutionTimes() {
    int i = 1;
    for (Double time : executionTimes) {
      System.out.println("Test " + (i++) + ": " + time);
    }
  }

  @Before
  public void startExecutionTime() {
    start = System.currentTimeMillis();
  }

  @After
  public void calculateExecutionTime() {
    executionTimes.add(System.currentTimeMillis() - start);
  }
}
太阳哥哥 2024-07-21 20:53:58

如果您使用测试单元 gem,以详细模式运行测试将为您提供有关每个测试花费多长时间的信息:

ruby test/test_unit.rb --verbose
Loaded suite test/test_unit
Started
ChaserTestCase: 
  test_handle_funny_characters_in_class_method_names:   .: (0.000645)
  test_handle_funny_characters_in_instance_method_names:.: (0.000523)
  test_modify_and_unmodify_class_method:                .: (0.000534)
...
Finished in 0.019894 seconds.

MiniTest 将 还有 基准测试。

If you're using the test-unit gem, running the tests in verbose mode will give you information on how long each test took:

ruby test/test_unit.rb --verbose
Loaded suite test/test_unit
Started
ChaserTestCase: 
  test_handle_funny_characters_in_class_method_names:   .: (0.000645)
  test_handle_funny_characters_in_instance_method_names:.: (0.000523)
  test_modify_and_unmodify_class_method:                .: (0.000534)
...
Finished in 0.019894 seconds.

MiniTest will also have benchmarking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文