KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > web > WebLoadRunner


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.test.web;
6
7 import groovy.lang.GroovyClassLoader;
8 import java.net.URL JavaDoc;
9
10 /**
11  * May 22, 2004
12  * @author: Tuan Nguyen
13  * @email: tuan08@users.sourceforge.net
14  * @version: $Id: WebLoadRunner.java,v 1.1 2004/10/11 23:36:03 tuan08 Exp $
15  **/

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 JavaDoc 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 JavaDoc 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 JavaDoc threadGroup_ = new ThreadGroup JavaDoc("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 JavaDoc {
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 JavaDoc createGroovyObject(String JavaDoc groovyscript) throws Exception JavaDoc {
87     System.out.println("groovy src : " + groovyscript) ;
88     ClassLoader JavaDoc parentLoader = WebLoadRunner.class.getClassLoader() ;
89     GroovyClassLoader classloader = new GroovyClassLoader(parentLoader) ;
90     URL JavaDoc classURL = classloader.getResource(groovyscript) ;
91     Class JavaDoc clazz = classloader.parseClass(classURL.openStream()) ;
92     return clazz.newInstance() ;
93   }
94   
95   static public void main(String JavaDoc[] args) throws Exception JavaDoc {
96     String JavaDoc runnableScript = args[0] ;
97     Runnable JavaDoc runnable = (Runnable JavaDoc)createGroovyObject(runnableScript) ;
98     runnable.run() ;
99   }
100   
101   /*
102   static public void main(String[] args) throws Exception {
103     WebLoadRunner runner = new WebLoadRunner() ;
104     runner.setDefaultURL(args[0]) ;
105     runner.setNumberOfWorker(Integer.parseInt(args[1])) ;
106     runner.setNumberOfTask(Integer.parseInt(args[2])) ;
107     runner.setValidateWebUnit("true".equals(args[3])) ;
108     runner.setValidateWellFormedXhtml("true".equals(args[4])) ;
109     String className = args[5] ;
110     TestSuites suites = null ;
111     if(className.endsWith(".groovy")) {
112       suites = (TestSuites)createGroovyObject(className) ;
113     } else {
114       Class clazz = Class.forName(className) ;
115       suites = (TestSuites)clazz.newInstance() ;
116     }
117     runner.run(suites) ;
118     System.out.print(suites.getTextSummary()) ;
119   }
120   */

121 }
122
Popular Tags