-
Notifications
You must be signed in to change notification settings - Fork 242
/
AbstractMassIndexingBenchmarks.java
83 lines (69 loc) · 3.15 KB
/
AbstractMassIndexingBenchmarks.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.integrationtest.performance.backend.base;
import java.util.concurrent.CompletableFuture;
import org.hibernate.search.engine.backend.work.execution.DocumentCommitStrategy;
import org.hibernate.search.engine.backend.work.execution.spi.IndexIndexer;
import org.hibernate.search.integrationtest.performance.backend.base.testsupport.dataset.Dataset;
import org.hibernate.search.integrationtest.performance.backend.base.testsupport.dataset.DatasetHolder;
import org.hibernate.search.integrationtest.performance.backend.base.testsupport.index.AbstractBackendHolder;
import org.hibernate.search.integrationtest.performance.backend.base.testsupport.index.MappedIndex;
import org.hibernate.search.util.common.impl.Futures;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubBackendSessionContext;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMapperUtils;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
/**
* Abstract class for JMH benchmarks related to mass indexing,
* i.e. document adds with few commits.
* <p>
* This benchmark generates and executes batches of documents to add to the index from a single thread,
* guaranteeing not to conflict with any other thread in the same trial.
*/
@Fork(1)
@State(Scope.Thread)
public abstract class AbstractMassIndexingBenchmarks extends AbstractBackendBenchmarks {
@Param({ "NONE" })
private DocumentCommitStrategy commitStrategy;
/**
* Equivalent to the MassIndexer's "batchSizeToLoadObjects".
*/
@Param({ "200" })
private int batchSize;
private MappedIndex index;
private IndexIndexer indexer;
private Dataset dataset;
private long currentDocumentIdInThread = 0L;
@Setup(Level.Iteration)
public void prepareIteration(DatasetHolder datasetHolder) {
index = getIndexPartition().getIndex();
StubBackendSessionContext sessionContext = new StubBackendSessionContext();
indexer = index.createIndexer( sessionContext );
dataset = datasetHolder.getDataset();
}
@Benchmark
@Threads(10 * AbstractBackendHolder.INDEX_COUNT)
public void writeBatch(WriteCounters counters) throws InterruptedException {
CompletableFuture<?>[] futures = new CompletableFuture<?>[batchSize];
for ( int i = 0; i < batchSize; ++i ) {
long documentId = getIndexPartition().toDocumentId( currentDocumentIdInThread++ );
futures[i] = indexer.add(
StubMapperUtils.referenceProvider( String.valueOf( documentId ) ),
document -> dataset.populate( index, document, documentId, 0L )
);
}
// Do not return until works are *actually* executed
Futures.unwrappedExceptionGet( CompletableFuture.allOf( futures ) );
counters.write += batchSize;
}
}