-
上次直接使用jmeter裡面的FTPsampler沒有連接配接成功
現在想着自己寫java代碼,通過jmeter進行調用進行連接配接測試實作并發
代碼引文:
http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888384.html
/*
* Created on 2009-9-14
* Copyright 2009 by www.xfok.net. All Rights Reserved
*
*/
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
/**
* @author YangHua
* 轉載請注明出處:http://www.xfok.net/2009/10/124485.html
*/
public class TestSftp {
/**
* 連接配接sftp伺服器
* @param host 主機
* @param port 端口
* @param username 使用者名
* @param password 密碼
* @return
*/
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;
}
/**
* 上傳檔案
* @param directory 上傳的目錄
* @param uploadFile 要上傳的檔案
* @param sftp
*/
public void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file=new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下載下傳檔案
* @param directory 下載下傳目錄
* @param downloadFile 下載下傳的檔案
* @param saveFile 存在本地的路徑
* @param 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();
}
}
/**
* 删除檔案
* @param directory 要删除檔案所在目錄
* @param deleteFile 要删除的檔案
* @param sftp
*/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 列出目錄下的檔案
* @param directory 要列出的目錄
* @param sftp
* @return
* @throws SftpException
*/
public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException{
return sftp.ls(directory);
}
public static void main(String[] args) {
TestSftp sf = new TestSftp();
String host = "192.168.0.1";
int port = ;
String username = "root";
String password = "root";
String directory = "/";
String uploadFile = "D:\\tmp\\upload.txt";
String downloadFile = "upload.txt";
String saveFile = "D:\\tmp\\download.txt";
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();
}
}
}
- 寫測試代碼,調試下載下傳
package test;
import com.jcraft.jsch.ChannelSftp;
public class TestSftpMain {
public static void main(String[] args){
TestSftp sf = new TestSftp();
String host = "192.168.1.107";
int port = ;
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();
}
}
}
代碼是将伺服器的Desktop下面的一個py檔案下載下傳到本地的D盤
運作結果:下載下傳成功
代碼沒有問題。使用上面的方法還需要下載下傳第三方jar包位址:
下載下傳位址:CSDN有下載下傳
-
在jmeter中實作調用
搞了半天,調用上面的代碼失敗了,總是提示Typed variable declaration : Object constructorshi
問題定位過程:
1 經過不斷嘗試,定位問題,以為BeanShell Sampler不允許執行個體化對象,隻能使用匿名對象。
2 實際是因為有了類型變量聲明:
下面聲明了一個ChannelSftp的類型變量。也就是說代碼中隻能聲明基礎變量
3 實際是沒有加載建立類型變量的jar包,在eclipse裡面通過添加外部jar包加進去了是以是成功的。到了jmeter就會出現編譯都失敗。隻是這個問題提示不好定位。
錯誤如下
Response message: org.apache.jorphan.util.JMeterException:
Error invoking bsh method: eval Sourced file: inline
evaluation of:
`source("F:\\javatcp\\test\\src\\test\\test11.java");
test11 ts = new test11(); . . . '' :
Typed variable declaration : Object constructor
public ChannelSftp connect(String host, int port, String username,
String password) {
ChannelSftp sftp = null;
return sftp;
}
先附上簡單的調用接口方式,再解決前面的問題
beanShell測試自己的java接口:
步驟:
1 編寫代碼:
public class Test {
public int ad(){
return ;
}
}
2 beanshell腳本
source是直接加載java,也可以加載class檔案更快
vars.put(“ad”,res.toString()); 給變量ad指派
source("F:\\javatcp\\BSFjmeter\\src\\TestAdd.java");
int res = new Test().ad(,);
vars.put("ad",res.toString());
Debug Sampler樣本的結果
JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@c5adae
START.HMS=
START.MS=
START.YMD=
TESTSTART.MS=
ddd=
這裡實作了簡單的調用
BeanShell Sampler測試還有很多強大的用法,後面測試中繼續加強練習
搞了好久,前面提示了不能聲明類型變量,是因為沒有引入對應的jar包。現在引入jar包
jmter引入第三方jar包除了直接放到lib/ext下,還有一種方式就是如圖:
然後再測試就可以通過了