之前使用Jmeter進行介面測試時,有生成錢包地址的需求,於是有時間就簡單寫了個自定義函式
環境說明
JDK1.8.0,Jmeter 5.4.3,maven構建工具
實現程式碼
新建一個簡單的maven專案即可,以下是pom.xml配置
<?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>com.qgc</groupId> <artifactId>walletTools</artifactId> <version>1.0-SNAPSHOT</version> <name>walletTools</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_functions</artifactId> <version>5.4.3</version> </dependency> <!-- web3J依賴 --> <dependency> <groupId>org.web3j</groupId> <artifactId>core</artifactId> <version>5.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-ext-jdk15on --> <!-- <dependency>--> <!-- <groupId>org.bouncycastle</groupId>--> <!-- <artifactId>bcprov-ext-jdk15on</artifactId>--> <!-- <version>1.70</version>--> <!-- </dependency>--> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.70</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
實現程式碼,可以自行調整相關引數,type實現了3種返回方式,其中1 隨機返回私鑰,2 隨機返回地址,3 返回指定私鑰的地址
package com.qgc.functions; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.engine.util.CompoundVariable; import org.apache.jmeter.functions.InvalidVariableException; import org.apache.jmeter.samplers.Sampler; import org.web3j.crypto.*; import org.apache.jmeter.functions.AbstractFunction; public class App extends AbstractFunction { //定義一個obect物件去接受傳入變數值 private Object[] values; //儲存第一個和第二個引數 private CompoundVariable type, privateKey; @Override /** * 執行方法 * @param sampleResult * @param sampler * @return * @throws InvalidVariableException */ public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException { //接住第一個引數值 type = (CompoundVariable) values[0]; privateKey = (CompoundVariable) values[1]; if (type.execute().equals("3")){ try { String address = getKeyAddress(privateKey.execute()); return address; } catch (Exception e){ e.printStackTrace(); } } else { try { String str = getRandomAddress(type.execute()); return str; } catch (Exception e){ e.printStackTrace(); } } return null; } @Override /** * 設定引數,接受使用者傳遞的引數 * @param collection * @throws InvalidVariableException */ public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException { //檢查引數是否合法 checkParameterCount(collection,2); //轉換成陣列 values = collection.toArray(); } @Override /** * 函式名稱 * @return */ public String getReferenceKey() { return "__qgcWalletApp"; } @Override /** * 函式描述,獲取引數描述 * @return */ public List<String> getArgumentDesc() { List desc = new ArrayList(); //介面上顯示兩行引數描述 desc.add("type(1隨機返回私鑰,2隨機返回地址,3返回指定私鑰的地址)"); desc.add("privateKey(type為3的時候傳)"); return desc; } public static String getKeyAddress(String privateKey) throws Exception {// 透過私鑰生成公鑰和地址 if (privateKey !=null || privateKey.equals("")){ String address = null; try { ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey, 16)); address = "0x" + Keys.getAddress(keyPair.getPublicKey()); } catch (Exception e){ throw new InvalidVariableException(e); } finally { return address; } } else { System.out.println("privateKey不合法!"); return null; } } public static String getRandomAddress(String type) throws Exception { // 生成隨機的ECKeyPair ECKeyPair ecKeyPair = Keys.createEcKeyPair(); // 根據ECKeyPair生成Credentials物件 Credentials credentials = Credentials.create(ecKeyPair); // 輸出生成的私鑰和地址 if (type.equals("1")){ String privateKey = credentials.getEcKeyPair().getPrivateKey().toString(16); return privateKey; } else if (type.equals("2")){ String address = credentials.getAddress(); return address; } else { return null; } } }
外掛準備
使用maven打包後,把jar包複製到apache-jmeter-5.4.3安裝目錄下\lib\ext
Jmeter使用
jmeter重啟後,應該在取樣器中能看見函式助手中找到該選項
使用效果如下,後面有時間再做一個私鑰簽名的函式。