25
25
import org .apache .lucene .search .ScoreMode ;
26
26
import org .apache .lucene .store .Directory ;
27
27
import org .apache .lucene .store .MMapDirectory ;
28
+ import org .elasticsearch .common .settings .Settings ;
28
29
import org .elasticsearch .common .unit .ByteSizeValue ;
29
30
import org .elasticsearch .common .util .BigArrays ;
30
31
import org .elasticsearch .common .util .LongHash ;
32
+ import org .elasticsearch .node .Node ;
33
+ import org .elasticsearch .threadpool .ThreadPool ;
31
34
import org .elasticsearch .xpack .sql .action .compute .data .Block ;
32
35
import org .elasticsearch .xpack .sql .action .compute .data .LongBlock ;
33
36
import org .elasticsearch .xpack .sql .action .compute .data .Page ;
34
37
import org .elasticsearch .xpack .sql .action .compute .lucene .LuceneSourceOperator ;
35
38
import org .elasticsearch .xpack .sql .action .compute .lucene .NumericDocValuesExtractor ;
36
39
import org .elasticsearch .xpack .sql .action .compute .operator .Driver ;
40
+ import org .elasticsearch .xpack .sql .action .compute .operator .LongAvgOperator ;
37
41
import org .elasticsearch .xpack .sql .action .compute .operator .LongGroupingOperator ;
38
42
import org .elasticsearch .xpack .sql .action .compute .operator .LongMaxOperator ;
39
43
import org .elasticsearch .xpack .sql .action .compute .operator .LongTransformerOperator ;
40
44
import org .elasticsearch .xpack .sql .action .compute .operator .Operator ;
41
45
import org .elasticsearch .xpack .sql .action .compute .operator .PageConsumerOperator ;
46
+ import org .elasticsearch .xpack .sql .action .compute .operator .exchange .ExchangeSink ;
47
+ import org .elasticsearch .xpack .sql .action .compute .operator .exchange .ExchangeSinkOperator ;
48
+ import org .elasticsearch .xpack .sql .action .compute .operator .exchange .ExchangeSource ;
49
+ import org .elasticsearch .xpack .sql .action .compute .operator .exchange .ExchangeSourceOperator ;
50
+ import org .elasticsearch .xpack .sql .action .compute .operator .exchange .PassthroughExchanger ;
51
+ import org .elasticsearch .xpack .sql .action .compute .operator .exchange .RandomExchanger ;
52
+ import org .elasticsearch .xpack .sql .action .compute .operator .exchange .RandomUnionSourceOperator ;
42
53
import org .openjdk .jmh .annotations .Benchmark ;
43
54
import org .openjdk .jmh .annotations .BenchmarkMode ;
44
55
import org .openjdk .jmh .annotations .Fork ;
60
71
import java .util .Random ;
61
72
import java .util .concurrent .TimeUnit ;
62
73
import java .util .concurrent .atomic .AtomicInteger ;
74
+ import java .util .function .Consumer ;
75
+ import java .util .stream .Collectors ;
63
76
64
77
@ Fork (value = 1 )
65
78
@ Warmup (iterations = 1 )
@@ -75,6 +88,8 @@ public class OperatorBenchmark {
75
88
@ Param ({ "100000000" }) // 100 million
76
89
int numDocs ;
77
90
91
+ ThreadPool threadPool ;
92
+
78
93
@ Setup
79
94
public void setup () throws IOException {
80
95
Path path = Files .createTempDirectory ("test" );
@@ -94,12 +109,14 @@ public void setup() throws IOException {
94
109
indexWriter .flush ();
95
110
}
96
111
indexReader = DirectoryReader .open (dir );
112
+ threadPool = new ThreadPool (Settings .builder ().put (Node .NODE_NAME_SETTING .getKey (), "OperatorBenchmark" ).build ());
97
113
}
98
114
99
115
@ TearDown
100
116
public void tearDown () throws IOException {
101
117
indexReader .close ();
102
118
dir .close ();
119
+ ThreadPool .terminate (threadPool , 30 , TimeUnit .SECONDS );
103
120
}
104
121
105
122
private static class SimpleXORValueCollector implements Collector {
@@ -284,7 +301,7 @@ private int runWithDriver(int pageSize, Operator... operators) {
284
301
}
285
302
286
303
@ Benchmark
287
- public long testVisitAllNumbersBatched4K () throws InterruptedException {
304
+ public long testVisitAllNumbersBatched4K () {
288
305
return runWithDriver (
289
306
ByteSizeValue .ofKb (4 ).bytesAsInt (),
290
307
new NumericDocValuesExtractor (indexReader , 0 , 1 , "value" ),
@@ -293,7 +310,7 @@ public long testVisitAllNumbersBatched4K() throws InterruptedException {
293
310
}
294
311
295
312
@ Benchmark
296
- public long testVisitAllNumbersBatched16K () throws InterruptedException {
313
+ public long testVisitAllNumbersBatched16K () {
297
314
return runWithDriver (
298
315
ByteSizeValue .ofKb (16 ).bytesAsInt (),
299
316
new NumericDocValuesExtractor (indexReader , 0 , 1 , "value" ),
@@ -302,17 +319,17 @@ public long testVisitAllNumbersBatched16K() throws InterruptedException {
302
319
}
303
320
304
321
@ Benchmark
305
- public long testVisitAllDocsBatched4K () throws InterruptedException {
322
+ public long testVisitAllDocsBatched4K () {
306
323
return runWithDriver (ByteSizeValue .ofKb (4 ).bytesAsInt ());
307
324
}
308
325
309
326
@ Benchmark
310
- public long testVisitAllDocsBatched16K () throws InterruptedException {
327
+ public long testVisitAllDocsBatched16K () {
311
328
return runWithDriver (ByteSizeValue .ofKb (16 ).bytesAsInt ());
312
329
}
313
330
314
331
@ Benchmark
315
- public long testOperatorsWithLucene () throws InterruptedException {
332
+ public long testOperatorsWithLucene () {
316
333
return runWithDriver (
317
334
ByteSizeValue .ofKb (16 ).bytesAsInt (),
318
335
new NumericDocValuesExtractor (indexReader , 0 , 1 , "value" ),
@@ -321,4 +338,73 @@ public long testOperatorsWithLucene() throws InterruptedException {
321
338
new LongTransformerOperator (0 , i -> i + 1 ) // adds +1 to group number (which start with 0) to get group count
322
339
);
323
340
}
341
+
342
+ @ Benchmark
343
+ public long testSingleThreadedAvg () {
344
+ return runWithDriver (
345
+ ByteSizeValue .ofKb (16 ).bytesAsInt (),
346
+ new NumericDocValuesExtractor (indexReader , 0 , 1 , "value" ),
347
+ new LongAvgOperator (2 ), // partial reduction
348
+ new LongAvgOperator (0 , 1 ) // final reduction
349
+ );
350
+ }
351
+
352
+ @ Benchmark
353
+ public long testMultiThreadedAvg () {
354
+ AtomicInteger rowCount = new AtomicInteger ();
355
+ int parallelCount = 8 ;
356
+ List <Driver > drivers = new ArrayList <>(parallelCount );
357
+ List <ExchangeSource > forkExchangeSources = new ArrayList <>(parallelCount );
358
+ List <ExchangeSource > joinExchangeSources = new ArrayList <>(parallelCount );
359
+ for (int i = 0 ; i < parallelCount ; i ++) {
360
+ ExchangeSource forkExchangeSource = new ExchangeSource ();
361
+ forkExchangeSources .add (forkExchangeSource );
362
+ ExchangeSource joinExchangeSource = new ExchangeSource ();
363
+ joinExchangeSources .add (joinExchangeSource );
364
+ List <Operator > operatorList = new ArrayList <>();
365
+ operatorList .add (new ExchangeSourceOperator (forkExchangeSource ));
366
+ operatorList .addAll (
367
+ List .of (
368
+ new NumericDocValuesExtractor (indexReader , 0 , 1 , "value" ),
369
+ new LongAvgOperator (2 ), // PARTIAL
370
+ new ExchangeSinkOperator (
371
+ new ExchangeSink (new PassthroughExchanger (joinExchangeSource , Integer .MAX_VALUE ), s -> joinExchangeSource .finish ())
372
+ )
373
+ )
374
+ );
375
+ Driver driver = new Driver (operatorList , () -> {});
376
+ drivers .add (driver );
377
+ }
378
+
379
+ Driver luceneDriver = new Driver (
380
+ List .of (
381
+ new LuceneSourceOperator (indexReader , new MatchAllDocsQuery (), ByteSizeValue .ofKb (16 ).bytesAsInt ()),
382
+ new ExchangeSinkOperator (
383
+ new ExchangeSink (
384
+ new RandomExchanger (
385
+ forkExchangeSources .stream ()
386
+ .map (exchangeSource -> (Consumer <Page >) page -> exchangeSource .addPage (page , () -> {}))
387
+ .collect (Collectors .toList ())
388
+ ),
389
+ sink -> forkExchangeSources .stream ().forEach (ExchangeSource ::finish )
390
+ )
391
+ )
392
+ ),
393
+ () -> {}
394
+ );
395
+ drivers .add (luceneDriver );
396
+
397
+ Driver reduceDriver = new Driver (
398
+ List .of (
399
+ new RandomUnionSourceOperator (joinExchangeSources ),
400
+ new LongAvgOperator (0 , 1 ), // FINAL
401
+ new PageConsumerOperator (page -> rowCount .addAndGet (page .getPositionCount ()))
402
+ ),
403
+ () -> {}
404
+ );
405
+ drivers .add (reduceDriver );
406
+
407
+ Driver .runToCompletion (threadPool .executor (ThreadPool .Names .SEARCH ), drivers ).actionGet ();
408
+ return rowCount .get ();
409
+ }
324
410
}
0 commit comments