简体   繁体   English

使用 JMeter 使用 RabbitMQ 应用程序负载测试进行后端 Spring 启动

[英]Backend Spring boot with RabbitMQ application load testing using JMeter

I have Spring boot with RabbitMQ back-end application.我有带有 RabbitMQ 后端应用程序的 Spring Boot。 I want to run my JUnit test case with JMeter for load testing.我想用 JMeter 运行我的 JUnit 测试用例进行负载测试。 While I run Junit test case , I face below error.当我运行 Junit 测试用例时,我面临以下错误。 OmniRoute class available from other jar file. OmniRoute 类可从其他 jar 文件获得。 When I run Junit test case from my IDE, then it is running successfully but when I run it from JMeter then I am facing below error.当我从 IDE 运行 Junit 测试用例时,它运行成功,但是当我从 JMeter 运行它时,我面临以下错误。 I have shared my code as well.我也分享了我的代码。

Error -- testShrotUrlGenerate(org.apache.jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase): mb/omni/proto/OmniRoute Trace -- java.lang.NoClassDefFoundError: mb/omni/proto/OmniRoute at mb.omni.shorturl.handlers.ShortUrlGenerationHandlerTest.testShrotUrlGenerate(ShortUrlGenerationHandlerTest.java:79) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase.runTest(JUnitSampler.java:596) at junit.framework.TestResult.runProtected(TestResult.java:142) at org.apache.jmeter.protocol.java.sampler.JUnitSampler.sample(JUnitSampler.java:396) at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) at org.apache.jmeter.threads错误 -- testShrotUrlGenerate(org.apache.jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase): mb/omni/proto/OmniRoute Trace -- java.lang.NoClassDefFoundError: mb/omni/proto/OmniRoute at mb.omni。 shorturl.handlers.ShortUrlGenerationHandlerTest.testShrotUrlGenerate(ShortUrlGenerationHandlerTest.java:79) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflectorlGenerationHandlerTest.java:79)在 java.lang.reflect.Method.invoke(Unknown Source) at org.apache.jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase.runTest(JUnitSampler.java:596) at junit.framework.TestResult.runProtected(TestResult. java:142) 在 org.apache.jmeter.protocol.java.sampler.JUnitSampler.sample(JUnitSampler.java:396) 在 org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) 在 org.apache .jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) 在 org.apache.jmeter.threads .JMeterThread.processSampler(JMeterThread.java:490) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) at java.lang.Thread.run(Unknown Source) Error -- testShrotUrlGenerate(org.apache.jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase): org/springframework/amqp/rabbit/core/RabbitTemplate Trace -- java.lang.NoClassDefFoundError: org/springframework/amqp/rabbit/core/RabbitTemplate at mb.omni.shorturl.handlers.ShortUrlGenerationHandlerTest.cleanup(ShortUrlGenerationHandlerTest.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.jmeter.protocol.java.sampler.JUnitSampler.sample(JUnitSampler.java:400) at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.jav .JMeterThread.processSampler(JMeterThread.java:490) 在 org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) 在 java.lang.Thread.run(Unknown Source) 错误 -- testShrotUrlGenerate(org.apache) .jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase): org/springframework/amqp/rabbit/core/RabbitTemplate Trace -- java.lang.NoClassDefFoundError: org/springframework/amqp/rabbit/core/RabbitTemplate at mb.omni。 shorturl.handlers.ShortUrlGenerationHandlerTest.cleanup(ShortUrlGenerationHandlerTest.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.voke0(Native Method)在 org.apache.jmeter.protocol.java.sampler.JUnitSampler.sample(JUnitSampler.java:400) 在 org.apache.jmeter.threads.JMeterThread.doSampling( JMeterThread.java:627) 在 org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.jav a:551) at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.ClassNotFoundException: org.springframework.amqp.rabbit.core.RabbitTemplate at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 11 more a:551) 在 org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) 在 org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) 在 java.lang.Thread.run (Unknown Source) 引起:java.lang.ClassNotFoundException: org.springframework.amqp.rabbit.core.RabbitTemplate at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java .lang.ClassLoader.loadClass(Unknown Source) ... 11 more

I am generating jar file of project with below gradle file.我正在使用以下 gradle 文件生成项目的 jar 文件。

