KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > control > gui > AccessLogSamplerGui


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/control/gui/Attic/AccessLogSamplerGui.java,v 1.11 2004/03/05 01:38:02 sebb 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.control.gui;
20
21 import java.awt.Font JavaDoc;
22
23 import javax.swing.JLabel JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.JCheckBox JavaDoc;
26 import javax.swing.border.Border JavaDoc;
27 import javax.swing.border.EmptyBorder JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.JOptionPane JavaDoc;
31
32 import org.apache.jmeter.protocol.http.sampler.AccessLogSampler;
33 import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
34 import org.apache.jmeter.testelement.TestElement;
35 import org.apache.jmeter.util.JMeterUtils;
36 import org.apache.jmeter.gui.UnsharedComponent;
37 import org.apache.jmeter.gui.util.FilePanel;
38 import org.apache.jorphan.gui.JLabeledTextField;
39 import org.apache.jorphan.gui.layout.VerticalLayout;
40
41 import junit.framework.TestCase;
42
43 /**
44  * Title: JMeter Access Log utilities<br>
45  * Copyright: Apache.org<br>
46  * Company: nobody<br>
47  * License:<br>
48  * <br>
49  * Look at the apache license at the top.<br>
50  * <br>
51  * Description:<br>
52  * So what is this log Sampler GUI? It is a sampler that
53  * can take Tomcat access logs and use them directly. I
54  * wrote a tomcat access log parser to convert each line
55  * to a normal HttpSampler. This way, you can stress
56  * test your servers using real production traffic. This
57  * is useful for a couple of reasons. Some bugs are
58  * really hard to track down, which only appear under
59  * production traffic. Therefore it is desirable to use
60  * the actual queries to simulate the same exact condition
61  * to facilitate diagnosis.<p>
62  * If you're working on a project to replace an existing
63  * site, it is a good way to simulate the same exact
64  * use pattern and compare the results. The goal here is
65  * to get as close to apples to apples comparison as
66  * possible. Running a select subset of queries against
67  * a webserver normally catches a lot, but it won't give
68  * an accurate picture of how a system will perform
69  * under real requests.
70  * <br>
71  * Created on: Jun 26, 2003
72  *
73  * @version $Id: AccessLogSamplerGui.java,v 1.11 2004/03/05 01:38:02 sebb Exp $
74  */

