zoukankan      html  css  js  c++  java
  • Jmeter java request : java.lang.ClassNotFoundException 问题

    1. pom build 必须按下面的设置, 打包出 with-dependenciesjar, 并把这个xxx-with-dependencies copy 到 lib/ext
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>JmeterPlug</groupId>
        <artifactId>aliyuntest</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <!-- 防止打出jar包后,中文参数乱码 -->
        <properties>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <lombok.version>1.16.22</lombok.version>
            <jackson.version>2.8.10</jackson.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.jmeter</groupId>
                <artifactId>ApacheJMeter_java</artifactId>
                <version>4.0</version>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
    
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>4.1.4</version>
            </dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.datatype</groupId>
                <artifactId>jackson-datatype-guava</artifactId>
                <version>${jackson.version}</version>
            </dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.datatype</groupId>
                <artifactId>jackson-datatype-jsr310</artifactId>
                <version>${jackson.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.5.14.RELEASE</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>jmeter-test</finalName>
            <!-- 把以来jar包要打入jar内,不然运行时报类找不到错误 -->
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <!-- 指定运行主类 -->
                                <mainClass>com.demo.Test</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>8</source>
                        <target>8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    1. 实现AbstractJavaSamplerClient
    package support;
    
    import support.util.InvokeAliyunGate ;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.jmeter.config.Arguments;
    import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
    import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
    import org.apache.jmeter.samplers.SampleResult;
    import support.util.Keys;
    
    
    @Slf4j
    public class JmeterSupportNing extends AbstractJavaSamplerClient {
    
    //    // cdp api
    //    private static InvokeAliyunGate cdpApis = new InvokeAliyunGate(Keys.UAT_CDP_AK,Keys.UAT_CDP_SK,Keys.UAT_CDP_URI);
    //    // ulp web
    //    private static InvokeAliyunGate ulpApis = new InvokeAliyunGate(Keys.UAT_ULP_AK,Keys.UAT_ULP_SK,Keys.UAT_ULP_URI);
    //    private static InvokeAliyunGate ulpApis_test = new InvokeAliyunGate(Keys.UAT_ULP_AK_TEST,Keys.UAT_ULP_SK_TEST,Keys.UAT_ULP_URI_TEST);
    
    
        @Override
        public void setupTest(JavaSamplerContext context) {
            //可选,测试前执行,做一些初始化工作;
            log.info("setupTest");
        }
    
        @Override
        public void teardownTest(JavaSamplerContext context) {
            // 可选,测试结束时调用;
    
            System.out.println("teardownTest");
        }
    
        @Override
        public Arguments getDefaultParameters() {
            // 设置可用参数
            Arguments jMeterProperties = new Arguments();
            jMeterProperties.addArgument("cookie", "");
            jMeterProperties.addArgument("method", "");
            jMeterProperties.addArgument("body", "");
            jMeterProperties.addArgument("path", "");
            jMeterProperties.addArgument("gateway_url", "");
            jMeterProperties.addArgument("ak", "");
            jMeterProperties.addArgument("sk", "");
            return jMeterProperties;
        }
    
        @Override
        public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
            //必选,实现自定义请求,请求逻辑;
            SampleResult sampleResult = new SampleResult();
            sampleResult.setSampleLabel("NW Jmeter Java Request"); // 设置请求名称
            try {
                sampleResult.sampleStart(); //开始统计响应时间标记
                String cookie = javaSamplerContext.getParameter("cookie");
                String method = javaSamplerContext.getParameter("method");
                String body = javaSamplerContext.getParameter("body");
                String path = javaSamplerContext.getParameter("path");
                String url = javaSamplerContext.getParameter("gateway_url");
                String ak = javaSamplerContext.getParameter("ak");
                String sk = javaSamplerContext.getParameter("sk");
                InvokeAliyunGate apis = new InvokeAliyunGate(ak, sk ,url);
                apis.invokeGateByMode(body, path, method, cookie);
    
            } finally {
                sampleResult.sampleEnd();// 结束统计响应时间标记
            }
            return sampleResult;
        }
    
        public static void main(String[] args) {
    
            System.out.println("ok!!!");
    
    //        InvokeAliyunGate ulpApis = new InvokeAliyunGate(Keys.UAT_ULP_AK,Keys.UAT_ULP_SK,Keys.UAT_ULP_URI);
    //        String body = "{"captcha":"string","mobile":11111111111,"prefix":86,"token":"string"}";
    //        String path = "/api/login-send-sms-code";
    ////        String body = "{ "receiver":"11111111111", "noticeType":"SMS", "type":1 }";
    ////        String path = "/api/user/web/mobile/reset/send-sms";
    //        String method = "post";
    //        ulpApis.invokeGateByMode(body, path, method);
    
        }
    
    }
    
    
  • 相关阅读:
    二叉树的镜像
    Prototype 模式
    假如编程语言在一起聚餐
    从零开始——Ubuntu系统安装LAMP
    从零开始——Ubuntu系统配置与软件安装
    从零开始——制作Ubuntu系统U盘启动
    apt-get upgrade failure with libc
    再生龙Clonezilla备份Ubuntu系统
    Ubuntu上apache多端口配置虚拟主机的方法
    bash & Makefile & 正则表达式学习
  • 原文地址:https://www.cnblogs.com/blogabc/p/14185633.html
Copyright © 2011-2022 走看看