需求:项目需要从kakfka接收报文,使用jmeter和LR12各做了脚本,先介绍LR的脚本。
注:LR11不支持高版本jdk,如果没有LR12还是用jmeter压测
编写工具LR12,环境jdk1.8
import lrapi.lr;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import ncl.fxjssend; //封装的kakfka代码
public class Actions
{
fxjssend fx ; //封装包里的方法
public int init() throws Throwable {
fx = new fxjssend();
return 0;
}//end of init
public int action() throws Throwable{
String name =getRandStr(5); //封装的随机数姓名26英文字母
int messageNo = 1;
String key = String.valueOf(messageNo);
String lt=String.format("报文:/s", lr.eval_string("{date}"));//报文并传参,
String aa = String.format(lt, key);
lr.start_transaction("l");
fx.produce(aa); //发送报文
lr.end_transaction("l", lr.PASS);
return 0;
}
public int end() throws Throwable {
fx.closed();//关闭
return 0;
}//end of end
封装部分java脚本,具体kakfka函数用法度娘:
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class fxjssend {
private static KafkaProducer<String,String> producer;
public final static String topic = "test";
//private final String topic;
public fxjssend() {
Properties props = new Properties();
props.put("bootstrap.servers", "ip:port,ip:port,ip:port");
props.put("acks", "all");
// props.put("topic", "test");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer",
"org.apache.kafka.common.serialization.IntegerSerializer");
props.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer<String, String>(props);
// this.topic = "test";
}
public void produce(String data) {
try {
producer.send(new ProducerRecord<String, String>(topic, data));
} catch (Exception e) {
e.printStackTrace();
}
// producer.close();
}
public void closed() {
producer.close();
}