1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.constructs.web.filter;
18  
19  import com.meterware.httpunit.WebConversation;
20  import com.meterware.httpunit.WebResponse;
21  import net.sf.ehcache.StopWatch;
22  import net.sf.ehcache.constructs.web.AbstractWebTest;
23  import org.apache.commons.httpclient.HttpClient;
24  import org.apache.commons.httpclient.HttpMethod;
25  import org.apache.commons.httpclient.methods.GetMethod;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import java.io.IOException;
30  
31  /**
32   * @author Greg Luck
33   * @version $Id: SpeedTest.java 512 2007-07-10 09:18:45Z gregluck $
34   */
35  public class SpeedTest extends AbstractWebTest {
36      private static final Log LOG = LogFactory.getLog(SpeedTest.class.getName());
37  
38      /**
39       * Time to get 200 Cached Pages
40       * StopWatch time: 947ms
41       */
42      public void testSpeedHttpClientNotCached() throws IOException {
43          StopWatch stopWatch = new StopWatch();
44          String url = "http://localhost:9080/Login.jsp";
45          HttpClient httpClient = new HttpClient();
46          HttpMethod httpMethod = new GetMethod(url);
47          stopWatch.getElapsedTime();
48          for (int i = 0; i < 200; i++) {
49              httpClient.executeMethod(httpMethod);
50              httpMethod.getResponseBodyAsStream();
51          }
52          long time = stopWatch.getElapsedTime();
53          LOG.info("Time for 200 uncached page requests: " + time);
54      }
55  
56      /**
57       * Time to get 200 Cached Pages
58       * StopWatch time: 1021ms
59       */
60      public void testSpeedHttpClientCached() throws IOException {
61          StopWatch stopWatch = new StopWatch();
62          String url = "http://localhost:9080/CachedPage.jsp";
63          HttpClient httpClient = new HttpClient();
64          HttpMethod httpMethod = new GetMethod(url);
65          stopWatch.getElapsedTime();
66          for (int i = 0; i < 200; i++) {
67              httpClient.executeMethod(httpMethod);
68              httpMethod.getResponseBodyAsStream();
69          }
70          long time = stopWatch.getElapsedTime();
71          LOG.info("Time for 200 cached page requests: " + time);
72      }
73  
74      /**
75       * StopWatch time: 2251ms
76       */
77      public void testSpeedNoDom() throws Exception {
78          StopWatch stopWatch = new StopWatch();
79          final WebConversation conversation = createWebConversation(true);
80  
81          String requestUrl = "http://localhost:9080/CachedPage.jsp";
82          stopWatch.getElapsedTime();
83          for (int i = 0; i < 200; i++) {
84              WebResponse response = conversation.getResponse(requestUrl);
85              response.getText().indexOf("timestamp");
86          }
87          long time = stopWatch.getElapsedTime();
88          LOG.info("Time for 200 cached page requests: " + time);
89  
90      }
91  
92      /**
93       * Test time: 10.2s, 11.5    4.2 without JavaScript library
94       * StopWatch time: 2686ms without JavaScript library
95       * This test gets the CacheLogin twice so we need half as many
96       */
97      public void testSpeedDom() throws Exception {
98          StopWatch stopWatch = new StopWatch();
99          CachingFilterTest cachingFilterTest = new CachingFilterTest();
100         cachingFilterTest.testCachedPageIsCached();
101         stopWatch.getElapsedTime();
102         for (int i = 0; i < 100; i++) {
103             cachingFilterTest.testCachedPageIsCached();
104         }
105         long time = stopWatch.getElapsedTime();
106         LOG.info("Time for 200 cached page requests: " + time);
107 
108     }
109 
110 
111     /**
112      * This is the last test run in the suite. It tells clover to flush.
113      */
114     public void testFlushClover() {
115         ///CLOVER:FLUSH
116     }
117 }