1. 程式人生 > >jmeter自定義函數學習筆記

jmeter自定義函數學習筆記

xsd lns -o 方式 -i ins encoding new 源碼學習

源碼學習

https://github.com/apache/jmeter/blob/trunk/src/functions/org/apache/jmeter/functions/CSVRead.java

Java代碼

import org.apache.commons.io.FileUtils;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

import java.io.File;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * @author lw
 * @createTime 2018/12/1 20:42
 * @description jmeter插件開發
 */
public class StringToFile extends AbstractFunction {

    private static final List<String> desc = new LinkedList<String>();
    private static final String KEY = "__StringToFile";
    private Object[] values;

    static {
        desc.add("Content to save");
        desc.add("Target file");
        desc.add("Is append (default:false)");
    }

    @Override
    public String execute(SampleResult previousResult, Sampler currentSampler)  {
        String content = ((CompoundVariable) values[0]).execute().trim();
        String file = ((CompoundVariable) values[1]).execute().trim();
        try {
            boolean append = false;
            if (values.length > 2) {
                String appendStr = ((CompoundVariable) values[2]).execute().trim();
                if ("1".equals(appendStr) || "true".equals(appendStr)) {
                    append = true;
                }
            }
            FileUtils.write(new File(file), content, append);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
        checkMinParameterCount(parameters, 2);
        values = parameters.toArray();
    }

    @Override
    public String getReferenceKey() {
        return KEY;
    }

    @Override
    public List<String> getArgumentDesc() {
        return desc;
    }
}

IDEAmaven文件

<?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>jmeter-plugin</groupId>
    <artifactId>jmeter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_core</artifactId>
            <version>2.13</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>2.13</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

技術分享圖片

結果:

技術分享圖片
使用方式:
Debug Sampler ${__StringToFile(${res},C:\Users\lw\Desktop\eeee\login.txt,1)}
技術分享圖片
技術分享圖片

文件內容:


{status:0,msg:"login success!"}

jmeter自定義函數學習筆記