KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > sampler > AccessLogSampler


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/sampler/AccessLogSampler.java,v 1.8.2.1 2004/05/20 18:08:54 mstover1 Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.sampler;
20
21 import java.io.IOException JavaDoc;
22 import java.net.HttpURLConnection JavaDoc;
23 import java.net.URL JavaDoc;
24 import org.apache.jmeter.samplers.Entry;
25 import org.apache.jmeter.protocol.http.util.accesslog.LogParser;
26 import org.apache.jmeter.protocol.http.util.accesslog.Generator;
27 //import org.apache.jmeter.protocol.http.util.accesslog.LogFilter;
28
import org.apache.jmeter.samplers.SampleResult;
29
30 /**
31  * Description:<br>
32  * <br>
33  * AccessLogSampler is responsible for a couple of things:<p>
34  * <ul>
35  * <li> creating instances of Generator
36  * <li> creating instances of Parser
37  * <li> triggering popup windows
38  * <li> calling Generator.generateRequest()
39  * <li> checking to make sure the classes are valid
40  * <li> making sure a class can be instantiated
41  * </ul>
42  * The intent of this sampler is it uses the generator and
43  * parser to create a HTTPSampler when it is needed. It
44  * does not contain logic about how to parse the logs. It
45  * also doesn't care how Generator is implemented, as long
46  * as it implements the interface. This means a person
47  * could simply implement a dummy parser to generate
48  * random parameters and the generator consumes the results.
49  * This wasn't the original intent of the sampler. I
50  * originaly wanted to write this sampler, so that I can
51  * take production logs to simulate production traffic in
52  * a test environment. Doing so is desirable to study odd
53  * or unusual behavior. It's also good to compare a new
54  * system against an existing system to get near apples-
55  * to-apples comparison. I've been asked if benchmarks
56  * are really fair comparisons just about every single
57  * time, so this helps me accomplish that task.<p>
58  * Some bugs only appear under production traffic, so it
59  * is useful to generate traffic using production logs.
60  * This way, JMeter can record when problems occur and
61  * provide a way to match the server logs.
62  * <p>
63  * Created on: Jun 26, 2003
64  *
65  * @author Peter Lin
66  * @version $Revision: 1.8.2.1 $ last updated $Date: 2004/05/20 18:08:54 $
67  */

