1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.jmeter

File HTTPPerformanceTest.java

 
 

Code metrics

4
62
8
1
194
136
11
0.18
7.75
8
1.38

Classes

Class Line # Actions
HTTPPerformanceTest 55 62 0% 11 69
0.0675675656.8%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.jmeter;
21   
22    import java.io.File;
23    import java.io.FileNotFoundException;
24    import java.io.IOException;
25    import java.io.UnsupportedEncodingException;
26    import java.net.URLEncoder;
27    import java.util.ArrayList;
28    import java.util.Collection;
29    import java.util.List;
30    import java.util.Locale;
31    import java.util.regex.Pattern;
32   
33    import org.apache.commons.io.FileUtils;
34    import org.apache.commons.io.IOUtils;
35    import org.apache.jmeter.control.LoopController;
36    import org.apache.jmeter.engine.StandardJMeterEngine;
37    import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
38    import org.apache.jmeter.reporters.ResultCollector;
39    import org.apache.jmeter.samplers.SampleSaveConfiguration;
40    import org.apache.jmeter.testelement.TestPlan;
41    import org.apache.jmeter.threads.ThreadGroup;
42    import org.apache.jmeter.util.JMeterUtils;
43    import org.apache.jorphan.collections.HashTree;
44    import org.junit.BeforeClass;
45    import org.junit.Test;
46    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer;
47    import org.xwiki.model.reference.DocumentReference;
48    import org.xwiki.model.reference.LocalDocumentReference;
49    import org.xwiki.model.reference.WikiReference;
50    import org.xwiki.test.integration.XWikiExecutor;
51    import org.xwiki.xar.XarEntry;
52    import org.xwiki.xar.XarException;
53    import org.xwiki.xar.XarPackage;
54   
 
