JMH 测试「建议收藏」

JMH 测试「建议收藏」JHM测试框架

大家好,又见面了,我是你们的朋友全栈君。

JMH的作用

代码工具:jmh


JMH是一个用于构建,运行和分析以Java和其他语言编写的面向JVM的nano / micro / milli / macro基准测试的Java代码库。

相关注解:


//————————————

@State注释定义了给定类的实例可用的范围(JMH允许您同时在多个线程中运行测试)

名称
        描述

Scope.Thread
这是一个默认状态。将为运行给定测试的每个线程分配一个实例。

Scope.Benchmark
运行相同测试的所有线程将共享一个实例。可以用来测试状态对象的多线程性能(或者只是用这个范围标记你的基准)。

Scope.Group
将为每个线程组分配一个实例(请参阅下面的组部分)。

//————————

@BenchmarkMode在测试方法中使用注释指定的以下测试模式:

名称
                 描述

Mode.Throughput
         以时间单位计算操作次数。

Mode.AverageTime
计算平均运行时间。

Mode.SampleTime
         计算运行方法需要多长时间(包括百分位数)。

Mode.SingleShotTime
只需运行一次方法(对冷测试模式有用)。或者如果您为迭代指定了批处理大小,则不止一次(参                           见@Measurement下面的注释) – 在这种情况下,JMH将计算批处理运行时间(批处理中所有调用的
总时间)。

任何一组这些模式
您可以指定任何一组这些模式 – 测试将运行多次(取决于所需模式的数量)。

Mode.All
         所有这些模式一个接一个。

//———————————-

Junit测试

@Setup

完成测试前的初始化工作

@teardown 完成测试完成后的垃圾回收等工作

//————————————-

@OutputTimeUnit     指定时间单位

@OutputTimeUnit(TimeUnit.SECONDS)

TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段

TimeUnit.DAYS          //天

TimeUnit.HOURS         //小时

TimeUnit.MINUTES       //分钟

TimeUnit.SECONDS       //秒

TimeUnit.MILLISECONDS  //毫秒


在Eclipse 中实现一个JMH测试

1.创建maven项目

   1.pom文件:
       <dependency>


<groupId>org.openjdk.jmh</groupId>


<artifactId>jmh-core</artifactId>


<version>1.19</version>


</dependency>


<dependency>


<groupId>org.openjdk.jmh</groupId>


<artifactId>jmh-generator-annprocess</artifactId>


<version>1.19</version>


<scope>provided</scope>


</dependency>
           添加插件
             <plugin>


    <groupId>org.codehaus.mojo</groupId>


    <artifactId>exec-maven-plugin</artifactId>


    <executions>


        <execution>


            <id>run-benchmarks</id>


            <phase>integration-test</phase>


            <goals>


                <goal>exec</goal>


            </goals>


            <configuration>


                <classpathScope>test</classpathScope>


                <executable>java</executable>


                <arguments>


                    <argument>-classpath</argument>


                    <classpath />


                    <argument>org.openjdk.jmh.Main</argument>


                    <argument>.*</argument>


                </arguments>


            </configuration>


        </execution>


    </executions>


</plugin>
       2.代码编写
           package com.ns.proxy.jmhtest;

import java.io.IOException;

import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.openjdk.jmh.annotations.Benchmark;

import org.openjdk.jmh.annotations.BenchmarkMode;

import org.openjdk.jmh.annotations.Mode;

import org.openjdk.jmh.annotations.OutputTimeUnit;

import org.openjdk.jmh.annotations.Scope;

import org.openjdk.jmh.annotations.Setup;

import org.openjdk.jmh.annotations.State;

import org.openjdk.jmh.annotations.TearDown;

import org.openjdk.jmh.runner.Runner;

import org.openjdk.jmh.runner.RunnerException;

import org.openjdk.jmh.runner.options.Options;

import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.alibaba.fastjson.JSONObject;

@State(Scope.Benchmark) // 给定类的可用范围

@BenchmarkMode(Mode.Throughput) // 指定测试模式

@OutputTimeUnit(TimeUnit.SECONDS) // 指定时间单位

