1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.test.jmeter

File HTTPPerformanceTest.java

 

Code metrics

4
62
8
1
195
137
11
0.18
7.75
8
1.38

Classes

Class Line # Actions
HTTPPerformanceTest 56 62 0% 11 9
0.878378487.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.internal.reference.DefaultSymbolScheme;
48    import org.xwiki.model.reference.DocumentReference;
49    import org.xwiki.model.reference.LocalDocumentReference;
50    import org.xwiki.model.reference.WikiReference;
51    import org.xwiki.test.integration.XWikiExecutor;
52    import org.xwiki.xar.XarEntry;
53    import org.xwiki.xar.XarException;
54    import org.xwiki.xar.XarPackage;
55   
 
56    public class HTTPPerformanceTest
57    {
58    private static final DefaultStringEntityReferenceSerializer SERIALIZER =
59    new DefaultStringEntityReferenceSerializer(new DefaultSymbolScheme());
60   
 
61  0 toggle protected static List<DocumentReference> readXarContents(String fileName, String patternFilter) throws Exception
62    {
63  0 Collection<XarEntry> entries = XarPackage.getEntries(new File(fileName));
64   
65  0 List<DocumentReference> result = new ArrayList<DocumentReference>(entries.size());
66   
67  0 WikiReference wikiReference = new WikiReference("xwiki");
68   
69  0 for (XarEntry entry : entries) {
70  0 result.add(new DocumentReference(entry, wikiReference));
71    }
72   
73  0 return result;
74    }
75   
 
76  1 toggle private static void addXarFiles(List<HTTPSampler> samplers) throws UnsupportedEncodingException, XarException,
77    IOException
78    {
79  1 String path = System.getProperty("pathToDocuments");
80  1 String patternFilter = System.getProperty("documentsToTest");
81   
82  1 Pattern pattern = patternFilter == null ? null : Pattern.compile(patternFilter);
83   
84  1 for (XarEntry xarEntry : XarPackage.getEntries(new File(path))) {
85  919 if (pattern == null || pattern.matcher(SERIALIZER.serialize(xarEntry)).matches()) {
86  919 samplers.add(createSample(xarEntry, "get"));
87  919 samplers.add(createSample(xarEntry, "view"));
88    }
89    }
90    }
91   
 
92  1838 toggle private static HTTPSampler createSample(LocalDocumentReference documentReference, String action)
93    throws UnsupportedEncodingException
94    {
95  1838 return createSample(SERIALIZER.serialize(documentReference) + " (" + action + ")",
96    "/xwiki/bin/" + action + "/" + URLEncoder.encode(documentReference.getParent().getName(), "UTF8") + "/"
97    + URLEncoder.encode(documentReference.getName(), "UTF8"));
98    }
99   
 
100  1 toggle @BeforeClass
101    public static void before() throws IOException
102    {
103  1 FileUtils.writeByteArrayToFile(new File("target/jmeter/home/bin/httpclient.parameters"),
104    IOUtils.toByteArray(HTTPPerformanceTest.class.getResource("/jmeterbin/httpclient.parameters")));
105  1 FileUtils.writeByteArrayToFile(new File("target/jmeter/home/bin/jmeter.properties"),
106    IOUtils.toByteArray(HTTPPerformanceTest.class.getResource("/jmeterbin/jmeter.properties")));
107  1 FileUtils.writeByteArrayToFile(new File("target/jmeter/home/bin/saveservice.properties"),
108    IOUtils.toByteArray(HTTPPerformanceTest.class.getResource("/jmeterbin/saveservice.properties")));
109  1 FileUtils.writeByteArrayToFile(new File("target/jmeter/home/bin/upgrade.properties"),
110    IOUtils.toByteArray(HTTPPerformanceTest.class.getResource("/jmeterbin/upgrade.properties")));
111    }
112   
 
113  1840 toggle private static HTTPSampler createSample(String name, String path)
114    {
115  1840 HTTPSampler httpSampler = new HTTPSampler();
116   
117  1840 httpSampler.setDomain("localhost");
118  1840 httpSampler.setPort(Integer.valueOf(XWikiExecutor.DEFAULT_PORT));
119  1840 httpSampler.setMethod("GET");
120   
121  1840 httpSampler.setName(path);
122  1840 httpSampler.setPath(path);
123   
124  1840 return httpSampler;
125    }
126   
 
127  1 toggle public void execute(List<HTTPSampler> samplers)
128    {
129  1 execute(samplers, null, null);
130    }
131   
 
132  1 toggle public void execute(List<HTTPSampler> samplers, String user, String password)
133    {
134    // jmeter.properties
135  1 JMeterUtils.loadJMeterProperties("target/jmeter/home/bin/saveservice.properties");
136  1 JMeterUtils.setLocale(Locale.ENGLISH);
137  1 JMeterUtils.setJMeterHome("target/jmeter/home");
138   
139    // Result collector
140  1 ResultCollector resultCollector = new ResultCollector();
141  1 resultCollector.setFilename("target/jmeter/report.jtl");
142  1 SampleSaveConfiguration saveConfiguration = new SampleSaveConfiguration();
143  1 saveConfiguration.setAsXml(true);
144  1 saveConfiguration.setCode(true);
145  1 saveConfiguration.setLatency(true);
146  1 saveConfiguration.setTime(true);
147  1 saveConfiguration.setTimestamp(true);
148  1 resultCollector.setSaveConfig(saveConfiguration);
149   
150    // Thread Group
151  1 ThreadGroup threadGroup = new ThreadGroup();
152  1 threadGroup.setName("xwiki");
153  1 threadGroup.setNumThreads(1);
154  1 threadGroup.setRampUp(1);
155  1 LoopController loopCtrl = new LoopController();
156  1 loopCtrl.setLoops(5);
157  1 loopCtrl.setFirst(true);
158  1 threadGroup.setSamplerController(loopCtrl);
159   
160  1 HashTree threadGroupTree = new HashTree();
161  1 threadGroupTree.add(samplers);
162   
163    // Test plan
164  1 TestPlan testPlan = new TestPlan("ping");
165   
166  1 HashTree testPlanTree = new HashTree();
167  1 testPlanTree.add(threadGroup, threadGroupTree);
168  1 testPlanTree.add(resultCollector);
169   
170  1 HashTree hashTree = new HashTree();
171  1 hashTree.add(testPlan, testPlanTree);
172   
173    // Engine
174  1 StandardJMeterEngine jm = new StandardJMeterEngine("localhost");
175   
176  1 jm.configure(hashTree);
177   
178  1 jm.run();
179    }
180   
181    // Tests
182   
 
183  1 toggle @Test
184    public void guest() throws FileNotFoundException, Exception
185    {
186  1 List<HTTPSampler> samplers = new ArrayList<HTTPSampler>();
187   
188  1 samplers.add(createSample("root", "/xwiki/"));
189  1 samplers.add(createSample("Main.WebHome (edit)", "/xwiki/bin/edit/Main/WebHome"));
190   
191  1 addXarFiles(samplers);
192   
193  1 execute(samplers);
194    }
195    }