我目前正试图通过实现生产者 - 消费者模式来提高我的软件性能。在我的特定情况下,我有一个生产者,它依次创建Rows和多个使用者,为给定的一批行执行某些任务。
我现在面临的问题是,当我衡量生产者 - 消费者模式的表现时,我可以看到生产者的运行时间大幅增加,我不明白为什么会这样。
到目前为止,我主要是对我的代码进行了分析并进行了微基准测试,结果并没有将我引向实际问题。
public class ProdCons {
static class Row {
String[] _cols;
Row() {
_cols = Stream.generate(() -> "Row-Entry").limit(5).toArray(String[]::new);
}
}
static class Producer {
private static final int N_ITER = 8000000;
final ExecutorService _execService;
final int _batchSize;
final Function<Row[], Consumer> _f;
Producer(final int batchSize, final int nThreads, Function<Row[], Consumer> f) throws InterruptedException {
_execService = Executors.newFixedThreadPool(nThreads);
_batchSize = batchSize;
_f = f;
// init all threads to exclude their generaration time
startThreads();
}
private void startThreads() throws InterruptedException {
List<Callable<Void>> l = Stream.generate(() -> new Callable<Void>() {
@Override
public Void call() throws Exception {
Thread.sleep(10);
return null;
}
}).limit(4).collect(Collectors.toList());
_execService.invokeAll(l);
}
long run() throws InterruptedException {
final long start = System.nanoTime();
int idx = 0;
Row[] batch = new Row[_batchSize];
for (int i = 0; i < N_ITER; i++) {
batch[idx++] = new Row();
if (idx == _batchSize) {
_execService.submit(_f.apply(batch));
batch = new Row[_batchSize];
idx = 0;
}
}
final long time = System.nanoTime() - start;
_execService.shutdownNow();
_execService.awaitTermination(100, TimeUnit.MILLISECONDS);
return time;
}
}
static abstract class Consumer implements Callable<String> {
final Row[] _rowBatch;
Consumer(final Row[] data) {
_rowBatch = data;
}
}
static class NoOpConsumer extends Consumer {
NoOpConsumer(Row[] data) {
super(data);
}
@Override
public String call() throws Exception {
return null;
}
}
static class SomeConsumer extends Consumer {
SomeConsumer(Row[] data) {
super(data);
}
@Override
public String call() throws Exception {
String res = null;
for (int i = 0; i < 1000; i++) {
res = "";
for (final Row r : _rowBatch) {
for (final String s : r._cols) {
res += s;
}
}
}
return res;
}
}
public static void main(String[] args) throws InterruptedException {
final int nRuns = 10;
long totTime = 0;
for (int i = 0; i < nRuns; i++) {
totTime += new Producer(100, 1, (data) -> new NoOpConsumer(data)).run();
}
System.out.println("Avg time with NoOpConsumer:\t" + (totTime / 1000000000d) / nRuns + "s");
totTime = 0;
for (int i = 0; i < nRuns; i++) {
totTime += new Producer(100, 1, (data) -> new SomeConsumer(data)).run();
}
System.out.println("Avg time with SomeConsumer:\t" + (totTime / 1000000000d) / nRuns + "s");
}
实际上,由于消费者使用的线程不同于生产者,我希望生产者的运行时间不受消费者工作量的影响。但是,运行程序我得到以下输出
#1线程,#100批量大小
使用NoOpConsumer的平均时间:0.7507254368s
SomeConsumer的平均时间:1.5334749871s
请注意,时间测量仅测量生产时间而不是消费者时间,并且不提交任何工作需要平均值。 ~0.6秒。
更令人惊讶的是,当我将线程数从1增加到4时,我得到以下结果(4核与超线程)。
#4个线程,#100批量大小
与NoOpConsumer的平均时间:0.7741189636s
SomeConsumer的平均时间:2.5561667638s
难道我做错了什么?我错过了什么?目前我不得不相信运行时差异是由于上下文切换或与我的系统相关的任何事情。
线程彼此不完全隔离。
看起来你的SomeConsumer
类分配了大量的内存,这会产生在所有线程之间共享的垃圾收集工作,包括你的生产者线程。
它还访问大量内存,这可以将生产者使用的内存从L1或L2缓存中剔除。访问实际内存比访问缓存需要更长的时间,因此这可以使您的生产者花费更长的时间。
另请注意,我实际上没有确认您是否正确地测量了制作人的时间,并且很容易在那里犯错误。