下面列出了怎么用org.openjdk.jmh.annotations.Benchmark的API类实例代码及写法,或者点击链接到github查看源代码。
@Benchmark
@OperationsPerInvocation(POSITIONS)
public Object bigintGroupByHash(SingleChannelBenchmarkData data)
{
GroupByHash groupByHash = new BigintGroupByHash(0, data.getHashEnabled(), EXPECTED_SIZE, NOOP);
data.getPages().forEach(p -> groupByHash.addPage(p).process());
ImmutableList.Builder<Page> pages = ImmutableList.builder();
PageBuilder pageBuilder = new PageBuilder(groupByHash.getTypes());
for (int groupId = 0; groupId < groupByHash.getGroupCount(); groupId++) {
pageBuilder.declarePosition();
groupByHash.appendValuesTo(groupId, pageBuilder, 0);
if (pageBuilder.isFull()) {
pages.add(pageBuilder.build());
pageBuilder.reset();
}
}
pages.add(pageBuilder.build());
return pageBuilder.build();
}
@Benchmark
public void usingInlineStringBuilder(
BenchmarkBlockDataToString state, Blackhole sink) {
for (int i = 0; i < state.count; i++) {
BlockData item = state.data.get(i);
BlockID blockID = item.getBlockID();
ContainerBlockID containerBlockID = blockID.getContainerBlockID();
String str = new StringBuilder(capacity)
.append("[")
.append("blockId=")
.append("conID: ")
.append(containerBlockID.getContainerID())
.append(" locID: ")
.append(containerBlockID.getLocalID())
.append(" bcsId: ")
.append(blockID.getBlockCommitSequenceId())
.append(",size=")
.append(item.getSize())
.append("]")
.toString();
sink.consume(str);
Preconditions.checkArgument(str.equals(state.values.get(i)));
}
}
@Benchmark
public void writeAllPixelsByIndex() {
batches.elements(0).forEach(batch ->
pixels.elements(0).forEachIndexed((coords, pixel) -> {
long pixelIndex = coords[0];
batch
.setFloat(pixel.getFloat(0), 0, pixelIndex)
.setFloat(pixel.getFloat(1), 1, pixelIndex)
.setFloat(pixel.getFloat(2), 2, pixelIndex);
})
);
}
@Benchmark
public Object readIntWithNull(IntegerWithNullBenchmarkData data)
throws Exception
{
try (OrcRecordReader recordReader = data.createRecordReader()) {
return readFirstColumn(recordReader);
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(3)
public long racyGetAndSetAndGetGeneration() {
long oldGeneration = generator.getGeneration();
generator.setGeneration(oldGeneration + 1);
return generator.getGeneration();
}
@Benchmark
@Measurement(batchSize = 1000)
public void initTensorByFlatArray() {
IntDataBuffer data = DataBuffers.of(
0, 0, 0,
0, 0, 1,
0, 0, 2,
0, 1, 0,
0, 1, 1,
0, 1, 2,
0, 2, 0,
0, 2, 1,
0, 2, 2,
1, 0, 0,
1, 0, 1,
1, 0, 2,
1, 1, 0,
1, 1, 1,
1, 1, 2,
1, 2, 0,
1, 2, 1,
1, 2, 2,
2, 0, 0,
2, 0, 1,
2, 0, 2,
2, 1, 0,
2, 1, 1,
2, 1, 2,
2, 2, 0,
2, 2, 1,
2, 2, 2
);
TInt32.tensorOf(Shape.of(3, 3, 3, 3), data);
}
@Benchmark
public int hashCodeBenchCharSequenceOld() {
int h = 0;
for (int i = 0; i < string.length(); ++i) {
// masking with 0x1F reduces the number of overall bits that impact the hash code but makes the hash
// code the same regardless of character case (upper case or lower case hash is the same).
h = h * 31 + (string.charAt(i) & 0x1F);
}
return h;
}
@Benchmark
public void defaultPooledDirectAllocAndFree() {
int idx = rand.nextInt(defaultPooledDirectBuffers.length);
ByteBuf oldBuf = defaultPooledDirectBuffers[idx];
if (oldBuf != null) {
oldBuf.release();
}
defaultPooledDirectBuffers[idx] = PooledByteBufAllocator.DEFAULT.directBuffer(size);
}
@Benchmark
public void writeMultipleFiles(BenchmarkState state, Blackhole sink)
throws StorageContainerException {
ChunkManager chunkManager = new FilePerChunkStrategy(true, null);
benchmark(chunkManager, FILE_PER_CHUNK, state, sink);
}
@Benchmark
public void useIsValidIpv4() {
for (String host : invalidIpV4Hosts.keySet()) {
if (NetUtil.isValidIpV4Address(host)) {
throw new RuntimeException("error");
}
}
}
@Benchmark
public boolean lookupInList(FHIRValueSetState state) throws Exception {
for (ValueSet.Expansion.Contains concept : state.valueSet.getExpansion().getContains()) {
if (state.concept.getCode().equals(concept.getCode()) && state.concept.getSystem().equals(concept.getSystem())) {
return true;
}
}
return false;
}
@Benchmark
public void usingToStringBuilderDefaultCapacity(
BenchmarkBlockDataToString state, Blackhole sink) {
for (int i = 0; i < state.count; i++) {
BlockData item = state.data.get(i);
String str = new ToStringBuilder(item, ToStringStyle.NO_CLASS_NAME_STYLE)
.append("blockId", item.getBlockID().toString())
.append("size", item.getSize())
.toString();
sink.consume(str);
Preconditions.checkArgument(str.equals(state.values.get(i)));
}
}
@Benchmark
@OperationsPerInvocation(3)
public Object baselineLength3(BenchmarkData data)
throws Exception
{
data.input.setPosition(0);
unpackGeneric(data.buffer, 0, 3, data.bits, data.input);
return data.buffer;
}
/**
* Javadoc comment.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int attachDetach() {
Context old = cu.attach();
try {
return key.get();
} finally {
Context.current().detach(old);
}
}
@Benchmark
@Fork(1)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 10, time = 1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Threads(value = 1)
public void aggregate_1Threads() {
aggregator.recordDouble(100.0056);
}
/**
* Checkstyle.
*/
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void grpcHeaders_clientHandler(Blackhole bh) {
clientHandler(bh, new GrpcHttp2ResponseHeaders(2));
}
@Benchmark
@OperationsPerInvocation(POSITIONS)
public List<Optional<Page>> mapConcat(BenchmarkData data)
{
return ImmutableList.copyOf(
data.getPageProcessor().process(
SESSION,
new DriverYieldSignal(),
newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()),
data.getPage()));
}
/** Benchmark for measuring inject with default trace state and sampled trace options. */
@Benchmark
@BenchmarkMode({Mode.AverageTime})
@Fork(1)
@Measurement(iterations = 15, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
@OperationsPerInvocation(COUNT)
public Map<String, String> measureInject() {
for (int i = 0; i < COUNT; i++) {
httpTraceContext.inject(contexts.get(i), carrier, setter);
}
return carrier;
}
@Benchmark
public int fastThreadLocal() {
int result = 0;
for (FastThreadLocal<Integer> i: fastThreadLocals) {
result += i.get();
}
return result;
}
/**
* Measure throughput of unary calls. The calls are already running, we just observe a counter
* of received responses.
*/
@Benchmark
public void pingPong(AdditionalCounters counters) throws Exception {
record.set(true);
// No need to do anything, just sleep here.
Thread.sleep(1001);
record.set(false);
}
@Benchmark
public Object overflowChecksSubtract()
{
long result = 0;
result += BigintOperators.subtract(leftOperand0, rightOperand0);
result += BigintOperators.subtract(leftOperand1, rightOperand0);
result += BigintOperators.subtract(leftOperand2, rightOperand0);
result += BigintOperators.subtract(leftOperand3, rightOperand0);
result += BigintOperators.subtract(leftOperand4, rightOperand0);
result += BigintOperators.subtract(leftOperand0, rightOperand1);
result += BigintOperators.subtract(leftOperand1, rightOperand1);
result += BigintOperators.subtract(leftOperand2, rightOperand1);
result += BigintOperators.subtract(leftOperand3, rightOperand1);
result += BigintOperators.subtract(leftOperand4, rightOperand1);
result += BigintOperators.subtract(leftOperand0, rightOperand2);
result += BigintOperators.subtract(leftOperand1, rightOperand2);
result += BigintOperators.subtract(leftOperand2, rightOperand2);
result += BigintOperators.subtract(leftOperand3, rightOperand2);
result += BigintOperators.subtract(leftOperand4, rightOperand2);
result += BigintOperators.subtract(leftOperand0, rightOperand3);
result += BigintOperators.subtract(leftOperand1, rightOperand3);
result += BigintOperators.subtract(leftOperand2, rightOperand3);
result += BigintOperators.subtract(leftOperand3, rightOperand3);
result += BigintOperators.subtract(leftOperand4, rightOperand3);
result += BigintOperators.subtract(leftOperand0, rightOperand4);
result += BigintOperators.subtract(leftOperand1, rightOperand4);
result += BigintOperators.subtract(leftOperand2, rightOperand4);
result += BigintOperators.subtract(leftOperand3, rightOperand4);
result += BigintOperators.subtract(leftOperand4, rightOperand4);
return result;
}
/**
* Adding custom call options, overwritting existing keys.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public CallOptions withOptionDuplicates() {
CallOptions opts = allOpts;
for (int i = 1; i < shuffledCustomOptions.size(); i++) {
opts = opts.withOption(shuffledCustomOptions.get(i), "value2");
}
return opts;
}
@Benchmark
@OperationsPerInvocation(ITERATIONS)
public void nanoTime(Blackhole blackhole)
{
for (int i = 0; i < ITERATIONS; i++) {
blackhole.consume(System.nanoTime());
}
}
@Benchmark
public List<Optional<Page>> benchmark()
{
return ImmutableList.copyOf(
processor.process(
SESSION,
new DriverYieldSignal(),
newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()),
inputPage));
}
@Benchmark
public Slice benchmarkSubstringStart(BenchmarkData data)
{
Slice slice = data.getSlice();
int length = data.getLength();
return substring(slice, (length / 2) - 1);
}
@Benchmark
@OperationsPerInvocation(POSITIONS)
public List<Optional<Page>> mapSubscript(BenchmarkData data)
{
return ImmutableList.copyOf(
data.getPageProcessor().process(
SESSION,
new DriverYieldSignal(),
newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()),
data.getPage()));
}
@Benchmark
public void unpooledDirectAllocAndFree() {
int idx = rand.nextInt(unpooledDirectBuffers.length);
ByteBuf oldBuf = unpooledDirectBuffers[idx];
if (oldBuf != null) {
oldBuf.release();
}
unpooledDirectBuffers[idx] = unpooledAllocator.directBuffer(size);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MINUTES)
@Warmup(iterations = 10, time = 3, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 50, time = 3, timeUnit = TimeUnit.SECONDS)
@Threads(1)
public void testBasicStringSplit(Blackhole blackhole, MyState state) throws IOException {
BufferedReader bf = new BufferedReader(new StringReader(state.eventListStr));
StringBuilder sb = new StringBuilder();
String line;
List<String> msseList = new ArrayList<>();
int dollarCnt = 0;
while ((line = bf.readLine()) != null) {
for (int i = 0; i < line.length(); i++) {
if (dollarCnt == 3) {
msseList.add(sb.toString());
dollarCnt = 0;
sb = new StringBuilder();
}
if (line.charAt(i) != '$') {
sb.append(line.charAt(i));
} else {
dollarCnt++;
}
}
}
blackhole.consume(msseList);
//blackhole.consume(state.eventListStr.split("$$"));
//state.sum = state.a + state.b;
}
@Benchmark
@OperationsPerInvocation(NUMBER_OF_ENTRIES)
public ReferenceCountMap benchmarkInserts(Data data)
{
ReferenceCountMap map = new ReferenceCountMap();
for (int i = 0; i < NUMBER_OF_ENTRIES; i++) {
map.incrementAndGet(data.slices[i]);
map.incrementAndGet(data.slices[i].getBase());
}
return map;
}
@Benchmark
public Object overflowChecksAdd()
{
long result = 0;
result += BigintOperators.add(leftOperand0, rightOperand0);
result += BigintOperators.add(leftOperand1, rightOperand0);
result += BigintOperators.add(leftOperand2, rightOperand0);
result += BigintOperators.add(leftOperand3, rightOperand0);
result += BigintOperators.add(leftOperand4, rightOperand0);
result += BigintOperators.add(leftOperand0, rightOperand1);
result += BigintOperators.add(leftOperand1, rightOperand1);
result += BigintOperators.add(leftOperand2, rightOperand1);
result += BigintOperators.add(leftOperand3, rightOperand1);
result += BigintOperators.add(leftOperand4, rightOperand1);
result += BigintOperators.add(leftOperand0, rightOperand2);
result += BigintOperators.add(leftOperand1, rightOperand2);
result += BigintOperators.add(leftOperand2, rightOperand2);
result += BigintOperators.add(leftOperand3, rightOperand2);
result += BigintOperators.add(leftOperand4, rightOperand2);
result += BigintOperators.add(leftOperand0, rightOperand3);
result += BigintOperators.add(leftOperand1, rightOperand3);
result += BigintOperators.add(leftOperand2, rightOperand3);
result += BigintOperators.add(leftOperand3, rightOperand3);
result += BigintOperators.add(leftOperand4, rightOperand3);
result += BigintOperators.add(leftOperand0, rightOperand4);
result += BigintOperators.add(leftOperand1, rightOperand4);
result += BigintOperators.add(leftOperand2, rightOperand4);
result += BigintOperators.add(leftOperand3, rightOperand4);
result += BigintOperators.add(leftOperand4, rightOperand4);
return result;
}