KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > recorder > Simulator


1 /*
2  * $Id: Simulator.java,v 1.3 2004/12/01 07:54:26 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.recorder;
15
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.PrintStream JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21
22 public class Simulator {
23     public static void main(String JavaDoc[] args) {
24         int index = 0;
25         boolean fail = false;
26
27         float delay = 1.0f;
28         String JavaDoc auth = "BASIC";
29         String JavaDoc userid = "user ";
30         String JavaDoc password = null;
31         int iterations = 1;
32         int ramp = 0;
33
34         try {
35             while (index < args.length) {
36                 if ("-d".equals(args[index]) || "--delay".equals(args[index]))
37                     delay = new Float JavaDoc(args[index + 1]).floatValue();
38                 else if ("-a".equals(args[index]) || "--auth".equals(args[index]))
39                     auth = args[index + 1];
40                 else if ("-u".equals(args[index]) || "--userid".equals(args[index]))
41                     userid = args[index + 1];
42                 else if ("-p".equals(args[index]) || "--password".equals(args[index]))
43                     password = args[index + 1];
44                 else if ("-i".equals(args[index]) || "--iterations".equals(args[index]))
45                     iterations = new Integer JavaDoc(args[index + 1]).intValue();
46                 else if ("-r".equals(args[index]) || "--ramp".equals(args[index]))
47                     ramp = new Integer JavaDoc(args[index + 1]).intValue();
48                 else if ("-o".equals(args[index]) || "--output".equals(args[index]))
49                     System.setOut(new PrintStream JavaDoc(new FileOutputStream JavaDoc(args[index + 1])));
50                 else
51                     break;
52
53                 index += 2;
54             }
55         } catch (Exception JavaDoc e) { fail = true; }
56
57         if (fail || args.length < index + 2) {
58             printUsage();
59             System.exit(1);
60         }
61
62         String JavaDoc url = args[index];
63         String JavaDoc scriptClassName = args[index + 1];
64
65         int firstUser;
66         int lastUser;
67         int pos = args[index + 2].indexOf("-");
68         if (pos > -1) {
69             String JavaDoc a = args[index + 2].substring(0, pos);
70             String JavaDoc b = args[index + 2].substring(pos + 1);
71             firstUser = Integer.valueOf(a).intValue();
72             lastUser = Integer.valueOf(b).intValue();
73         } else {
74             firstUser = 1;
75             lastUser = Integer.valueOf(args[index + 2]).intValue();
76         }
77
78         try {
79             Class JavaDoc scriptClass = Class.forName(scriptClassName);
80
81             System.out.println("INFO: initializing clients for " +
82                     userid + firstUser + " to " + userid + lastUser);
83             List JavaDoc clients = new LinkedList JavaDoc();
84             for (int i = firstUser; i <= lastUser; i++) {
85                 Script script = (Script) scriptClass.newInstance();
86                 script.setUrl(url);
87                 script.setDelay(delay);
88
89                 Client client = new Client();
90                 client.init(script);
91                 client.setUserid(userid + i);
92                 client.setPasswd(password);
93                 client.setInitialDelay((i - firstUser) * ramp);
94                 client.setIterations(iterations);
95                 clients.add(client);
96             }
97
98             System.out.println("INFO: starting clients");
99             Iterator JavaDoc it = clients.iterator();
100             while (it.hasNext()) {
101                 Client client = (Client) it.next();
102                 client.start();
103             }
104         } catch (Exception JavaDoc e) {
105             System.err.println(e.getMessage());
106             e.printStackTrace(System.err);
107         }
108     }
109
110     protected static void printUsage() {
111         System.err.println("usage: simulator [options] url script_class user_range");
112         System.err.println("arguments:");
113         System.err.println(" url url of application client jar with deployment descriptors");
114         System.err.println(" script_class full qualified classname");
115         System.err.println(" user_range 1-'lastUser' or 'firstUser'-'lastUser'");
116         System.err.println("options:");
117         System.err.println(" -d, --delay [0..t] factor, that is applied to the recorded delays");
118         System.err.println(" 0.0 for no delays, 1.0 for same tempo as during recording (default)");
119         System.err.println(" -a, --auth the auth type, one of HTTP, JBoss (more to follow), defaults to HTTP");
120         System.err.println(" -u, --userid the base userid, to which firstUser .. lastUser will be appended, defaults to 'demo'");
121         System.err.println(" -p, --password the password for all users");
122         System.err.println(" -i, --iterations number of iterations a virtual user runs the script, defaults to 'start'");
123         System.err.println(" -r, --ramp load ramp, n'th client is started after n times the delay in ms, default is 0");
124         System.err.println(" -o, --output redirect output to a file");
125     }
126 }
127
128
129
Popular Tags