代码改变世界

jmeter自定义函数开发—Web3钱包生成工具

2024-07-09 19:11  第二个卿老师  阅读(14)  评论(0编辑  收藏  举报

之前使用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>
View Code

实现代码,可以自行调整相关参数,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;
            }
    }
}
View Code

插件准备

使用maven打包后,把jar包复制到apache-jmeter-5.4.3安装目录下\lib\ext

Jmeter使用

jmeter重启后,应该在取样器中能看见函数助手中找到该选项

 使用效果如下,后面有时间再做一个私钥签名的函数。