You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by Rob Schroder <rs...@jadefox.com> on 2001/07/28 07:45:42 UTC

FTPClient.java performance enhancement

I've changed FTPClient to use a BufferedInputStream, it now reads at a 
more realistic speed.  The altered file is pasted at the bottom of this 
message.

The changes were made in the run() method, in particular....

try {
		  InputStream in = s.getInputStream();
                  BufferedInputStream dataIn = new BufferedInputStream
(in);
                  int bufferSize = 4096;
                  byte[] inputBuffer = new byte[bufferSize];
		  int i = 0;
		//System.out.println("About to read data.");  
                while ((i = dataIn.read(inputBuffer, 0, bufferSize)) !
= -1) {
                    //buffer.append((char) i);
		  }




----------Changed File-----------------

/*
 * ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 * if any, must include the following acknowledgment:
 * "This product includes software developed by the
 * Apache Software Foundation (http://www.apache.org/)."
 * Alternately, this acknowledgment may appear in the software itself,
 * if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 * "Apache JMeter" must not be used to endorse or promote products
 * derived from this software without prior written permission. For
 * written permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 * "Apache JMeter", nor may "Apache" appear in their name, without
 * prior written permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
 package org.apache.jmeter.protocol.ftp.sampler;

import java.io.*;
import java.net.*;

/*
 *  Simple FTP client (non-passive transfers don't work yet)
 *  kind of a hack, lots of room for optimizations
 */

public class FtpClient {
  static int port = 21;
  static int dataPort = 4096;
  File f = new File("e:\\");
  OutputStream out;
  BufferedReader in;
  Socket s;
  boolean passive = false;
  public FtpClient() {
  }
  /** connect to server */
  public void connect(String host, String username, String password) 
throws Exception {
	 InetAddress addr = InetAddress.getByName(host);
	 s = new Socket(addr, port);
	 out = s.getOutputStream();
	 InputStreamReader isr = new InputStreamReader(s.getInputStream
());
	 in = new BufferedReader(isr);
	 System.out.println(in.readLine());
	 send("USER " + username);
	 System.out.println(getResponse());
	 send("PASS " + password);
	 System.out.println(getResponse());
	 //System.out.println("Done connecting");
  }
  /** set passive mode */
  public void setPassive(boolean flag) {
	 passive = flag;
  }
  /** get a file from the server */
  public String getResponse() throws IOException {
	 StringBuffer response = new StringBuffer();
	 String line = in.readLine();
	 response.append(line);
	 //System.out.println("#" + line + "#");
	 while (line.charAt(3) == '-') {
		line = in.readLine();
		response.append("\n");
		response.append(line);
		System.out.println("#" + line + "#");
	 }
	 //System.out.println("return response");
	 return response.toString();
  }
  /** get a file from the server */
  public String get(String file) throws Exception {
	 send("SYST");
	 getResponse();
	 send("PWD");
	 getResponse();
	 send("TYPE I");
	 getResponse();
	 String data = "";
	 if (!passive) {
		dataPort++;
		int upper = getUpper(dataPort);
		int lower = getLower(dataPort);
		String ip = InetAddress.getLocalHost().getHostAddress
().replace('.',',');
		String port = ip + "," + upper + "," + lower;
		//System.out.println("port:" + port);
		send("PORT " + port);
		getResponse();
		dataGrabber grab = new dataGrabber(ip, dataPort);
		while (!grab.isPortCreated()) {
		}
		send("RETR " + file);
		String response = in.readLine();
		//System.out.println(response);
		//System.out.println(dataPort);
		data = "FTP client - File Not Found";
		if (!response.startsWith("5")) {
		  while (!grab.isDone()) {
		  }
		  data = grab.getData();
		}
	 } else {
		send("PASV");
		String port = getResponse();
		while (!port.startsWith("227")) {
			 port = getResponse();
		}
		int start = port.indexOf('(');
		int end = port.indexOf(')');
		port = port.substring(start + 1, end);
		int a = port.indexOf(',');
		int b = port.indexOf(',', a + 1);
		int c = port.indexOf(',', b + 1);
		int d = port.indexOf(',', c + 1);
		int e = port.indexOf(',', d + 1);
		String ip = port.substring(0, a) + "." + port.substring
(a + 1, b) + "." + port.substring(b + 1, c) + "." + port.substring(c + 
1, d);
		int upper = Integer.parseInt(port.substring(d + 1, e));
		int lower = Integer.parseInt(port.substring(e + 1));
		int dataPort = getPort(upper,lower);
		send("RETR " + file);
		dataGrabber grab = new dataGrabber(ip, dataPort);
		getResponse();
		while (!grab.isDone()) {
		}
		data = grab.getData();
	 }
	 return data;
  }
  public static int getPort(int upper, int lower) {
	 return upper * 256 + lower;
  }
  public static int getUpper(int port) {
	 return port / 256;
  }
  public static int getLower(int port) {
	 return port % 256;
  }
  /** grabs the data from the dataport */
  public class dataGrabber implements Runnable {
	 StringBuffer buffer = new StringBuffer();
	 Socket s;
	 boolean done = false;
	 boolean portCreated = false;
	 String host = "";
	 int port = 22;
	 public dataGrabber(String host, int port) throws Exception {
		this.host = host;
		this.port = port;
		new Thread((Runnable) this).start();
	 }
	 public boolean isDone() {
		return done;
	 }
	 public String getData() {
		return buffer.toString();
	 }
	 public boolean isPortCreated() {
		return portCreated;
	 }
	 public void run() {
		try{
		  if (passive) {
			 s = new Socket(host, port);
                         //System.out.println("In Passive Mode.");
		  } else {
			 //System.out.println("creating socket on " + 
port);
			 ServerSocket server = new ServerSocket(port);
			 //System.out.println("accepting...");
			 portCreated = true;
			 s = server.accept();
			 //System.out.println("accepted");
		  }
		} catch (Exception e) {
		}
		try {
		  InputStream in = s.getInputStream();
                  BufferedInputStream dataIn = new BufferedInputStream
(in);
                  int bufferSize = 4096;
                  byte[] inputBuffer = new byte[bufferSize];
		  int i = 0;
		//System.out.println("About to read data.");  
                while ((i = dataIn.read(inputBuffer, 0, bufferSize)) !
= -1) {
                    //buffer.append((char) i);
		  }
		  dataIn.close();
                  System.out.println("Finished reading data.");
		  s.close();
		} catch (Exception e) {
		  System.out.println("FTP client: dataGrabber - " + 
e.toString());
		}
		done = true;
	 }
  } 
  /** disconnect from the server */
  public void disconnect() {
	 try {
		send("QUIT");
		getResponse();
	 } catch (Exception e) {
		System.out.println("FTP client - " + e.toString());
	 }
	 try {
		in.close();
		out.close();
		s.close();
	 } catch (Exception e) {
		System.out.println("FTP client - " + e.toString());
	 }
  }
  /** send a command to the server */
  public void send(String command) throws IOException {
	 System.out.println("#" + command + "#");
	 for (int i = 0; i < command.length(); i++) {
		out.write(command.charAt(i));
                //System.out.println(command.charAt(i));
	 }
	 out.write('\r');
	 out.write('\n');
	 out.flush();
  }
}


-----------End changed file------------


...rob



---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org