| 1 14 15 package xmladder; 16 17 import java.io.*; 18 import java.net.*; 19 import java.util.*; 20 21 public class LoadTest { 22 private Agent agents[] = null; 23 private ThreadGroup agentGroup = null; 24 private List times = null; 25 26 public void init(int count, String host, int port) { 27 System.out.print("Starting init.."); 28 agentGroup = new ThreadGroup ("AgentGroup"); 29 times = new ArrayList(count); 30 agents = new Agent[count]; 31 for(int i=0;i<count;i++) { 32 agents[i] = new Agent(agentGroup, times, host, port); 33 agents[i].start(); 34 } 35 System.out.println(" "+agentGroup.activeCount()+" ready."); 36 } 37 38 public void test() throws InterruptedException { 39 int count = agents.length; 40 int half = count/2; 41 42 System.out.print("Starting test.."); 43 long stime = System.currentTimeMillis(); 44 long htime = -1; 45 long hcount = -1; 46 47 51 synchronized(agentGroup) { 52 agentGroup.notifyAll(); 53 } 54 55 int ac = times.size(); 56 while(ac<count) { 57 if(htime<0 && half>=ac) { 58 htime = System.currentTimeMillis(); 59 hcount = count-ac; 60 } 61 Thread.sleep(5); 62 ac = times.size(); 63 } 64 65 long etime = System.currentTimeMillis(); 66 System.out.println("Done\n"); 67 68 69 long time = etime - stime; 70 System.out.println("Total Time : "+time+"ms"); 71 72 time = time/count; 73 System.out.println("Avg. Time : "+time+"ms\n"); 74 75 83 84 91 } 92 93 public static void main(String args[]) throws Exception { 94 LoadTest lt = new LoadTest(); 95 96 String host = "127.0.0.1"; 97 int port = 2222; 98 int count = 50; 99 100 if(args.length>0) 101 count = Integer.parseInt(args[0]); 102 if(args.length>1) 103 host = args[1]; 104 if(args.length>2) 105 port = Integer.parseInt(args[2]); 106 107 lt.init(count, host, port); 108 109 Thread.sleep(200); lt.test(); 111 } 112 } 113 114 class Agent extends Thread { 115 private static int count = 0; 116 117 XmlAdderClient client = null; 118 List list = null; 119 120 public Agent(ThreadGroup threadGroup, List list, String host, int port) { 121 super(threadGroup, null, "Agent:"+ ++count); 122 client = new XmlAdderClient(host, port); 123 this.list = list; 124 } 125 126 public void run() { 127 synchronized(getThreadGroup()) { 128 try { 129 getThreadGroup().wait(); 130 } catch(Exception e) { 131 System.err.println("Error is wait! "+e); 132 } 133 } 134 client.test(); 135 list.add(""+client.getTime()); 136 } 137 } 138 | Popular Tags |