public class JMHTestProxy {


String url = “”;


String dubbo = “”;


JSONObject entity = new JSONObject();


public static void main(String[] args) throws RunnerException {


Options opt = new OptionsBuilder()//


.include(JMHTestProxy.class.getSimpleName())//


.warmupIterations(2)// 预热次数


.measurementIterations(10)// 真正执行的次数


.forks(1).build();


new Runner(opt).run();


}


@Setup


public void setup() {


url = “http://192.168.201.162:10007/web/”;


dubbo = “http://192.168.201.162:10009/trsdc/tradition/search”;


entity.put(“pageNo”, 1);


entity.put(“query”,


“IR_CONTENT:((\”中国比特币\” OR \”中国比特币\” OR \”中国比特币\”)的) AND IR_LOADTIME:[20151207 TO 201712018]”);


entity.put(“pageSize”, 1);


}


/**


* 需要测试的方法


*/


@Benchmark


public void bufferLoop() {





}


@TearDown


public void getAvgOut() {


}

}

 2.编译

   项目代码完成后不要直接执行:
      在eclipse中runas:


1.maven clean 


2.maven generator-source


3.maven install
  以此来创建BenchmarkList文件



3.遇到的错误

可能一:

一台主机只能运行一个jmh的Java项目

Exception in thread “main” org.openjdk.jmh.runner.RunnerException: ERROR: Unable to acquire the JMH lock (C:\Users\ADMINI~1\AppData\Local\Temp\/jmh.lock): already taken by another JMH instance, exiting. Use -Djmh.ignoreLock=true to forcefully continue.


at org.openjdk.jmh.runner.Runner.run(Runner.java:202)


at com.ns.bloomfilter.JMHTestBloomFilterFile.main(JMHTestBloomFilterFile.java:34)

可能二:

应该是windows权限导致的,java无法在C:\Windows目录下创建文件。两种解决方案。一种是以管理员权限启动intellij,另一种是如错误提示所说,通过-D参数来修改java的系统属性。我选择了前者略微简单一点。

//缺少插件

Exception in thread “main” java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList


at org.openjdk.jmh.runner.AbstractResourceReader.getReaders(AbstractResourceReader.java:98)


at org.openjdk.jmh.runner.BenchmarkList.find(BenchmarkList.java:122)


at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:256)


at org.openjdk.jmh.runner.Runner.run(Runner.java:206)


at Example.example.JMHSample_01_HelloWorld.main(JMHSample_01_HelloWorld.java:32)

解决办法:

<plugin>


<groupId>org.codehaus.mojo</groupId>


<artifactId>exec-maven-plugin</artifactId>


<executions>


<execution>


<id>run-benchmarks</id>


<phase>integration-test</phase>


<goals>


<goal>exec</goal>


</goals>


<configuration>


<classpathScope>test</classpathScope>


<executable>java</executable>


<arguments>


<argument>-classpath</argument>


<classpath />


<argument>org.openjdk.jmh.Main</argument>


<argument>.*</argument>


</arguments>


</configuration>


</execution>


</executions>


</plugin>


</plugins>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157354.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 整合Druid—SpringBoot[通俗易懂]

    整合Druid—SpringBoot[通俗易懂]整合Druid(数据源)Druid简介Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。Druid是阿里巴巴开源平台上一个数据库连接池实现,结合了C3P0、DBCP等DB池的优点,同时加入了日志监控。Druid可以很好的监控DB池连接和SQL的执行情况,天生就是针对监控而生的DB连接池。Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验。SpringBoot2.0以上默认使用Hikari

    2022年7月23日
    13
  • android学习笔记之ImageView的scaleType属性

    android学习笔记之ImageView的scaleType属性我们知道,ImageView有一个属性叫做scaleType,它的取值一共有八种,分别是:matrix,fitXY,fitStart,fitCenter,fitEnd,center,centerCrop,centerInside。那我们下面一起来看看这八种取值分别代表什么意思。我用两张图片来做demo,这两张图片的分辨率一大一小,图片分别叫做big和small。原图如下:big:small:OK,

    2022年6月15日
    28
  • 免费空间推荐「建议收藏」

    免费空间推荐「建议收藏」免费空间使用googiehost免备案地址:

    2022年6月22日
    27
  • Windows服务器如何修改SQL server内存大小等设置

    Windows服务器如何修改SQL server内存大小等设置

    2021年6月1日
    115
  • sizeof和strlen的区别(strlen和sizeof的用法)

    charstr[20]=”0123456789″;int  a=strlen(str);/*a=10;strlen计算字符串的长度,以�为字符串结束标记。int  b=sizeof(str);/*b=20;sizeof计算的则是分配的数组str[20]所占的内存空间的大小,不受里面存储的内容影响========================================

    2022年4月14日
    43
  • 机器学习:回归问题

    机器学习:回归问题回归,我第一次看到回归的时候,想的就是回归是什么意思?后来看了一个答案解释很有意思,回归这个词来自于生物学,在调查父母与子代身高问题的时候,发现父母如果过高的话,子女就会比父母矮一点,如果父母矮的话,

    2022年8月6日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号