75 public class AccessLogSamplerGui
76     extends AbstractSamplerGui
77     implements ChangeListener JavaDoc, UnsharedComponent
78 {
79
80     JLabeledTextField parserClassName =
81         new JLabeledTextField(JMeterUtils.getResString("log_parser"));
82     JLabeledTextField generatorClassName =
83         new JLabeledTextField(JMeterUtils.getResString("generator"));
84     JLabeledTextField HOSTNAME =
85         new JLabeledTextField(JMeterUtils.getResString("servername"));
86     JLabeledTextField PORT =
87         new JLabeledTextField(JMeterUtils.getResString("port"));
88     FilePanel logFile =
89         new FilePanel(JMeterUtils.getResString("log_file"), ".txt");
90     private JCheckBox JavaDoc getImages;
91
92     protected int PORTNUMBER = 80;
93     
94     public String JavaDoc DEFAULT_GENERATOR =
95         "org.apache.jmeter.protocol.http.util.accesslog.StandardGenerator";
96     public String JavaDoc DEFAULT_PARSER =
97         "org.apache.jmeter.protocol.http.util.accesslog.TCLogParser";
98     private AccessLogSampler SAMPLER = null;
99
100     /**
101      * This is the font for the note.
102      */

103     Font JavaDoc plainText = new Font JavaDoc("plain", Font.PLAIN, 10);
104
105     JLabel JavaDoc noteMessage =
106         new JLabel JavaDoc(JMeterUtils.getResString("als_message"));
107     JLabel JavaDoc noteMessage2 =
108         new JLabel JavaDoc(JMeterUtils.getResString("als_message2"));
109     JLabel JavaDoc noteMessage3 =
110         new JLabel JavaDoc(JMeterUtils.getResString("als_message3"));
111
112     public AccessLogSamplerGui()
113     {
114         init();
115     }
116
117     /**
118      * @see org.apache.jmeter.gui.JMeterGUIComponent#getStaticLabel()
119      */

120     public String JavaDoc getLabelResource()
121     {
122         return "log_sampler";
123     }
124
125     /**
126      * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
127      */

128     public TestElement createTestElement()
129     {
130         if (SAMPLER == null){
131             SAMPLER = new AccessLogSampler();
132             this.configureTestElement(SAMPLER);
133             SAMPLER.setParserClassName(parserClassName.getText());
134             SAMPLER.setGeneratorClassName(generatorClassName.getText());
135             SAMPLER.setLogFile(logFile.getFilename());
136             SAMPLER.setDomain(HOSTNAME.getText());
137             SAMPLER.setPort(getPortNumber());
138
139         }
140         return SAMPLER;
141     }
142
143     /**
144      * Utility method to parse the string and get a int port
145      * number. If it couldn't parse the string to an integer,
146      * it will return the default port 80.
147      * @return port number
148      */

149     public int getPortNumber(){
150         try {
151             int port = Integer.parseInt(PORT.getText());
152             return port;
153         } catch (NumberFormatException JavaDoc exception){
154                         // since we return 80, there's not point
155
// in printing out the stack trace or
156
// an exception.
157
return 80;
158         }
159     }
160
161     /**
162      * Modifies a given TestElement to mirror the data in the gui components.
163      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
164      */

165     public void modifyTestElement(TestElement s)
166     {
167         SAMPLER = (AccessLogSampler) s;
168         this.configureTestElement(SAMPLER);
169         SAMPLER.setParserClassName(parserClassName.getText());
170         SAMPLER.setGeneratorClassName(generatorClassName.getText());
171         SAMPLER.setLogFile(logFile.getFilename());
172         SAMPLER.setDomain(HOSTNAME.getText());
173         SAMPLER.setPort(getPortNumber());
174         if (getImages.isSelected()){
175             SAMPLER.setImageParser(true);
176         } else {
177             SAMPLER.setImageParser(false);
178         }
179     }
180
181     /**
182      * init() adds soapAction to the mainPanel. The class
183      * reuses logic from SOAPSampler, since it is common.
184      */

185     private void init()
186     {
187         this.setLayout(
188             new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
189
190         // MAIN PANEL
191
JPanel JavaDoc mainPanel = new JPanel JavaDoc();
192         Border JavaDoc margin = new EmptyBorder JavaDoc(10, 10, 5, 10);
193         mainPanel.setBorder(margin);
194         mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
195
196         // TITLE
197
JLabel JavaDoc panelTitleLabel = new JLabel JavaDoc(getStaticLabel());
198         Font JavaDoc curFont = panelTitleLabel.getFont();
199         int curFontSize = curFont.getSize();
200         curFontSize += 4;
201         panelTitleLabel.setFont(
202             new Font JavaDoc(curFont.getFontName(), curFont.getStyle(), curFontSize));
203         mainPanel.add(panelTitleLabel);
204         // NAME
205
mainPanel.add(getNamePanel());
206         mainPanel.add(HOSTNAME);
207         mainPanel.add(PORT);
208
209         mainPanel.add(parserClassName);
210         mainPanel.add(generatorClassName);
211         mainPanel.add(logFile);
212         HOSTNAME.addChangeListener(this);
213         PORT.addChangeListener(this);
214         logFile.addChangeListener(this);
215         parserClassName.addChangeListener(this);
216         generatorClassName.addChangeListener(this);
217
218         // RETRIEVE IMAGES
219
JPanel JavaDoc retrieveImagesPanel = new JPanel JavaDoc();
220         getImages =
221             new JCheckBox JavaDoc(
222                 JMeterUtils.getResString("web_testing_retrieve_images"));
223         retrieveImagesPanel.add(getImages);
224         mainPanel.add(retrieveImagesPanel);
225         mainPanel.add(noteMessage);
226         mainPanel.add(noteMessage2);
227         mainPanel.add(noteMessage3);
228
229         this.add(mainPanel);
230     }
231
232     /**
233      * the implementation loads the URL and the soap
234      * action for the request.
235      */

236     public void configure(TestElement el)
237     {
238         super.configure(el);
239         SAMPLER = (AccessLogSampler) el;
240         if (SAMPLER.getParserClassName().length() > 0){
241             parserClassName.setText(SAMPLER.getParserClassName());
242         } else {
243             parserClassName.setText(this.DEFAULT_PARSER);
244         }
245         if (SAMPLER.getGeneratorClassName().length() > 0){
246             generatorClassName.setText(SAMPLER.getGeneratorClassName());
247         } else {
248             generatorClassName.setText(this.DEFAULT_GENERATOR);
249         }
250         logFile.setFilename(SAMPLER.getLogFile());
251         HOSTNAME.setText(SAMPLER.getDomain());
252         PORT.setText(String.valueOf(SAMPLER.getPort()));
253         getImages.setSelected(SAMPLER.isImageParser());
254
255     }
256     
257     /**
258      * stateChanged implements logic for the text field
259      * and file chooser. When the value in the widget
260      * changes, it will call the corresponding method to
261      * create the parser and initialize the generator.
262      */

263     public void stateChanged(ChangeEvent JavaDoc event)
264     {
265         if (event.getSource() == parserClassName)
266         {
267             SAMPLER.setParserClassName(parserClassName.getText());
268             handleParserEvent();
269         }
270         if (event.getSource() == logFile)
271         {
272             //this.setUpGenerator();
273
}
274         if (event.getSource() == generatorClassName){
275             SAMPLER.setGeneratorClassName(generatorClassName.getText());
276             handleGeneratorEvent();
277         }
278         if (event.getSource() == HOSTNAME){
279         }
280         if (event.getSource() == PORT){
281         }
282     }
283
284     /**
285      * handleParserEvent is used to check the
286      * parser class. If it is not valid, it
287      * should pop up an error message.
288      */

289     public void handleParserEvent(){
290         if(!SAMPLER.checkParser()){
291             // we should pop up a dialog
292
JOptionPane.showConfirmDialog(
293                 this,
294                 JMeterUtils.getResString("log_parser_cnf_msg"),
295                 "Warning",
296                 JOptionPane.OK_CANCEL_OPTION,
297                 JOptionPane.ERROR_MESSAGE);
298         }
299     }
300
301     /**
302      * handleGeneratorEvent is used to check the
303      * generator class. If it is not valid, it
304      * should pop up an error message.
305      */

306     public void handleGeneratorEvent(){
307         if(!SAMPLER.checkGenerator()){
308             // we should pop up a dialog
309
JOptionPane.showConfirmDialog(
310                 this,
311                 JMeterUtils.getResString("generator_cnf_msg"),
312                 "Warning",
313                 JOptionPane.OK_CANCEL_OPTION,
314                 JOptionPane.ERROR_MESSAGE);
315         }
316     }
317
318     /**
319      * Added basic TestCase for AccessLogSamplerGui. It does
320      * the same test at HttpTestSampleGui.java.
321      */

322     public static class Test extends TestCase
323     {
324         AccessLogSamplerGui gui;
325         
326         public Test(String JavaDoc name)
327         {
328             super(name);
329         }
330         
331         public void setUp()
332         {
333             gui = new AccessLogSamplerGui();
334         }
335         
336         public void testCloneSampler() throws Exception JavaDoc
337         {
338             AccessLogSampler sampler = (AccessLogSampler)gui.createTestElement();
339             sampler.addArgument("param","value");
340             AccessLogSampler clonedSampler = (AccessLogSampler)sampler.clone();
341             clonedSampler.setRunningVersion(true);
342             sampler.getArguments().getArgument(0).setValue("new value");
343             assertEquals(
344                 "Sampler didn't clone correctly",
345                 "new value",
346                 sampler.getArguments().getArgument(0).getValue());
347         }
348     }
349
350 }
351
Popular Tags