jmeter使用BeanShell Sampler測試自己寫的java介面(二)
阿新 • • 發佈:2019-01-28
- 上一篇藉助java程式測試SFTP還沒有完成這節繼續
使用BeamShell sampler的步驟和前面一節一樣,java程式碼如下,這裡只測試下載。有些多餘的程式碼沒有清理掉。懶人不想清理先留著了。
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class test11 {
public ChannelSftp connect(String host, int port, String username,
String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession (username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect ();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
} catch (Exception e) {
}
return sftp;
}
public void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file=new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
public int test(){
TestSftp sf = new TestSftp();
String host = "192.168.1.107";
int port = 22;
String username = "root";
String password = "wxf";
String directory = "/root/Desktop";
String uploadFile = "D:\\tmp\\upload.txt";
String downloadFile = "threadServer.py";
String saveFile = "D:\\threadServer.py";
String deleteFile = "delete.txt";
ChannelSftp sftp=sf.connect(host, port, username, password);
// sf.upload(directory, uploadFile, sftp);
sf.download(directory, downloadFile, saveFile, sftp);
// sf.delete(directory, deleteFile, sftp);
try{
sftp.cd(directory);
sftp.mkdir("ss");
System.out.println("finished");
}catch(Exception e){
e.printStackTrace();
}
return 2;
}
}
在jmeter中的BeamShell Sampler程式碼如下:
source("F:\\javatcp\\test\\src\\test\\test11.java");
test11 ts = new test11();
int res = ts.test();
vars.put("a",res.toString());
執行結果:
成功的下載檔案到D盤指定目錄
不知道為什麼下載下來的檔案大小為0Kb,這個問題留到以後分析程式碼解決,這裡只研究jmeter beanshell和java測試SFTP下載
jmeter執行結果:
後面就可以實行SFTP的併發測試了
後續:
由於是使用的自己寫的java程式,本身執行效率可能會影響到測試的結果,對服務端的影響比較大。後面文章將會展示是用javaSampler進行測試。比較兩者的區別。
後面還會補上匯出jar包和直接載入class檔案的測試方法。先記錄一下