原文链接  译者:lijunshu

Netty有一个模块叫’netty-microbench’,我们可以用他来执行一系列的微型基准测试。Netty-microbench是基于OpenJDK JMH构件的(HotSpot的推荐基准测试方案)。当你开始netty基准测试时,你不需要额外的依赖。

运行基准测试

你可以通过maven 命令行或者直接从IDE开始你的基准测试,如果以默认设置开始跑测试,你可以使用命令行mvn -DskipTests=false。我们设置skipTests=false 是因为我们不想以单元测试的方式去运行这些基准测试。

如果没有问题,你将为看到JMH开始warmup然后基于fork数量来运行基准测试,并提供给你漂亮的报告。你会看到一般情况下基准测试的结果如下。

# Fork: 2 of 2

# Warmup: 10 iterations, 1 s each

# Measurement: 10 iterations, 1 s each

# Threads: 1 thread, will synchronize iterations

# Benchmark mode: Throughput, ops/time

# Running: io.netty.microbench.buffer.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_1_0

# Warmup Iteration   1: 8454.103 ops/ms

# Warmup Iteration   2: 11551.524 ops/ms

# Warmup Iteration   3: 11677.575 ops/ms

# Warmup Iteration   4: 11404.954 ops/ms

# Warmup Iteration   5: 11553.299 ops/ms

# Warmup Iteration   6: 11514.766 ops/ms

# Warmup Iteration   7: 11661.768 ops/ms

# Warmup Iteration   8: 11667.577 ops/ms

# Warmup Iteration   9: 11551.240 ops/ms

# Warmup Iteration  10: 11692.991 ops/ms

Iteration   1: 11633.877 ops/ms

Iteration   2: 11740.063 ops/ms

Iteration   3: 11751.798 ops/ms

Iteration   4: 11260.071 ops/ms

Iteration   5: 11461.010 ops/ms

Iteration   6: 11642.912 ops/ms

Iteration   7: 11808.595 ops/ms

Iteration   8: 11683.780 ops/ms

Iteration   9: 11750.292 ops/ms

Iteration  10: 11769.986 ops/ms

Result : 11650.238 ±(99.9%) 229.698 ops/ms

Statistics: (min, avg, max) = (11260.071, 11650.238, 11808.595), stdev = 169.080

Confidence interval (99.9%): [11420.540, 11879.937]

最终,你的测试结果看上去和这个相似,这个更多的取决于你的系统配置。

Benchmark                                                                Mode   Samples         Mean   Mean error    Units

i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_1_0          thrpt        20    11658.812      120.728   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_2_256        thrpt        20    10308.626      147.528   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_3_1024       thrpt        20     8855.815       55.933   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_4_4096       thrpt        20     5545.538     1279.721   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_5_16384      thrpt        20     6741.581       75.975   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledDirectAllocAndFree_6_65536      thrpt        20     7252.869       70.609   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_1_0            thrpt        20     9750.225       73.900   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_2_256          thrpt        20     9936.639      657.818   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_3_1024         thrpt        20     8903.130      197.533   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_4_4096         thrpt        20     6664.157       74.163   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_5_16384        thrpt        20     6374.924      337.869   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.pooledHeapAllocAndFree_6_65536        thrpt        20     6386.337       44.960   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_1_0        thrpt        20     2137.241       30.792   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_2_256      thrpt        20     1873.727       41.843   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_3_1024     thrpt        20     1902.025       34.473   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_4_4096     thrpt        20     1534.347       20.509   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_5_16384    thrpt        20      838.804       12.575   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledDirectAllocAndFree_6_65536    thrpt        20      276.976        3.021   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_1_0          thrpt        20    35820.568      259.187   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_2_256        thrpt        20    19660.951      295.012   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_3_1024       thrpt        20     6264.614       77.704   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_4_4096       thrpt        20     2921.598       95.492   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_5_16384      thrpt        20      991.631       49.220   ops/ms

i.n.m.b.ByteBufAllocatorBenchmark.unpooledHeapAllocAndFree_6_65536      thrpt        20      261.718       11.108   ops/ms

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 993.382 sec - in io.netty.microbench.buffer.ByteBufAllocatorBenchmark

你也可以直接从IDE运行基准测试。如果你导入了netty的最顶层项目,打开子项目microbench到子目录src/test/java/io/netty/microbench 。在buffer子目录,你可以像其它JUnit测试一样运行ByteBufAllocatorBenchmark 。主要的区别就是,你可以一下子运行全部的基准测试,而不是去一个个的运行每一个基准测试。如果maven一样,你可以在控制台看到同样的输出。

编写基准测试

虽然编写基准测试不是太麻烦,但是却不一定编写的正确。这不是因为microbench项目很难使用,而是因为你很难去避免一些常用的陷阱。因此,JMH提供了有用的annotation和特性来帮助你避免大多数的陷阱。为此,你需要使你的基准继承自AbstractMicrobenchmark,AbstractMicrobenchmark能保证以JUnit默认参赛的方式运行。

public class MyBenchmark extends AbstractMicrobenchmark {

}

下一步是创建一个方法以@GenerateMicroBenchmark 标注,然后给他一个合适的方法名。

@GenerateMicroBenchmark

public void measureSomethingHere() {

}

看这些样例是很好的主意能启发你如何去写JMH测试。你也可以看下JMH主要作者的一些座谈。

定制运行参数

默认的AbstractMicrobenchmark配置是

