Jmeter ExcelDataPreProcessor

Jmeter的预处理器主要是用来在采样器(sample)被执行之前做一些额外的工作,比如参数化等等。

本文写一个例子来说明如何增加一个预处理器,需求如下:我们想在执行采样器前读取Excel文件中的数据作为参数,此预处理器可以配合While循环控制器使用,每次循环读取excel中的一行数据。

@GUIMenuSortOrder(5)
public class ExcelDataPreProcessor extends AbstractTestElement implements
        Cloneable, PreProcessor, TestBean {
    private static final Logger log = LoggerFactory
            .getLogger(ExcelDataPreProcessor.class);
 
    private String filename = ""; // file to source (overrides script)
    
    private static final long serialVersionUID = 233L;
 
    @Override
    public void process() {
        //做你想做的一些逻辑
        System.out.println(this.getFilename());
    }
 
    public String getFilename() {
        return filename;
    }
 
    public void setFilename(String filename) {
        this.filename = filename;
    }
 
    @Override
    public Object clone() {
        return super.clone();
    }
}

2、写一个对应的Bean

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 */
 
package org.apache.jmeter.modifiers;
 
import java.beans.PropertyDescriptor;
import java.util.ResourceBundle;
 
import org.apache.jmeter.testbeans.BeanInfoSupport;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.jmeter.testbeans.gui.FileEditor;
 
public class ExcelDataPreProcessorBeanInfo extends BeanInfoSupport {
 
    public ExcelDataPreProcessorBeanInfo() {
        this(ExcelDataPreProcessor.class,null);
    }
    
 
    public ExcelDataPreProcessorBeanInfo(Class<? extends TestBean> beanClass, String[] languageTags) {
        this(beanClass, languageTags, null);
    }
 
    protected ExcelDataPreProcessorBeanInfo(Class<? extends TestBean> beanClass, String[] languageTags, ResourceBundle rb) {
        super(beanClass);
        PropertyDescriptor p;
 
        p = property("filename"); // $NON-NLS-1$
        p.setValue(NOT_UNDEFINED, Boolean.TRUE);
        p.setValue(DEFAULT, ""); // $NON-NLS-1$
        p.setPropertyEditorClass(FileEditor.class);
 
        createPropertyGroup("filenameGroup",  // $NON-NLS-1$
                new String[] { "filename" }); // $NON-NLS-1$
 
    }
 
}

这个是描述界面布局和相关元素的的

3,在saveservice.properties文件中增加配置:

ExcelDataPreProcessor=org.apache.jmeter.modifiers.ExcelDataPreProcessor
4、增加一个(ExcelDataPreProcessorResources.properties)多语言支持:

猜你喜欢

转载自www.cnblogs.com/a00ium/p/10381192.html