68 public class AccessLogSampler extends HTTPSampler
69 {
70     public static final String JavaDoc DEFAULT_CLASS =
71         "org.apache.jmeter.protocol.http.util.accesslog.TCLogParser";
72
73     public static final String JavaDoc LOG_FILE =
74         "AccessLogSampler.log_file";
75     public static final String JavaDoc PARSER_CLASS_NAME = "AccessLogSampler.parser_class_name";
76     public static final String JavaDoc GENERATOR_CLASS_NAME = "AccessLogSampler.generator_class_name";
77
78     /** private members used by class **/
79     transient private Generator GENERATOR = null;
80     transient private LogParser PARSER = null;
81     //transient private LogFilter FILTER = null; //TODO not used
82
private Class JavaDoc GENERATORCLASS = null;
83     private Class JavaDoc PARSERCLASS = null;
84
85     /**
86      * Set the path where XML messages are stored for random selection.
87      */

88     public void setLogFile(String JavaDoc path)
89     {
90         setProperty(LOG_FILE, path);
91     }
92
93     /**
94      * Get the path where XML messages are stored. this is the directory where
95      * JMeter will randomly select a file.
96      */

97     public String JavaDoc getLogFile()
98     {
99         return getPropertyAsString(LOG_FILE);
100     }
101
102     /**
103      * it's kinda obvious, but we state it anyways. Set the xml file with a
104      * string path.
105      * @param classname - parser class name
106      */

107     public void setParserClassName(String JavaDoc classname)
108     {
109         setProperty(PARSER_CLASS_NAME, classname);
110     }
111
112     /**
113      * Get the file location of the xml file.
114      * @return String file path.
115      */

116     public String JavaDoc getParserClassName()
117     {
118         return getPropertyAsString(PARSER_CLASS_NAME);
119     }
120
121     /**
122      * We give the user the choice of entering their own generator
123      * instead of the default generator. This also has the positive
124      * side effect that users can provide their own generators
125      * without providing a parser. Things like creating a test plan
126      * that randomly generates requests can be support in a simple
127      * way for programmers. Non programmers should use existing
128      * features to accomplish similar task.
129      * @param classname
130      */

131     public void setGeneratorClassName(String JavaDoc classname){
132         setProperty(GENERATOR_CLASS_NAME, classname);
133     }
134
135     /**
136      * Return the name of the generator class to use.
137      * @return generator class name
138      */

139     public String JavaDoc getGeneratorClassName(){
140         return getPropertyAsString(GENERATOR_CLASS_NAME);
141     }
142
143     /**
144      * Set the generator for the Sampler to use
145      * @param gen
146      */

147     public void setGenerator(Generator gen){
148         if (gen == null){
149         } else {
150             GENERATOR = gen;
151         }
152     }
153
154     /**
155      * Return the generator
156      * @return generator
157      */

158     public Generator getGenerator(){
159         return GENERATOR;
160     }
161     
162     /**
163      * Set the parser for the sampler to use
164      * @param parser
165      */

166     public void setParser(LogParser parser){
167         PARSER = parser;
168     }
169
170     /**
171      * Return the parser
172      * @return parser
173      */

174     public LogParser getParser(){
175         return PARSER;
176     }
177     
178     /**
179      * sample gets a new HTTPSampler from the generator and
180      * calls it's sample() method.
181      */

182     public SampleResult sampleWithGenerator()
183     {
184         checkParser();
185         checkGenerator();
186         this.instantiateParser();
187         this.instantiateGenerator();
188         HTTPSampler samp = null;
189         SampleResult res = null;
190         try
191         {
192             samp = (HTTPSampler) GENERATOR.generateRequest();
193             
194             /*samp.setDomain(this.getDomain());
195             samp.setPort(this.getPort());
196             samp.setImageParser(this.isImageParser());*/

197             // we call parse with 1 to get only one.
198
// this also means if we change the implementation
199
// to use 2, it would use every other entry and
200
// so on. Not that it is really useful, but a
201
// person could use it that way if they have a
202
// huge gigabyte log file and they only want to
203
// use a quarter of the entries.
204
PARSER.parse(1);
205             this.addTestElement(samp);
206             res = sample();
207             res.setSampleLabel(toString());
208         }
209         catch (Exception JavaDoc e)
210         {
211             // e.printStackTrace();
212
}
213         return res;
214     }
215
216     /**
217      * sample(Entry e) simply calls sample().
218      * @param e - ignored
219      * @return the new sample
220      */

221     public SampleResult sample(Entry e)
222     {
223         return sampleWithGenerator();
224     }
225
226     /**
227      * Method will instantiate the generator. This is done to
228      * make it easier for users to extend and plugin their
229      * own generators.
230      */

231     public void instantiateGenerator(){
232         if (this.getGeneratorClassName() != null
233             && this.getGeneratorClassName().length() > 0)
234         {
235             try
236             {
237                 GENERATOR = (Generator) GENERATORCLASS.newInstance();
238                 if (GENERATOR != null)
239                 {
240                     if (this.getDomain() != null)
241                     {
242                         GENERATOR.setHost(this.getDomain());
243                     }
244                     if (this.getPort() > 0)
245                     {
246                         GENERATOR.setPort(this.getPort());
247                     }
248                 }
249                 if (PARSER != null && GENERATOR != null)
250                 {
251                     PARSER.setGenerator(GENERATOR);
252                 }
253             }
254             catch (InstantiationException JavaDoc e)
255             {
256                 // e.printStackTrace();
257
}
258             catch (IllegalAccessException JavaDoc e)
259             {
260                 // e.printStackTrace();
261
}
262         }
263     }
264     
265     /**
266      * Method will instantiate the generator. This is done to
267      * make it easier for users to extend and plugin their
268      * own generators.
269      */

270     public boolean checkGenerator(){
271         if (this.getGeneratorClassName() != null
272             && this.getGeneratorClassName().length() > 0)
273         {
274             try
275             {
276                 if (GENERATORCLASS == null)
277                 {
278                     GENERATORCLASS =
279                         Class.forName(this.getGeneratorClassName());
280                 }
281                 return true;
282             }
283             catch (ClassNotFoundException JavaDoc e)
284             {
285                 // since samplers shouldn't deal with
286
// gui stuff, bad class names will
287
// fail silently.
288
return false;
289             }
290         }
291         return false;
292     }
293     
294     /**
295      * Method will instantiate the log parser based on
296      * the class in the text field. This was done to
297      * make it easier for people to plugin their own log parser
298      * and use different log parser.
299      */

300     public void instantiateParser()
301     {
302         if (this.getParserClassName() != null && this.getParserClassName().length() > 0)
303         {
304             try
305             {
306                 if (PARSER == null)
307                 {
308                     if (this.getLogFile() != null
309                         && this.getLogFile().length() > 0)
310                     {
311                         PARSER = (LogParser) PARSERCLASS.newInstance();
312                         PARSER.setSourceFile(this.getLogFile());
313                     }
314                 }
315             }
316             catch (InstantiationException JavaDoc e)
317             {
318                 // since samplers shouldn't deal with
319
// gui stuff, bad class names will
320
// fail silently.
321
}
322             catch (IllegalAccessException JavaDoc e)
323             {
324                 // since samplers shouldn't deal with
325
// gui stuff, bad class names will
326
// fail silently.
327
}
328         }
329     }
330
331     /**
332      * Method will instantiate the log parser based on
333      * the class in the text field. This was done to
334      * make it easier for people to plugin their own log parser
335      * and use different log parser.
336      * @return true if parser exists or was created
337      */

338     public boolean checkParser()
339     {
340         if (this.getParserClassName() != null && this.getParserClassName().length() > 0)
341         {
342             try
343             {
344                 if (PARSERCLASS == null)
345                 {
346                     PARSERCLASS = Class.forName(this.getParserClassName());
347                 }
348                 return true;
349             }
350             catch (ClassNotFoundException JavaDoc e)
351             {
352                 // since samplers shouldn't deal with
353
// gui stuff, bad class names will
354
// fail silently.
355
return false;
356             }
357         } else {
358             return false;
359         }
360     }
361
362     /**
363      * We override this to prevent the wrong encoding and provide no
364      * implementation. We want to reuse the other parts of HTTPSampler, but not
365      * the connection. The connection is handled by the Apache SOAP driver.
366      */

367     public void addEncodedArgument(String JavaDoc name, String JavaDoc value, String JavaDoc metaData)
368     {
369     }
370
371     /**
372      * We override this to prevent the wrong encoding and provide no
373      * implementation. We want to reuse the other parts of HTTPSampler, but not
374      * the connection. The connection is handled by the Apache SOAP driver.
375      */

376     protected HttpURLConnection JavaDoc setupConnection(URL JavaDoc u, String JavaDoc method)
377         throws IOException JavaDoc
378     {
379         return null;
380     }
381
382     /**
383      * We override this to prevent the wrong encoding and provide no
384      * implementation. We want to reuse the other parts of HTTPSampler, but not
385      * the connection. The connection is handled by the Apache SOAP driver.
386      */

387     protected long connect() throws IOException JavaDoc
388     {
389         return -1;
390     }
391 }
392
Popular Tags