Java – how to write a small benchmark for GetBytes in jmh?
•
Java
I'm new to jmh and benchmarking I have written a small test of the public byte [] GetBytes (string charsetname) method However, Maven cannot be established This is my code:
package org.openjdk.jmh.samples; @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @State(Scope.Thread) public class Test { @GenerateMicroBenchmark public byte [] testgetbyte (String Str) { byte[] bytes = Str.getBytes("ISO-8859-1"); return bytes; } public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(Test.class.getSimpleName()) .warmupIterations(5) .measurementIterations(5) .forks(1) .build(); new Runner(opt).run(); } }
When I run: MVN clean install I received a build failure error message
Solution
@Generatemicrobenchmark seems to indicate that you are using an old version of jmh. Please update it Using jmh 1.7, this example:
@BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @State(Scope.Thread) public class Test { @Benchmark public byte[] test(String str) throws UnsupportedEncodingException { return str.getBytes("ISO-8859-1"); } }
... said:
[ERROR] /home/shade/temp/stest/src/main/java/org/openjdk/Test.java: [19,19] Method parameters should be @State classes.
This just shows that string STR is an unacceptable @ benchmark parameter Think about it: what string should jmh use to call this method? Who will produce it? What does it contain? These answers should be provided by the user through the @ state class, such as jmhsample_ 03_ States and jmhsample_ 04_ As described in defaultstate
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码