1 5 package org.exoplatform.test.web; 6 7 import groovy.lang.GroovyClassLoader; 8 import java.net.URL ; 9 10 16 public class WebLoadRunner { 17 private int numberOfWorker_ = 1 ; 18 private int numberOfTask_ = 1 ; 19 private boolean validateWellFormedXhtml_ = true ; 20 private boolean validateWebUnit_ = true ; 21 private String defaultURL_ ; 22 23 private WorkerThread[] workers_ ; 24 private Tasks tasks_ ; 25 26 public WebLoadRunner() { 27 } 28 29 public WebLoadRunner(int numberOfUser, int numberOfTask) { 30 numberOfWorker_ = numberOfUser ; 31 numberOfTask_ = numberOfTask ; 32 } 33 34 public void setValidateWellFormedXhtml(boolean b) { validateWellFormedXhtml_ = b ; } 35 public void setValidateWebUnit(boolean b) { validateWebUnit_ = b ; } 36 public void setDefaultURL(String s) { defaultURL_ = s ; } 37 public void setNumberOfWorker(int number) { numberOfWorker_ = number ; } 38 public void setNumberOfTask(int number) { numberOfTask_ = number ; } 39 40 public void run(ExoWebClient webClient) { 41 if(isRunning()) return ; 42 tasks_ = new Tasks(numberOfTask_ , webClient); 43 ThreadGroup threadGroup_ = new ThreadGroup ("Group Of Worker") ; 44 int limit = numberOfWorker_ ; 45 int unfinishedTaskCounter = tasks_.getUnfinishedTaskCounter() ; 46 if (limit > unfinishedTaskCounter) limit = unfinishedTaskCounter ; 47 workers_ = new WorkerThread[limit] ; 48 for (int i = 0; i < limit ; i++) { 49 workers_[i] = new WorkerThread(threadGroup_, "worker-" + i, tasks_) ; 50 workers_[i].setPriority(Thread.MIN_PRIORITY) ; 51 workers_[i].start() ; 52 } 53 } 54 55 public boolean isRunning() { 56 if(workers_ == null) return false ; 57 for(int i = 0 ; i < workers_.length; i++) { 58 if(workers_[i].isAlive()) return true ; 59 } 60 return false ; 61 } 62 63 public void stop() { 64 if(workers_ == null) return ; 65 for(int i = 0 ; i < workers_.length; i++) { 66 if(workers_[i].isAlive()) { 67 workers_[i].interrupt(); 68 } 69 } 70 } 71 72 public int getUnfinishedTaskCounter() { 73 if(tasks_ == null) return 0 ; 74 return tasks_.getUnfinishedTaskCounter() ; 75 } 76 77 public void run(TestSuites suites) throws Exception { 78 ExoWebClient webClient = new ExoWebClient("exo", defaultURL_); 79 webClient.setValidateWebUnit(validateWebUnit_) ; 80 webClient.setValidateWellFormedXhtml(validateWellFormedXhtml_) ; 81 webClient.setSuites(suites); 82 run(webClient) ; 83 while(isRunning()) Thread.sleep(1000) ; 84 } 85 86 static private Object createGroovyObject(String groovyscript) throws Exception { 87 System.out.println("groovy src : " + groovyscript) ; 88 ClassLoader parentLoader = WebLoadRunner.class.getClassLoader() ; 89 GroovyClassLoader classloader = new GroovyClassLoader(parentLoader) ; 90 URL classURL = classloader.getResource(groovyscript) ; 91 Class clazz = classloader.parseClass(classURL.openStream()) ; 92 return clazz.newInstance() ; 93 } 94 95 static public void main(String [] args) throws Exception { 96 String runnableScript = args[0] ; 97 Runnable runnable = (Runnable )createGroovyObject(runnableScript) ; 98 runnable.run() ; 99 } 100 101 121 } 122 | Popular Tags |