jar {
    enabled=true
    manifest {
        attributes "Main-Class": "MainClassName"
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

Below is my configuration class with RabbitMQ.下面是我使用 RabbitMQ 的配置类。

@Configuration
@EnableIntegration
@IntegrationComponentScan
public class ShortUrlGenerateConfig {

    @Bean
    public DirectExchange shortUrlExchange() {
        return new DirectExchange("exchange", true, false);
    }

    @Bean
    public Queue shortUrlGenerateQueue() {
        return new Queue("queue");
    }

    @Bean
    public Binding shortUrlGenerateBinding() {
        return BindingBuilder.bind(shortUrlGenerateQueue()).to(shortUrlExchange()).with("routingKey");
    }

    @Bean
    public ShortUrlGenerationHandler shortUrlGenerationHandler() {
        return new ShortUrlGenerationHandler();
    }
}

This is my handler class.这是我的处理程序类。

public class ShortUrlGenerationHandler {

    private static final Logger logger = LoggerFactory.getLogger(ShortUrlGenerationHandler.class);


    @Autowired
    private RabbitTemplate rabTmp;


    /**
     * @param message
     * generate shorturl
     */
    @RabbitListener(queues = "queue")
    public void consumeMessageFromGenerateQueue(final Message<?> message) {

         String routingKey = (String) message.getHeaders().get(AmqpHeaders.RECEIVED_ROUTING_KEY);
         logger.info("ShortURL Message received on Queue["+message.getHeaders().get(AmqpHeaders.CONSUMER_QUEUE)+"], Routing Key["+routingKey+"].");
         Object correlationId = message.getHeaders().get(AmqpHeaders.CORRELATION_ID);

         Object directReplyTo = message.getHeaders().get(AmqpHeaders.REPLY_TO);
         ShortURLRequest request = null;
         ShortURLRequest.Builder response = null;

            try {
                request = ShortURLRequest.parseFrom((byte[]) message.getPayload());
                response = ShortURLRequest.newBuilder();
                if (correlationId == null || directReplyTo == null) {
                    throw new ResourceNotFoundException("CORRELATION_ID_NOT_FOUND","Invalid Correlation ID, Correlation ID not found",9103);
                }

                .
                .
                .

            }catch (Exception e) {
                logger.error(" Error in consumeMessageFromGenerateQueue method.Error  is - " + e.getMessage(), e);

            }
        logger.info(" response - " + response);
        MessageProperties props = MessagePropertiesBuilder.newInstance().setCorrelationId(correlationId.toString())
                .build();
        rabTmp.send("", directReplyTo.toString(),
                MessageBuilder.withBody(response.build().toByteArray()).andProperties(props).build());
    }
}

Below is my Junit test case.下面是我的 Junit 测试用例。

@Test
    public void testShrotUrlGenerate() throws Exception {

        OmniRoute omniRoute = OmniRoute.newBuilder()
                .setRouteType("AMQP")
                .setProcessor("processor")
                .setAmqpRoute(OmniAmqpRoute.newBuilder()
                        .setRoutingkey("routingKey")
                        .setExchange("exchange")
                        .build())
                .build();

        List<OmniRoute> omniRoutes = new ArrayList<>();
        omniRoutes.add(omniRoute);

        OmniRequest omniRequest = OmniRequest.newBuilder()
                .setRequestId("182fd990725011e9a9ca8f4121c06f8b12")
                .setRequestType("SHORT_URL")
                .addAllRoutes(omniRoutes)
                .setUserId(100)
                .build();

        ShortURL shortUrl = ShortURL.newBuilder().setOriginalUrl("http://dashboard.omni.platform/#/login").setUser(User.newBuilder()
                .setUserId(100))
                .setMessageId("3")
                .setChannel("CHANNEL_ONE")
                .build();

        ShortURLRequest shortURLRequest = ShortURLRequest.newBuilder().setRequest(omniRequest).addShortUrls(shortUrl).build();

          MessageProperties messageProperties = new MessageProperties();
          messageProperties.setCorrelationId("1f4a13d0-f3b4-4b4b-98b4-50e9d75d6032");
          Message message = new Message(shortURLRequest.toByteArray(), messageProperties);
          byte[] responseByte = (byte[])  template.convertSendAndReceive(shortUrlExchange, shortUrlGenerateRoutingKey, message);
          ShortURLRequest shortURLResponse = ShortURLRequest.parseFrom(responseByte);
          assertTrue(shortURLResponse.getShortUrlsList().get(0).getShortUrlId()>0);
          assertEquals(shortURLResponse.getShortUrlsList().get(0).getOriginalUrl(),shortUrl.getOriginalUrl());
          assertTrue(shortURLResponse.getShortUrlsList().get(0).getShortUrlCode()!= null);
          assertEquals(shortURLResponse.getShortUrlsList().get(0).getUser().getUserId(),shortUrl.getUser().getUserId());
    }

NoClassDefFoundError: mb/omni/proto/OmniRoute NoClassDefFoundError: mb/omni/proto/OmniRoute

means that you don't have the .jar which contains this class inJMeter Classpath意味着您在JMeter 类路径中没有包含此类的 .jar

You can do something like:您可以执行以下操作:

task copyRuntimeLibs(type: Copy) {
    into "dependencies"
    from configurations.testRuntime - configurations.runtime
}

in your Gradle build script and when you invoke the task - all the .jar files which are required by your project will be copied to dependencies folder of your project.在您的Gradle 构建脚本中以及当您调用任务时 - 您的项目所需的所有 .jar 文件都将被复制到您的项目的dependencies文件夹中。 Once you have them - just "tell" JMeter to look for the .jars in that folder via search_paths property or just copy them to "lib" folder of your JMeter installation.一旦你有了它们 - 只需“告诉”JMeter 通过search_paths属性在该文件夹中查找 .jars 或者只是将它们复制到 JMeter 安装的“lib”文件夹中。

More information: How to Use JUnit With JMeter更多信息:如何在 JMeter 中使用 JUnit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM