?? forumloadtest.java
字號(hào):
package org.redsoft.forum;import java.io.File;import java.io.FileInputStream;import java.util.Properties;import com.meterware.httpunit.GetMethodWebRequest;import com.meterware.httpunit.HttpUnitOptions;import com.meterware.httpunit.WebConversation;import com.meterware.httpunit.WebForm;import com.meterware.httpunit.WebRequest;import com.meterware.httpunit.WebResponse;/** * A program that would keep sending http request to the online box to test * how the forum works under heavy load * * @author Charles Huang * @version 1.0 */public class ForumLoadTest{ int currentUser = -1; public void setConcurrentUsers( int number ){ currentUser = number; } public ForumLoadTest() throws Exception { /*System.getProperties().put( "proxySet", "true" ); System.getProperties().put( "proxyHost", "proxy.compaq.com" ); System.getProperties().put( "proxyPort", "8080" );*/ } public void bombard(){ for( int index = 1; index <=currentUser; index++ ){System.out.println("creating thread_" + index ); new HrefThread( index ).start(); } } public static void main(String args[]) { try{ final ForumLoadTest loadTest = new ForumLoadTest(); loadTest.setConcurrentUsers( Integer.parseInt( args[0] ) ); loadTest.bombard(); }catch( final Exception e ){ e.printStackTrace(); } }}/** * A thread that simulates that a client accesses the website by click a href * link */class HrefThread extends Thread{ String uri; int id; final Properties props = new Properties(); String mainScreenURI; final WebConversation conversation = new WebConversation(); public HrefThread( final int id ){ this.id = id; try{ props.load( new FileInputStream( new File("loadTest.properties") )); }catch( final Exception e ){ e.printStackTrace(); } } public void run(){ String uri = props.getProperty( "uri_" + id); String login = props.getProperty( "uri_logon"); while( true ){ try{ HttpUnitOptions.setDefaultCharacterSet("gb2312"); WebRequest request = new GetMethodWebRequest( "http://localhost/forum/logon.jsp" ); WebResponse response = conversation.getResponse( request ); WebForm form = response.getForms()[0]; request = form.getRequest(); request.setParameter( "userName", "charles" ); request.setParameter( "password", "charles" ); response = conversation.getResponse( request ); String url = props.getProperty( "uri_" + id ); request = new GetMethodWebRequest( url ); System.out.println( conversation.getResponse( request ).getTitle() ); sleep( 100 ); }catch (InterruptedException e){} catch ( Exception e ){ e.printStackTrace();} } }}//EOC
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -