1 9 package org.jboss.portal.test.cms.stress; 10 11 import org.jboss.portal.cms.NodeFactory; 12 import org.apache.log4j.Logger; 13 14 import java.util.List ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.Iterator ; 18 import java.io.StringWriter ; 19 import java.io.PrintWriter ; 20 21 25 public class Client implements Runnable 26 { 27 28 protected int id; 29 30 protected int loops; 31 32 protected Command[] scenario; 33 34 protected List results; 35 36 protected long waitTimeBetweenScenariosMillis; 37 38 protected NodeFactory factory; 39 40 protected Logger log; 41 42 public Client(int id, int loops, Command[] scenario, long waitTimeBetweenScenariosMillis, NodeFactory factory) 43 { 44 this.log = Logger.getLogger(Client.class.getName() + "." + id); 45 this.id = id; 46 this.loops = loops; 47 this.scenario = scenario; 48 this.waitTimeBetweenScenariosMillis = waitTimeBetweenScenariosMillis; 49 this.factory = factory; 50 this.results = new ArrayList (); 51 } 52 53 public List getResults() 54 { 55 return results; 56 } 57 58 public void run() 59 { 60 while (loops-- > 0) 61 { 62 try 63 { 64 play(); 65 Thread.sleep(waitTimeBetweenScenariosMillis); 66 } 67 catch (InterruptedException e) 68 { 69 e.printStackTrace(); 70 } 71 } 72 } 73 74 protected void play() 75 { 76 Result result = new Result(); 77 78 for (int i = 0; i < scenario.length; i++) 80 { 81 try 82 { 83 Command command = scenario[i]; 84 log.info("Executing " + command); 85 command.execute(result, factory); 86 } 87 catch (Exception e) 88 { 89 e.printStackTrace(); 90 StringWriter swriter = new StringWriter (); 91 PrintWriter pwriter = new PrintWriter (swriter); 92 e.printStackTrace(pwriter); 93 result.addFailure(new Failure(Failure.EXCEPTION, swriter.toString())); 94 } 95 } 96 97 results.add(result); 99 } 100 101 public static class Result 102 { 103 104 private final List failures; 105 106 public Collection getFailures() 107 { 108 return failures; 109 } 110 111 private Result() 112 { 113 this.failures = new ArrayList (); 114 } 115 116 private void addFailure(Failure failure) 117 { 118 failures.add(failure); 119 } 120 121 public void assertTrue(boolean b, String msg) 122 { 123 if (!b) 124 { 125 failures.add(new Failure(Failure.ASSERT, msg)); 126 } 127 } 128 129 public void assertFalse(boolean b, String msg) 130 { 131 assertTrue(!b, msg); 132 } 133 } 134 } 135 | Popular Tags |