首页 > 解决方案 > 如何在 Jmeter 中使用 Maven 配置文件

问题描述

我有不同的环境,比如 prod、local、dev 和 QA 来进行性能测试。根据环境,我想更改线程、循环计数和域名值。通过使用 Jmeter 属性,我可以更改如下值

mvn process-resources jmeter:jmeter jmeter-analysis:analyze -Dthreads=30 -DloopCount=30 -DdomainName=mydomainname

但我想通过使用maven profiles来改变这个值。你能帮帮我吗?我花了很多时间,但我不知道如何在 Jmeter 中使用配置文件。

标签: javamavenjmeter

解决方案


我的期望是在这里看到您的pom.xml文件,以便我们可以指出错误。这也将是您尝试解决问题而不是要求社区为您完成工作的证据。

根据构建配置文件简介文档一章,添加以下内容就足够了:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <threads>50</threads>
            <loopCount>50</loopCount>
            <domainName>devDomain</domainName>
        </properties>
    </profile>
    <profile>
        <id>qa</id>
        <properties>
            <threads>1000</threads>
            <loopCount>1000</loopCount>
            <domainName>qaDomain</domainName>
        </properties>
    </profile>
</profiles>

为了相应地定义devqa环境的属性,因此当您通过 Maven 运行 JMeter 时,例如:

mvn -P dev verify

您将获得 50 个用户、50 个循环等。

如果你qa像这样运行配置文件:

mvn -P qa verify

您将获得 1000 个用户、1000 个循环等。

完整的 pom.xml 以防万一:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>com.example</groupId>
    <artifactId>jmeter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <threads>100</threads>
        <loopCount>100</loopCount>
        <domainName>defaultDomain</domainName>
    </properties>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <threads>50</threads>
                <loopCount>50</loopCount>
                <domainName>devDomain</domainName>
            </properties>
        </profile>
        <profile>
            <id>qa</id>
            <properties>
                <threads>1000</threads>
                <loopCount>1000</loopCount>
                <domainName>qaDomain</domainName>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <!-- Generate JMeter configuration -->
                    <execution>
                        <id>configuration</id>
                        <goals>
                            <goal>configure</goal>
                        </goals>
                    </execution>
                    <!-- Run JMeter tests -->
                    <execution>
                        <id>jmeter-tests</id>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>

                    </execution>
                    <!-- Fail build on errors in test -->
                    <execution>
                        <id>jmeter-check-results</id>
                        <goals>
                            <goal>results</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <propertiesUser>
                        <threads>${threads}</threads>
                        <loopCount>${loopCount}</loopCount>
                        <domainName>${domainName}</domainName>
                    </propertiesUser>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

更多信息:如何使用 JMeter Maven 插件


推荐阅读