View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.test.performance.upgrade;
20  
21  import java.util.Date;
22  
23  import jgroup.core.ConfigManager;
24  import jgroup.core.registry.DependableRegistry;
25  import jgroup.core.registry.RegistryFactory;
26  import jgroup.relacs.config.AppConfig;
27  import jgroup.test.performance.SpeedTest;
28  import jgroup.upgrade.UpgradeManagementClient;
29  
30  import org.apache.log4j.Logger;
31  
32  
33  
34  /**
35   *  Test suite for measuring the performance of a server during the upgrade process.
36   *  The class parses the experiment parameter from command line. The following
37   *  parameters are interpreted:
38   *
39   *  <p>
40   *  @param -method type of test calls sent to the server
41   *  @param -calibrate number of calibration calls
42   *  @param -rate the rate at which requests are sent to the test server
43   *  @param -time the time of the cycle in seconds
44   *  @param -cycles number of cycles of request bursts
45   *  @param -upgradeStart the cycle number at which the upgrade starts
46   *  @param -clients number of local clients generating the requests
47   *          This parameter is left for compatibility but is not considereed.
48   *  @param -server name at which the test server is registered at the repository
49   *  @param -verbose verbose way of performing the experiment 
50   *
51   *  @author       Marcin Solarski
52   *  @since        Jgroup 2.1
53   */
54  public class SimpleUpgradeTestSuite
55  {
56  
57  
58    /** Obtain logger for this class */
59    private static Logger log = Logger.getLogger(SimpleUpgradeTestSuite.class);
60    private static SimpleUpgradeTestSuite instance = null;
61    private boolean verbose;
62    private int method;
63    private int calibReqCallNo;
64    private int rps;
65    private int expTime;
66    private int cycles;
67    private int upgrade_cycle;
68    private int client_no;
69    private String serverName;
70    private SpeedTest server; 
71    private float avRT[];
72    private long startCycleTime[];
73   
74    private static final long  TIME_UNIT = 1000;
75  
76    public static void main(String[] argv) throws Exception
77    {
78      ConfigManager.init();
79      if (instance == null)
80        instance = new SimpleUpgradeTestSuite(argv);
81    }
82    
83    public SimpleUpgradeTestSuite(String argv[]) throws Exception {
84      if( argv.length <1 ) help();
85      verbose = PUtil.getSwitch(argv, "-v");
86      method = PUtil.getIntParameter(argv, "-method", "1");
87      calibReqCallNo = PUtil.getIntParameter(argv, "-calibrate", "10");
88      rps = PUtil.getIntParameter(argv, "-rate", "1");
89      expTime = PUtil.getIntParameter(argv, "-time", "10");
90      cycles = PUtil.getIntParameter(argv, "-cycles", "1");
91      upgrade_cycle = PUtil.getIntParameter(argv, "-upgradeStart", "-1");
92      client_no = PUtil.getIntParameter(argv, "-clients", "1");
93      serverName = PUtil.getStringParameter(argv, "-server", "UpgradeServer");
94      //package name is hard coded
95      String cl_name = "jgroup.test.performance." + serverName;
96      int rep_lvl = 0;
97      ConfigManager.init();
98      rep_lvl = AppConfig.getApplication(cl_name).getInitialRedundancy();
99  
100     ClientThreadPool client[] = new ClientThreadPool[client_no];
101 
102     int times = expTime*rps;
103     DependableRegistry reg = RegistryFactory.getRegistry();
104     server = (SpeedTest) reg.lookup(serverName);
105     int call_no = 0;
106     
107     avRT = new float[cycles+1];
108     startCycleTime = new long[cycles+1];
109     SimpleTestThread testRun = new SimpleTestThread(Thread.currentThread());
110     float allowedDelay = 1.20f;
111     testRun.start();
112     long expStartTime = System.currentTimeMillis();
113     long expEndTime = expStartTime + expTime*cycles*TIME_UNIT;
114     boolean terminated_ok = false;
115     System.out.println("Test started. Should be fininshed in " + expTime*cycles + "sec.");
116     try { Thread.sleep((long)(expTime*cycles*allowedDelay*TIME_UNIT)); }
117     catch (InterruptedException e) {
118        terminated_ok = true;
119     }
120     
121    
122     /*for(int i=0; i< client_no; i++) 
123       client[i].CycleDone(cycles);
124     ClientThreadPool.allStop();
125 */
126     //header printout
127      if( verbose ) {
128        expEndTime = System.currentTimeMillis();
129        String date = new Date(expStartTime).toString();
130        log.info("R" + rep_lvl + " m" + method + " r" + rps) ;
131        log.info("#Experiment started at: " + new Date(expStartTime));
132        String term_mesg = terminated_ok ? " ok" : " not ok";
133        log.info("#Experiment terminated at: " + new Date(expEndTime) + term_mesg);
134        log.info("#Testing suite with the following parameters: ");
135        log.info("#Server name:                \t"+ serverName);
136        log.info("#Server replication level:   \t"+ rep_lvl);
137        log.info("#Number of calibration calls:\t" + calibReqCallNo);
138        log.info("#Test method:                \t" + method);
139        log.info("#Request rate per sec:       \t" + rps);
140        log.info("#Cycle time:                 \t" + expTime);
141        log.info("#Cycles number:              \t"+ cycles);
142        log.info("#Staring upgrade at cycle:   \t" + upgrade_cycle);
143     }
144 
145    StringBuilder header = new StringBuilder("#Cycle\tT0\tdT");
146     for(int i=0; i< client_no; i++) 
147       header.append("\t" + "RT[" + i +"]");
148     log.info(header);
149    
150     for(int j=1; j< testRun.getLastCycleTerminated(); j++) {
151       long T0 = startCycleTime[j];
152       long dT = j>1 ? T0-startCycleTime[j-1] : 0;
153       StringBuilder line = new StringBuilder(""+j + "\t" + T0 + "\t" + dT);
154       for(int i=0; i< client_no; i++) 
155           line.append("\t" + avRT[j]);
156       log.info(line);
157     }
158       
159     //log.info("Stopping all threads");
160     //ClientThreadPool.allStop();
161     System.exit(0);
162 
163  }
164 
165 
166 
167   static void help() {
168      System.out.println("SimpleUpgradeTestSuite [-v][-method M][-calibrate C][-rate R][-time T][cycles Y][-clientsi N][-upgradeStart U][-server ServerName]");
169   } 
170 
171   class SimpleTestThread extends Thread {
172      private Thread mainThread;
173      private int j=1;
174      private UpgradeStart upgradeT;
175  
176      private UpgradeClientTest test;
177      public int getLastCycleTerminated() { return j-1; }
178 
179      //SimpleTestThread(Thread t, int method, int calibReqCallNo, int cycles, int rps, int times) { 
180      SimpleTestThread(Thread t) {
181 
182       upgradeT = new UpgradeStart();
183       mainThread= t; 
184 
185       test = new UpgradeClientTest(server);
186       avRT[0] = test.calibrate(method, calibReqCallNo);
187      }
188 
189      public void run() { 
190       //runTest(cyc, rate, calls); 
191       runTest(cycles, rps, expTime*rps); 
192       mainThread.interrupt(); 
193      } 
194  
195      private void runTest(int cycles, int rps, int times) {
196        for(j=1; j<= cycles; j++) { 
197         startCycleTime[j] = System.currentTimeMillis();
198         avRT[j] = test.generate_traffic(rps, times);
199         if(j == upgrade_cycle ) { upgradeT.start(); System.out.println("upgrade started at cycle" + j);}
200         if(j%5==0) System.out.println("Cycles done: " + j +" out of " + cycles);
201        }
202      }
203   }
204 
205 
206   class UpgradeStart extends Thread { 
207 	  String argv[] = new String[] {"u"}; 
208      public void run(){
209 	 try { UpgradeManagementClient.main(argv);
210 	 } catch(Exception e) {log.error("Exception in main", e);}
211 
212      }
213 
214 
215   }
216 
217   
218 }