美文网首页
JMeter 5.0(2):编写Java Request

JMeter 5.0(2):编写Java Request

作者: halfempty | 来源:发表于2018-12-13 20:45 被阅读0次

    1 前言

    现有的sample已基本能满足日常使用,但如果有些特殊要求,比如远程连接服务器,或者操作新型NoSQL库,就得专门定制

    Java Request采样器要求实现org.apache.jmeter.protocol.java.sampler.JavaSamplerClient接口,或者继承AbstractJavaSamplerClient抽象类

    系统自带JavaTest、SleepTest 2个Java Request采样器

    2 JavaSamplerClient

    每个线程,JMeter都会创建一个JavaSamplerClient的实例

    JavaSamplerClient运行过程:

    • 先执行setupTest(),完成初始化
    • 然后是runTest(),每次迭代都会执行
    • 最后是teardownTest(),可能存在的资源释放

    接口方法如下:

    • getDefaultParameters : Arguments,在GUI模式下显示参数及默认值
    • runTest(JavaSampleContext ctx) : SampleResult
    • setupTest(JavaSampleContext ctx) : void
    • teardownTest(JavaSampleContext ctx) : void

    3 SampleResult

    SampleResult携带一系列属性,可以提供给pre/post processor,或者交由Assertion校验

    SampleResult(boolean nanoTime, long nanoThreadSleep) {
        this.responseData = EMPTY_BA;
        this.responseCode = "";
        this.label = "";
        this.resultFileName = "";
        this.threadName = "";
        this.responseMessage = "";
        this.responseHeaders = "";
        this.requestHeaders = "";
        this.timeStamp = 0L;
        this.startTime = 0L;
        this.endTime = 0L;
        this.idleTime = 0L;
        this.pauseTime = 0L;
        this.dataType = "";
        this.files = new HashSet(3);
        this.contentType = "";
        this.elapsedTime = 0L;
        this.latency = 0L;
        this.connectTime = 0L;
        this.testLogicalAction = TestLogicalAction.CONTINUE;
        this.stopThread = false;
        this.stopTest = false;
        this.stopTestNow = false;
        this.sampleCount = 1;
        this.bytes = 0L;
        this.headersSize = 0;
        this.bodySize = 0L;
        this.groupThreads = 0;
        this.allThreads = 0;
        this.elapsedTime = 0L;
        this.useNanoTime = nanoTime;
        this.nanoThreadSleep = nanoThreadSleep;
        this.nanoTimeOffset = this.initOffset();
    }
    

    4 实例:图片下载

    public class DownloadPicSample extends AbstractJavaSamplerClient {
        private DataInputStream dataInputStream;
        private String localFoldStr;
    
        @Override
        public Arguments getDefaultParameters() {
            Arguments arguments = new Arguments();
            arguments.addArgument("PicLink", "");
            arguments.addArgument("Local_Directory", "");
            return arguments;
        }
    
        @Override
        public void setupTest(JavaSamplerContext context) {
            String picLink = context.getParameter("PicLink", "").trim();
            try {
                URL url = new URL(picLink);
                dataInputStream = new DataInputStream(url.openStream());
            } catch (java.io.IOException e) {
                dataInputStream = null;
            }
    
            localFoldStr = context.getParameter("Local_Directory").trim();
            File localFold = new File(localFoldStr);
            if (! localFold.exists() || ! localFold.isDirectory()) {
                localFoldStr = null;
            }
        }
    
        @Override
        public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
            SampleResult sampleResult = new SampleResult();
            if(dataInputStream == null || localFoldStr == null) {
                sampleResult.setResponseData("参数配置错误", "UTF-8");
                return sampleResult;
            }
    
            if(localFoldStr.endsWith(File.separator))
                localFoldStr.substring(0, localFoldStr.length()-1);
            String localPicFile = localFoldStr + File.separator + System.currentTimeMillis() + ".jpg";
    
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(new File(localPicFile), false);
    
                byte[] buffer = new byte[1024];
                int length;
                while((length = dataInputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, length);
                }
                sampleResult.setResponseOK();
                sampleResult.setResponseData(localPicFile, "UTF-8");
            } catch (java.io.IOException e) {
                sampleResult.setResponseData("IO读写失败", "UTF-8");
            } finally {
                try {
                    outputStream.close();
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sampleResult;
        }
    }
    

    相关文章

      网友评论

          本文标题:JMeter 5.0(2):编写Java Request

          本文链接:https://www.haomeiwen.com/subject/viqihqtx.html