【问题标题】:Warm up the code before start measuring the performance在开始测量性能之前预热代码
【发布时间】:2013-02-28 03:27:33
【问题描述】:

我已经开始着手一个项目,我将在其中传递我需要使用的线程数,然后我将尝试测量SELECT sql 在执行过程中花费了多少时间,因此我有一个计数器在这条线之前preparedStatement.executeQuery(); 和一个计数器来测量这条线之后的时间。

下面是我的代码的sn-p-

public class TestPool {

    public static void main(String[] args) {

        final int no_of_threads = 10;

        // create thread pool with given size
        ExecutorService service = Executors.newFixedThreadPool(no_of_threads);

        // queue some tasks
        for(int i = 0; i < 3 * no_of_threads; i++) {
            service.submit(new ThreadTask());
        }

        // wait for termination
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

        // Now print the select histogram here
        System.out.println(ThreadTask.selectHistogram);
    }
}

下面是我实现 Runnable 接口的 ThreadTask 类-

class ThreadTask implements Runnable {

    private PreparedStatement preparedStatement = null;
    private ResultSet rs = null;

    public static ConcurrentHashMap<Long, AtomicLong> selectHistogram = new ConcurrentHashMap<Long, AtomicLong>();

    public ThreadTask() {
    }

    @Override
    public void run() {

        ...........

        long start = System.nanoTime();
        rs = preparedStatement.executeQuery();
        long end = System.nanoTime() - start;

        final AtomicLong before = selectHistogram.putIfAbsent(end / 1000000L, new AtomicLong(1L));
        if (before != null) {
           before.incrementAndGet();
        }

        ..............

    }
}

问题陈述:-

今天我开了一个设计会议,会议上大多数人都说不要在运行程序后立即开始测量时间。有一些热身时间,然后开始测量。所以我认为在那之后这样做是有道理的。现在我在想我应该如何在我的代码中加入这个变化。我将在什么基础上这样做?任何人都可以提出一些建议吗?

【问题讨论】:

  • 您是否要测量 SQL 的性能?为什么不在数据库端测量它,因为它消除了应用程序端的所有不确定性。

标签: java sql database multithreading performance-testing


【解决方案1】:

最简单的热身方法之一是在运行实际(定时)测试之前在同一个 JVM 中运行相同的测试,无需计时。您应该运行数千次迭代,让 JIT 有机会识别和优化热点。对How do I write a correct micro-benchmark in Java?的回答有很多细节(关于这个和其他你需要考虑的问题)

【讨论】:

    【解决方案2】:

    你可以运行它大约 15 分钟来创建新的线程任务。之后清除 selectHistogram 重新运行所需的线程数以打印指标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      相关资源
      最近更新 更多