From: abretz <no...@us...> - 2004-09-17 14:21:08
|
Comments: Update of /cvsroot/opendte/opendte-ng/src/examples/org/opendte/testcase/examples/http In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5313 Modified Files: HTTPTest.java Log Message: The Tescase interface was changed, so this test was reworked. Index: HTTPTest.java =================================================================== RCS file: /cvsroot/opendte/opendte-ng/src/examples/org/opendte/testcase/examples/http/HTTPTest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- HTTPTest.java 19 Aug 2004 13:47:39 -0000 1.7 +++ HTTPTest.java 17 Sep 2004 14:20:56 -0000 1.8 @@ -18,13 +18,13 @@ package org.opendte.testcase.examples.http; +import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URL; -import java.net.URLClassLoader; import java.util.Properties; -import org.opendte.node.Node; -import org.opendte.node.NodeInfo; +import org.opendte.test.TcException; import org.opendte.test.Testcase; /** @@ -36,182 +36,80 @@ * @author Diedrich Pfeifle */ public class HTTPTest - implements Runnable, Testcase + implements Testcase { - private int mNumberOfThreads, mNumberOfCycles; + private static final int BUF_SIZE = 1024; private URL mDestinationURL; - private Thread[] mThreads; - private Properties mProperties; - - private Node mNode; + private final byte[] mBuf = new byte[BUF_SIZE]; /** * Constructor. */ public HTTPTest() { - System.out.println("Classloader of HTTPTest: " - + Thread.currentThread().getContextClassLoader()); - final URLClassLoader urlcl = (URLClassLoader) - Thread.currentThread().getContextClassLoader(); - final URL[] urls = urlcl.getURLs(); - for (int i = 0; i < urls.length; i++) - { - System.out.println("--> " + urls[i]); - } - - System.out.println("HTTPTest created"); } /** * Testcase initialization. * - * @param node The node on which this testcase is being executed. * @param props The testcase properties. - * @see org.opendte.test.Testcase#tcInit(Node, Properties) + * @throws TcException thrown in error cases. + * @see org.opendte.test.Testcase#tcInit(Properties) */ - public void tcInit(final Node node, final java.util.Properties props) + public void tcInit (final Properties props) + throws TcException { - mNode = node; - try { - mProperties = props; - - mNumberOfThreads = Integer.parseInt( - mProperties.getProperty("HTTP.Threads", "1")); - mNumberOfCycles = Integer.parseInt( - mProperties.getProperty("HTTP.Cycles", "1")); - mDestinationURL = new URL( - mProperties.getProperty("HTTP.Destination", - "http://www.yahoo.com")); - - mThreads = new Thread[mNumberOfThreads]; - for (int i = 0; i < mNumberOfThreads; i++) - { - mThreads[i] = new Thread(this); - } - - System.out.println("HTTPTest initialized"); - - mNode.updateState(NodeInfo.STATUS_INITIALIZED); + mDestinationURL = new URL(props.getProperty("HTTP.Destination", + "http://www.yahoo.com")); } - catch (Exception e) + catch (MalformedURLException e) { - e.printStackTrace(); - mNode.updateState(NodeInfo.STATUS_FAILED); + throw new TcException(e); } } /** - * Testcase startup. - * - * @see org.opendte.test.Testcase#tcStart() + * Starts the test execution. + * @throws TcException thrown in error cases. */ - public void tcStart() + public void tcStart () + throws TcException { - System.out.println("HTTPTest started"); - for (int i = 0; i < mNumberOfThreads; i++) + InputStream in = null; + try { - mThreads[i].start(); - } - - final Thread t = new Thread() + in = mDestinationURL.openStream(); + while (in.read(mBuf) > 0) { - public void run() - { - for (int i = 0; i < mNumberOfThreads; i++) - { - try - { - mThreads[i].join(); - } - catch (InterruptedException iex) - { - iex.printStackTrace(); - } - } - } - }; - t.start(); - - mNode.updateState(NodeInfo.STATUS_RUNNING); - } - - /** - * Testcase stop. - * - * @see org.opendte.test.Testcase#tcStop() - */ - public void tcStop() - { - for (int i = 0; i < mNumberOfThreads; i++) + // empty loop + } + closeStream(in); + in = null; + } + catch (IOException e) { - mThreads[i] = null; + closeStream(in); } - System.out.println("HTTPTest stopped"); - - mNode.updateState(NodeInfo.STATUS_STOPPED); - } - - /** - * This method is called when the user changed the spec properties - * for an already deployed testcase. - * - * @param props The new properties. - * @see org.opendte.test.Testcase#changedProperties(java.util.Properties) - */ - public void changedProperties (final Properties props) - { - System.out.println("Properties changed: " + props); } /** - * This is the actual worker method that sends the requests. - * - * @see java.lang.Thread#run() + * Closes the given InputStrem <code>in</code>. + * @param in The InputStream to be closed; */ - public void run() + private void closeStream (final InputStream in) { - long lTotalTime = 0L; - for (int i = 0; i < mNumberOfCycles; i++) + if (in != null) { try { - final long lStartTime = System.currentTimeMillis(); - - final InputStream in = mDestinationURL.openStream(); - final byte[] buf = new byte[1024]; - while (in.read(buf) > 0) - { - // empty loop - } in.close(); - - final long lStopTime = System.currentTimeMillis(); - lTotalTime += (lStopTime - lStartTime); - - System.out.print("*"); System.out.flush(); - } - catch (Exception e) - { - System.out.print("X"); System.out.flush(); - } - - try - { - Thread.sleep(1000); } - catch (Exception x) + catch (IOException e1) { - // ignore + e1.printStackTrace(); } } - - System.out.print("Average time: " + (lTotalTime / mNumberOfCycles)); - - System.out.flush(); - - mNode.updateState(NodeInfo.STATUS_FINISHED); } } |