JMeter Code for Screen Share

生成随机长度的字符串

import java.util.Random; 

String random(int s_length) { 
      strings= "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; 
      //int s_length = 10; 
      rstring ="";
      for (int i=0; i

保存数据到csv文件

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import org.json.JSONObject;
import org.json.JSONArray;

String response=prev.getResponseDataAsString();//取出接口的返回消息
JSONObject jsonstring=new JSONObject(response);//放进json对象里

String turn_id=jsonstring.getString("turnid");
String tls_addr=jsonstring.getJSONArray("addrs").getJSONObject(0).getString("tls_addr").split(":")[0];
String tls_port=jsonstring.getJSONArray("addrs").getJSONObject(0).getString("tls_addr").split(":")[1];
String tcp_addr=jsonstring.getJSONArray("signal_addrs").getJSONObject(0).getString("addr").split(":")[0];
String tcp_port=jsonstring.getJSONArray("signal_addrs").getJSONObject(0).getString("addr").split(":")[1];



String outputFile = "E:/Software/JMeter/roomsInfo.csv";
// before we open the file check to see if it already exists
boolean alreadyExists = new File(outputFile).exists();
    
try {
    // use FileWriter constructor that specifies open for appending
    CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');
    
    // if the file didn't already exist then we need to write out the header line
    if (!alreadyExists)
    {
        csvOutput.write("turnId");
        csvOutput.write("tlsIp");
        csvOutput.write("tlsPort");
        csvOutput.write("tcpIp");
        csvOutput.write("tcpPort");
        csvOutput.endRecord();
    }
    // else assume that the file already has the correct header line
    
    // write out a few records
    csvOutput.write(turn_id);
    csvOutput.write(tls_addr);
    csvOutput.write(tls_port);
    csvOutput.write(tcp_addr);
    csvOutput.write(tcp_port);
    csvOutput.endRecord();
    
    csvOutput.close();
} catch (IOException e) {
    e.printStackTrace();
}

vars.put("turnId",turn_id);
vars.put("tlsIp",tls_addr);
vars.put("tlsPort",tls_port);
vars.put("tcpIp",tcp_addr);
vars.put("tcpPort",tcp_port);

你可能感兴趣的:(性能测试,测试工具,jmeter,java,开发语言)