使用太多Threads基准程序的问题

Sib*_*bbo 1 java benchmarking multithreading multicore

我用Java编写了一个(非常简单的)基准测试程序.它只是将double值增加到指定值并占用时间.

当我在我的6核桌面上使用这个单线程或少量线程(最多100个)时,基准测试会返回合理且可重复的结果.

但是当我使用例如1200个线程时,平均多核持续时间明显低于单个核心持续时间(大约10倍或更多).无论我使用了多少线程,我都确保增量的总量是相同的.

为什么性能会随着线程的增加而下降呢?有没有办法解决这个问题?

我发布了我的来源,但我认为没有问题.

Benchmark.java:

package sibbo.benchmark;

import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;

public class Benchmark implements TestFinishedListener {
            private static final double TARGET = 1e10;
    private static final int THREAD_MULTIPLICATOR = 2;

    public static void main(String[] args) throws InterruptedException {
        Benchmark b = new Benchmark(TARGET);
        b.start();
    }

    private int coreCount;
    private List<Worker> workers = new LinkedList<>();
    private List<Worker> finishedWorkers = new LinkedList<>();
    private double target;

    public Benchmark(double target) {
        this.target = target;
        getSystemInfos();
        printInfos();
    }

    private void getSystemInfos() {
        coreCount = Runtime.getRuntime().availableProcessors();
    }

    private void printInfos() {
        System.out.println("Usable cores: " + coreCount);
        System.out.println("Multicore threads: " + coreCount *                 THREAD_MULTIPLICATOR);
        System.out.println("Loops per core: " + new DecimalFormat("###,###,###,###,##0").format(TARGET));

        System.out.println();
    }

    public synchronized void start() throws InterruptedException {
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

        System.out.print("Initializing singlecore benchmark... ");
        Worker w = new Worker(this, 0);
        workers.add(w);

        Thread.sleep(1000);
        System.out.println("finished");

        System.out.print("Running singlecore benchmark... ");
        w.runBenchmark(target);
        wait();

        System.out.println("finished");
        printResult();

        System.out.println();
        // Multicore
        System.out.print("Initializing multicore benchmark...  ");
        finishedWorkers.clear();

        for (int i = 0; i < coreCount * THREAD_MULTIPLICATOR; i++) {
            workers.add(new Worker(this, i));
        }

        Thread.sleep(1000);
        System.out.println("finished");

        System.out.print("Running multicore benchmark...  ");

        for (Worker worker : workers) {
            worker.runBenchmark(target / THREAD_MULTIPLICATOR);
        }

        wait();

        System.out.println("finished");
        printResult();

        Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
    }

    private void printResult() {
        DecimalFormat df = new DecimalFormat("###,###,###,##0.000");

        long min = -1, av = 0, max = -1;
        int threadCount = 0;
        boolean once = true;

        System.out.println("Result:");

        for (Worker w : finishedWorkers) {
            if (once) {
                once = false;

                min = w.getTime();
                max = w.getTime();
            }

            if (w.getTime() > max) {
                max = w.getTime();
            }

            if (w.getTime() < min) {
                min = w.getTime();
            }

            threadCount++;
            av += w.getTime();

            if (finishedWorkers.size() <= 6) {
                System.out.println("Worker " + w.getId() + ": " + df.format(w.getTime() / 1e9) + "s");
            }
        }

        System.out.println("Min: " + df.format(min / 1e9) + "s, Max: " + df.format(max / 1e9) + "s, Av per Thread: "
                + df.format((double) av / threadCount / 1e9) + "s");
    }

    @Override
    public synchronized void testFinished(Worker w) {
        workers.remove(w);
        finishedWorkers.add(w);

        if (workers.isEmpty()) {
            notify();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Worker.java:

package sibbo.benchmark;

public class Worker implements Runnable {
    private double value = 0;
    private long time;
    private double target;
    private TestFinishedListener l;
    private final int id;

    public Worker(TestFinishedListener l, int id) {
        this.l = l;
        this.id = id;

        new Thread(this).start();
    }

    public int getId() {
        return id;
    }

    public synchronized void runBenchmark(double target) {
        this.target = target;
        notify();
    }

    public long getTime() {
        return time;
    }

    @Override
    public void run() {
        synWait();
        value = 0;
        long startTime = System.nanoTime();

        while (value < target) {
            value++;
        }

        long endTime = System.nanoTime();
        time = endTime - startTime;

        l.testFinished(this);
    }

    private synchronized void synWait() {
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*ain 6

您需要了解OS(或Java线程调度程序,或两者)正在尝试在应用程序中的所有线程之间进行平衡,以便为它们提供执行某些工作的机会,并且在切换之间存在非零成本线程.使用1200个线程,您刚刚达到(并且可能远远超过)转换点,其中处理器花费更多时间上下文切换而不是实际工作.

这是一个粗略的比喻:

你在A房有一份工作要做.你每天在A房待8小时,然后完成你的工作.

然后你的老板过来告诉你,你也必须在B室做一份工作.现在你需要定期离开A房间,沿着大厅走到B房间,然后走回去.每天步行需要1分钟.现在你花3个小时,每个工作59.5分钟,在房间之间走一分钟.

现在想象一下,你有1200个房间可供工作.你将花费更多时间在房间之间行走而不是做实际工作.这就是您将处理器放入的情况.它花了很多时间在上下文之间切换,没有真正的工作.

编辑:现在,根据下面的评论,你可能会在每个房间里花费一定的时间,然后再继续工作 - 但是房间之间的上下文切换次数仍会影响单个任务的整体运行时间.