Skip to content

Instantly share code, notes, and snippets.

@matrixcloud
Last active November 11, 2022 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matrixcloud/10fae5dd2dffade3cb28bf31c6acc910 to your computer and use it in GitHub Desktop.
Save matrixcloud/10fae5dd2dffade3cb28bf31c6acc910 to your computer and use it in GitHub Desktop.
JMH Benchmark Sample
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 5, time = 5)
@Threads(4)
@Fork(1)
@State(value = Scope.Benchmark)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class KK {
private final XmlMapper xmlMapper;
private final String content;
{
xmlMapper = new Jackson2ObjectMapperBuilder().createXmlMapper(true).indentOutput(true).build();
xmlMapper.enable(ToXmlGenerator.Feature.WRITE_XML_1_1);
xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE);
content = ResourceLoader.read(new ClassPathResource("cw/shipment_req.xml"));
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(KK.class.getSimpleName())
.result("/Users/cwa166/Downloads/Benchmark.json")
.resultFormat(ResultFormatType.JSON).build();
new Runner(opt).run();
}
@Benchmark
public void testXmlMapper() throws JsonProcessingException {
ShipmentReq base = ShipmentReq.base();
base.getShipmentRequest().getDataContext().getDataTargetCollection().get(0).setType("T1");
base.getShipmentRequest().getDataContext().getDataTargetCollection().get(0).setKey("k1");
base.getShipmentRequest().getDataContext().getCompany().setCode("BBC");
base.getShipmentRequest().getDataContext().setServerID("server1");
xmlMapper.writeValueAsString(base);
}
@Benchmark
public void testResourceLoader() {
ResourceLoader.interpolate(content, "T1", "K1", "BBC", "server1");
}
}
@matrixcloud
Copy link
Author

Benchmark                       Mode  Cnt     Score      Error   Units
KK.testResourceLoader  avgt     5   5812.807 ± 261.780   ns/op
KK.testXmlMapper        avgt     5   2910.028 ± 177.324   ns/op

Refs: https://www.cnblogs.com/54chensongxia/p/15485421.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment