下面列出了怎么用org.openjdk.jmh.annotations.Mode的API类实例代码及写法,或者点击链接到github查看源代码。
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public IdMap<FixedIntArray> benchmarkOffHeapPreSizeWithoutCache()
throws IOException {
PinotDataBufferMemoryManager memoryManager = new DirectMemoryManager("perfTest");
IdMap<FixedIntArray> idMap =
new FixedIntArrayOffHeapIdMap(CARDINALITY, 0, NUM_COLUMNS, memoryManager, "perfTestWithCache");
for (FixedIntArray value : _values) {
idMap.put(value);
}
memoryManager.close();
return idMap;
}
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(
iterations = 10
)
@Measurement(
iterations = 10
)
@Fork(1)
public void vectorXOps() {
vectorX.map(i -> i * 2)
.concatMap(i->Vector.range(0,10))
.map(i -> i * 2)
.filter(i -> i < 5000)
.map(i -> "hello " + i)
.zip(Vector.range(0,1000000))
.map(i->i._1())
.map(i -> i.length())
.foldLeft((a, b) -> a + b);
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void forwardChainingSchemaCachingRDFSInferencer() throws IOException {
SailRepository sail = new SailRepository(new SchemaCachingRDFSInferencer(new MemoryStore()));
try (SailRepositoryConnection connection = sail.getConnection()) {
connection.begin();
connection.add(resourceAsStream("schema.ttl"), "", RDFFormat.TURTLE);
addAllDataSingleTransaction(connection);
connection.commit();
}
checkSize(sail);
}
/**
* Run the benchmark with all the examples in BenchmarkUtil.SPEC_EXAMPLE_NAMES
*/
public Collection<RunResult> runAll() throws RunnerException {
Options opt = new OptionsBuilder()
.include(".*" + benchmarkClass.getSimpleName() + ".*")
.jvmArgsPrepend("-Xms4g", "-Xmx4g")
.jvmArgsAppend(properties.toArray(new String[properties.size()]))
.verbosity(VerboseMode.NORMAL)
.warmupIterations(1)
.warmupTime(TimeValue.seconds(10))
.measurementIterations(1)
.measurementTime(TimeValue.seconds(10))
.shouldDoGC(true)
.forks(1)
.output("results.txt")
.mode(Mode.SingleShotTime)
.param("exampleName", BenchmarkUtil.SPEC_EXAMPLE_NAMES.toArray(new String[0])) // https://stackoverflow.com/a/4042464/161022
.build();
return new Runner(opt).run();
}
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public Integer threadIdMap() {
Long key = Thread.currentThread().getId();
Integer value = threadIdMap.get(key);
if (value != null) {
return value;
}
value = 100;
threadIdMap.put(key, value);
return value;
}
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@SuppressWarnings("deprecation")
public byte[] getBytesAsciiWithBuffer() throws Exception {
boolean ascii = false;
if (isLatin1Method != null) {
try {
ascii = (boolean) isLatin1Method.invoke(str);
} catch (Exception e) {
ascii = false;
}
}
if (ascii) {
byte[] buffer = bufferHolder.get();
str.getBytes(0, str.length(), buffer, 0);
return buffer;
} else {
return str.getBytes(StandardCharsets.UTF_8);
}
}
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public byte loopReadByteBytes() {
byte value = 0;
for (int i = 0; i < length; i++) {
value = bytes[i];
}
return value;
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void sixteenBitBulkContiguous() {
for (int startIndex = 0; startIndex < ROWS; startIndex += 32) {
_bitSet16.readInt(startIndex, 16, 32, unpacked);
}
}
/**
* Checkstyle.
*/
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void grpcHeaders_clientHandler(Blackhole bh) {
clientHandler(bh, new GrpcHttp2ResponseHeaders(2));
}
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Override
public Object getUser() throws Exception {
return super.getUser();
}
@Benchmark
@BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public MotanUser getUser() throws Exception {
int id = counter.getAndIncrement();
return userService.getUser(id);
}
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void nettyByteBufPage() {
nettyByteBuf.clear();
nettyByteBufOutput.setBuffer(nettyByteBuf);
kryo.writeObject(nettyByteBufOutput, page);
nettyByteBufInput.setBuffer(nettyByteBuf);
kryo.readObject(nettyByteBufInput, Page.class);
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int defaultClientHeaders() {
Http2Headers headers = new DefaultHttp2Headers(false);
for (int i = 0; i < headerCount; ++i) {
headers.add(headerNames[i], headerValues[i]);
}
headers.method(HttpMethod.POST.asciiName());
headers.scheme(HttpScheme.HTTPS.name());
headers.path(path);
headers.authority(authority);
return iterate(headers);
}
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void serializeUserList2() throws Exception {
nioBuffer.clear();
listBuffer.clear();
mapper.writeValue(nioOutputStream, userPage);
nioBuffer.flip();
listBuffer.writeBytes(nioBuffer);
}
@Benchmark
@BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Override
public User getUser() throws Exception {
return super.getUser();
}
@Benchmark
@BenchmarkMode(Mode.All)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void testTheMethod4(TestState state, Blackhole blackhole) {
SomeClass someClass = new SomeClass();
blackhole.consume(someClass.someMethod(state.m, state.s));
}
@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
public void queryPerson() {
try (Result<Person> results = data.select(Person.class).limit(10000).get()) {
for (Person p : results) {
p.getName();
}
}
}
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void loopSetDirectByteBuffer() {
directByteBuffe.clear();
for (int i = 0; i < length; i++) {
directByteBuffe.put((byte) i);
}
}
@CompilerControl(CompilerControl.Mode.INLINE)
private static void clientHandler(Blackhole bh, Http2Headers headers) {
// NettyClientHandler does not directly access headers, but convert to Metadata immediately.
bh.consume(headers.add(responseHeaders[0], responseHeaders[1]));
bh.consume(headers.add(responseHeaders[2], responseHeaders[3]));
bh.consume(Utils.convertHeaders(headers));
}
/**
* This benchmark attempts to measure performance of {@link TextFormat#extract(Object, Getter)}.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public SpanContext extract(Data data) throws SpanContextParseException {
return data.textFormatBase.extract(data.spanContextHeaders);
}
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public int benchmarkStringDictionaryCreation()
throws IOException {
try (SegmentDictionaryCreator dictionaryCreator = new SegmentDictionaryCreator(_sortedStrings, STRING_FIELD,
INDEX_DIR)) {
dictionaryCreator.build();
return dictionaryCreator.indexOfSV("0");
}
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(JMHBenchmark.class.getSimpleName())
.forks(1)
.warmupIterations(5)
.measurementIterations(10)
.mode(Mode.Throughput)
.build();
new Runner(opt).run();
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void http2Iterate(Blackhole bh) {
for (Entry<CharSequence, CharSequence> entry : http2Headers) {
bh.consume(entry);
}
}
/** Record batched double last value measures. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public MeasureMap recordBatchedDoubleLastValue(Data data) {
MeasureMap map = data.recorder.newMeasureMap();
for (int i = 0; i < data.numValues; i++) {
map.put(StatsBenchmarksUtil.DOUBLE_LASTVALUE_MEASURES[i], (double) i);
}
map.record(data.tags);
return map;
}
@Warmup(iterations = 5)
@Measurement(iterations = 10)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(value = 1)
@Threads(8)
@Benchmark
public void put(Blackhole blackhole,BenchmarkState state,ThreadState threadState) {
for (int i = 0; i < threadState.numRows; ++i) {
Map.Entry<String, String> pair = threadState.rows.get(i);
state.oakMap.zc().put(pair.getKey(), pair.getValue());
blackhole.consume(state.oakMap);
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void twoBitContiguous() {
for (int startIndex = 0; startIndex < ROWS; startIndex++) {
_bitSet2.readInt(startIndex, 2);
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int defaultTrailers() {
Http2Headers headers = new DefaultHttp2Headers(false);
for (int i = 0; i < headerCount; ++i) {
headers.add(headerNames[i], headerValues[i]);
}
return iterate(headers);
}
/**
* Javadoc comment.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Status codeDecode() {
return Status.CODE_KEY.parseBytes("15".getBytes(Charset.forName("US-ASCII")));
}
@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void batchSetByteBuffer() {
byteBuffe.clear();
byteBuffe.put(bytes);
}
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public int benchmarkOffHeapPreSizeWithOverflow()
throws IOException {
try (LongOffHeapMutableDictionary dictionary = new LongOffHeapMutableDictionary(CARDINALITY, 1000, _memoryManager,
"longColumn")) {
int value = 0;
for (Long colValue : _colValues) {
value += dictionary.index(colValue);
}
return value;
}
}