1. 程式人生 > 其它 >JAVA構造效能測試資料

JAVA構造效能測試資料

一、idea配置pom.xml檔案

1. 開啟idea編輯器,新建一個Maven專案,File-->New-->Project-->Maven,選擇Maven,點選next
2. 在Name編輯框中輸入專案的名稱,這裡是dataProject,點選finish
3. 當專案建成以後,需要配置pom.xml檔案,配置方法:
  • 百度搜索:https://mvnrepository.com/,進入到maven的倉庫頁面
  • 搜尋我們需要的包
  • 選擇javafaker的版本,並複製對應的版本的依賴到idea中的pom.xml檔案
  • 將複製的依賴程式碼塊複製到pom.xml檔案,注意需要在外層加上<dependencies>依賴的程式碼塊</dependencies>
<?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>org.example</groupId>
    <artifactId>dataProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

        <!--    構造資料的庫-->
    <dependencies>
        <dependency>
            <groupId>com.github.javafaker</groupId>
            <artifactId>javafaker</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>
    
    <!-- 使用maven-assembly-plugin外掛打包--》
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
  • 點選右側maven中的install下載依賴
4.在idea中的src-->main-->java中新建一個構造資料的類,匯入類,構造測試資料
package com.data;
import com.github.javafaker.Faker;

import java.util.Locale;

public class initData {
    static Faker faker = Faker.instance(Locale.CHINA);

    public static void main(String[] args) {
        System.out.println(initData.telephone());
        System.out.println(initData.address());

    }
    //電話號碼
    public static String telephone() {
        String phone = faker.phoneNumber().cellPhone();
        return phone;
    }
    //地址
    public static String address() {
        return faker.address().streetAddress();
    }
}

5.選擇maven中的package打包成jar包檔案,打包完成後在target目錄下會生成jar包,將jar包拷貝到jmeter中的lib目錄下的ext目錄下面
6. 重啟jmeter,在jmeter中的前置處理器或者後置處理器中寫上如下程式碼
import com.data.initData;
vars.put("telephone",initData.telephone());

加上除錯取樣器和察看結果數,傳送請求可以看到獲取到了電話號碼