Warmup次数 10

测试次数 10

Fork数量 2

这些配置可以通过系统配置在运行的时候做配置

mvn -DskipTests=false -DwarmupIterations=2 -DmeasureIterations=3 -Dforks=1 test

需要注意通常并不建议跑测试时,用较少的循环次数,但是较少的次数有助于确认基准测试时工作的,在确认结束后,再运行大量的基准测试。

@Warmup(iterations = 20)

@Fork(1)

public class MyBenchmark extends AbstractMicrobenchmark {

}

这可以以方法级别或者类级别来运行基准测试,命令行的参数会覆盖annotation上的参数。

java测试netty_《Netty官方文档》基准测试相关推荐

  1. netty java开发文档_《Netty官方文档》设置开发环境

    原文链接 译者:dan   QQ:903585177 请注意:这个指南并不是用户指南,它是开发 Netty 本身的指南,而不是使用Netty 开发其他程序的指南. 使用64位操作系统 必须使用64位操 ...

  2. netty冲突 play sbt_《Netty官方文档》本地传输接口

    原文链接 译者:gm777 自4.016版本以来, Netty提供了本地的socket传输使Linux系统可以使用JNI(JAVA本地接口).这个传输接口不仅有着高性能并且产生更少的垃圾,所以你也许会 ...

  3. 基于docker安装部署Zabbix及使用,自动发现、日志监控报警及参照官方文档遇到的问题(图文)

    目录 一.相关原理 (1)概述 (2)Zabbix 功能 (3)Zabbix 概述 (4)zabbix监控架构详解 二.主要步骤 (一)工具/环境准备 (二)docker安装及测试 (三)docker ...

  4. RabbitMQ官方文档知识点总结合集+代码注释(中文+Java版)

    全文代码.MD格式文档的github连接(求star~):https://github.com/Ruoyi-Chen/RabbitMQ-demos 文章目录 全文代码.MD格式文档的github连接( ...

  5. Java官方文档居然挂羊头,卖狗肉,我的错?

    今天闲来无事,研究了一下java线程的状态切换,发现一个匪夷所思的事情. 简单来说,就是Java官方文档描述与现实不符. Java官方文档说,调用Thread.wait(timeout)方法时,线程会 ...

  6. 渗透测试 ( 8 ) --- Burp Suite Pro 官方文档、Brida(frida)

    Burp Suite 官网 :https://portswigger.net/burp 官方文档:https://portswigger.net/burp/documentation/desktop ...

  7. 深入理解Java 8 Lambda表达式(Oracle官方文档版)

    Java 8 问世三年了,9马上也要问世了,所以,嗯,我要开始学8了-- 官方文档:http://docs.oracle.com/javase/tutorial/java/javaOO/lambdae ...

  8. Spring 4 官方文档学习 Spring与Java EE技术的集成

    本部分覆盖了以下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Ent ...

  9. 【Java基础】2020如何查看Java官方文档

    一.首先百度搜索oracle进入oracle公司官网(注意是官网). 二.点击首页最下角的developers(开发者). 三.点击开发者页面中间部分的technologies(技术),点击java. ...

最新文章

  1. 机器学习与深度学习常见面试问题与答案
  2. iOS 中KVC、KVO、NSNotification、delegate 总结及区别
  3. Python : 什么是*args和**kwargs[转载]
  4. C#中使用StreamReader实现文本文件的读取与写入
  5. 这个3月我有3场见面会,不知道你来不来?
  6. c++代码好玩_Py之pygame:有趣好玩—利用pygame库实现鱼儿自动实时目标跟踪(附完整代码)...
  7. html5 swiper 菜鸟,前端插件swiper使用新手教程
  8. 初识kbmmw 中的ORM
  9. 欧几里得算法及扩展欧几里得算法简单解释
  10. TurboMail助某水利委员会邮件通讯更新换血
  11. 坑爹!Quartz 重复调度问题,你遇到过么?
  12. opencv获取人脸眼镜位置_用opencv检测人眼并定位瞳孔位置
  13. 用SPSS搞定问卷调查中的决断值
  14. intel安装mac os
  15. 简单系统U盘制作 / 安装系统
  16. 笔试题(求一组数中能构成三角形的个数,不能重复)
  17. 201771010101 白玛次仁 《2018面向对象程序设计(Java)》第九周学习总结
  18. SQLMAP进阶:参数讲解
  19. java定时器整点报时_单片机 整点报时 定时小闹钟程序
  20. Spring基础之IOCAOPDI

热门文章

  1. JBoss AS 7.1.0.Final“ Thunder”发布-Java EE 6 Full Profile认证!
  2. Bootstrap的学习
  3. MySQL的存储过程和函数简单写法
  4. java请求并行方案_让 Yar Java Client 支持执行并行请求,ExecutorService 的使用
  5. visual2019没有勾选的在如何加入_发票管理系统完成升级,勾选认证平台改头换面,你所有的问题都在这里!...
  6. c语言文件的读写通讯录,学C三个月了,学了文件,用C语言写了个通讯录程序
  7. git gui 历史版本_这些Git命令都不会,还是不要去面试了
  8. dalsa工业相机8k参数_工业传感器再掀巨浪 | Teledyne 以80亿美元收购FLIR,互补性产品组合又增体量...
  9. android popupwindow 调用方法,Android PopUpWindow使用详解
  10. HTML语言name,HTML a 标签的 name 属性