55    public class HTTPPerformanceTest
56    {
57    private static final DefaultStringEntityReferenceSerializer SERIALIZER =
58    new DefaultStringEntityReferenceSerializer();
59   
 
60  0 toggle protected static List<DocumentReference> readXarContents(String fileName, String patternFilter) throws Exception
61    {
62  0 Collection<XarEntry> entries = XarPackage.getEntries(new File(fileName));
63   
64  0 List<DocumentReference> result = new ArrayList<DocumentReference>(entries.size());
65   
66  0 WikiReference wikiReference = new WikiReference("xwiki");
67   
68  0 for (XarEntry entry : entries) {
69  0 result.add(new DocumentReference(entry, wikiReference));
70    }
71   
72  0 return result;
73    }
74   
 
75  0 toggle private static void addXarFiles(List<HTTPSampler> samplers) throws UnsupportedEncodingException, XarException,
76    IOException
77    {
78  0 String path = System.getProperty("localRepository") + "/" + System.getProperty("pathToXWikiXar");
79  0 String patternFilter = System.getProperty("documentsToTest");
80   
81  0 Pattern pattern = patternFilter == null ? null : Pattern.compile(patternFilter);
82   
83  0 for (XarEntry xarEntry : XarPackage.getEntries(new File(path))) {
84  0 if (pattern == null || pattern.matcher(SERIALIZER.serialize(xarEntry)).matches()) {
85  0 Test failure here samplers.add(createSample(xarEntry, "get"));
86  0 samplers.add(createSample(xarEntry, "view"));
87    }
88    }
89    }
90   
 
91  0 toggle private static HTTPSampler createSample(LocalDocumentReference documentReference, String action)
92    throws UnsupportedEncodingException
93    {
94  0 Test failure here return createSample(SERIALIZER.serialize(documentReference) + " (" + action + ")",
95    "/xwiki/bin/" + action + "/" + URLEncoder.encode(documentReference.getParent().getName(), "UTF8") + "/"
96    + URLEncoder.encode(documentReference.getName(), "UTF8"));
97    }
98   
 
99  1 toggle @BeforeClass
100    public static void before() throws IOException
101    {
102  1 FileUtils.writeByteArrayToFile(new File("target/jmeter/home/bin/httpclient.parameters"),
103    IOUtils.toByteArray(HTTPPerformanceTest.class.getResource("/jmeterbin/httpclient.parameters")));
104  1 FileUtils.writeByteArrayToFile(new File("target/jmeter/home/bin/jmeter.properties"),
105    IOUtils.toByteArray(HTTPPerformanceTest.class.getResource("/jmeterbin/jmeter.properties")));
106  1 FileUtils.writeByteArrayToFile(new File("target/jmeter/home/bin/saveservice.properties"),
107    IOUtils.toByteArray(HTTPPerformanceTest.class.getResource("/jmeterbin/saveservice.properties")));
108  1 FileUtils.writeByteArrayToFile(new File("target/jmeter/home/bin/upgrade.properties"),
109    IOUtils.toByteArray(HTTPPerformanceTest.class.getResource("/jmeterbin/upgrade.properties")));
110    }
111   
 
112  0 toggle private static HTTPSampler createSample(String name, String path)
113    {
114  0 HTTPSampler httpSampler = new HTTPSampler();
115   
116  0 httpSampler.setDomain("localhost");
117  0 httpSampler.setPort(Integer.valueOf(XWikiExecutor.DEFAULT_PORT));
118  0 httpSampler.setMethod("GET");
119   
120  0 httpSampler.setName(path);
121  0 httpSampler.setPath(path);
122   
123  0 return httpSampler;
124    }
125   
 
126  0 toggle public void execute(List<HTTPSampler> samplers)
127    {
128  0 execute(samplers, null, null);
129    }
130   
 
131  0 toggle public void execute(List<HTTPSampler> samplers, String user, String password)
132    {
133    // jmeter.properties
134  0 JMeterUtils.loadJMeterProperties("target/jmeter/home/bin/saveservice.properties");
135  0 JMeterUtils.setLocale(Locale.ENGLISH);
136  0 JMeterUtils.setJMeterHome("target/jmeter/home");
137   
138    // Result collector
139  0 ResultCollector resultCollector = new ResultCollector();
140  0 resultCollector.setFilename("target/jmeter/report.jtl");
141  0 SampleSaveConfiguration saveConfiguration = new SampleSaveConfiguration();
142  0 saveConfiguration.setAsXml(true);
143  0 saveConfiguration.setCode(true);
144  0 saveConfiguration.setLatency(true);
145  0 saveConfiguration.setTime(true);
146  0 saveConfiguration.setTimestamp(true);
147  0 resultCollector.setSaveConfig(saveConfiguration);
148   
149    // Thread Group
150  0 ThreadGroup threadGroup = new ThreadGroup();
151  0 threadGroup.setName("xwiki");
152  0 threadGroup.setNumThreads(1);
153  0 threadGroup.setRampUp(1);
154  0 LoopController loopCtrl = new LoopController();
155  0 loopCtrl.setLoops(5);
156  0 loopCtrl.setFirst(true);
157  0 threadGroup.setSamplerController(loopCtrl);
158   
159  0 HashTree threadGroupTree = new HashTree();
160  0 threadGroupTree.add(samplers);
161   
162    // Test plan
163  0 TestPlan testPlan = new TestPlan("ping");
164   
165  0 HashTree testPlanTree = new HashTree();
166  0 testPlanTree.add(threadGroup, threadGroupTree);
167  0 testPlanTree.add(resultCollector);
168   
169  0 HashTree hashTree = new HashTree();
170  0 hashTree.add(testPlan, testPlanTree);
171   
172    // Engine
173  0 StandardJMeterEngine jm = new StandardJMeterEngine("localhost");
174   
175  0 jm.configure(hashTree);
176   
177  0 jm.run();
178    }
179   
180    // Tests
181   
 
182  0 toggle @Test
183    public void guest() throws FileNotFoundException, Exception
184    {
185  0 List<HTTPSampler> samplers = new ArrayList<HTTPSampler>();
186   
187  0 samplers.add(createSample("root", "/xwiki/"));
188  0 samplers.add(createSample("Main.WebHome (edit)", "/xwiki/bin/edit/Main/WebHome"));
189   
190  0 Test failure here addXarFiles(samplers);
191   
192  0 execute(samplers);
193    }
194    }