Author: pmouawad
Date: Wed Feb 17 21:31:24 2016
New Revision: 1730947

URL: http://svn.apache.org/viewvc?rev=1730947&view=rev
Log:
Bug 58099 - Performance : Lazily initialize HttpClient SSL Context to avoid its 
initialization even for HTTP only scenarios
Bugzilla Id: 58099

Added:
    
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/LazySchemeSocketFactory.java
   (with props)
Modified:
    
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
    
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java
    jmeter/trunk/xdocs/changes.xml

Modified: 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java?rev=1730947&r1=1730946&r2=1730947&view=diff
==============================================================================
--- 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
 (original)
+++ 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
 Wed Feb 17 21:31:24 2016
@@ -28,7 +28,6 @@ import java.net.URI;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.nio.charset.Charset;
-import java.security.GeneralSecurityException;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
@@ -78,6 +77,7 @@ import org.apache.http.conn.ClientConnec
 import org.apache.http.conn.ConnectionKeepAliveStrategy;
 import org.apache.http.conn.DnsResolver;
 import org.apache.http.conn.params.ConnRoutePNames;
+import org.apache.http.conn.scheme.PlainSocketFactory;
 import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
 import org.apache.http.entity.ContentType;
@@ -92,7 +92,6 @@ import org.apache.http.impl.client.Abstr
 import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
-import org.apache.http.impl.conn.SchemeRegistryFactory;
 import org.apache.http.impl.conn.SystemDefaultDnsResolver;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.params.BasicHttpParams;
@@ -110,11 +109,9 @@ import org.apache.jmeter.protocol.http.c
 import org.apache.jmeter.protocol.http.control.CookieManager;
 import org.apache.jmeter.protocol.http.control.HeaderManager;
 import org.apache.jmeter.protocol.http.util.EncoderCache;
-import org.apache.jmeter.protocol.http.util.HC4TrustAllSSLSocketFactory;
 import org.apache.jmeter.protocol.http.util.HTTPArgument;
 import org.apache.jmeter.protocol.http.util.HTTPConstants;
 import org.apache.jmeter.protocol.http.util.HTTPFileArg;
-import org.apache.jmeter.protocol.http.util.SlowHC4SSLSocketFactory;
 import org.apache.jmeter.protocol.http.util.SlowHC4SocketFactory;
 import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.services.FileServer;
@@ -197,9 +194,6 @@ public class HTTPHC4Impl extends HTTPHCA
     // Scheme used for slow HTTP sockets. Cannot be set as a default, because 
must be set on an HttpClient instance.
     private static final Scheme SLOW_HTTP;
     
-    // We always want to override the HTTPS scheme, because we want to trust 
all certificates and hosts
-    private static final Scheme HTTPS_SCHEME;
-
     /*
      * Create a set of default parameters from the ones initially created.
      * This allows the defaults to be overridden if necessary from the 
properties file.
@@ -229,24 +223,6 @@ public class HTTPHC4Impl extends HTTPHCA
             SLOW_HTTP = null;
         }
         
-        // We always want to override the HTTPS scheme
-        Scheme https = null;
-        if (CPS_HTTPS > 0) {
-            log.info("Setting up HTTPS SlowProtocol, cps="+CPS_HTTPS);
-            try {
-                https = new Scheme(HTTPConstants.PROTOCOL_HTTPS, 
HTTPConstants.DEFAULT_HTTPS_PORT, new SlowHC4SSLSocketFactory(CPS_HTTPS));
-            } catch (GeneralSecurityException e) {
-                log.warn("Failed to initialise SLOW_HTTPS scheme, 
cps="+CPS_HTTPS, e);
-            }
-        } else {
-            log.info("Setting up HTTPS TrustAll scheme");
-            try {
-                https = new Scheme(HTTPConstants.PROTOCOL_HTTPS, 
HTTPConstants.DEFAULT_HTTPS_PORT, new HC4TrustAllSSLSocketFactory());
-            } catch (GeneralSecurityException e) {
-                log.warn("Failed to initialise HTTPS TrustAll scheme", e);
-            }
-        }
-        HTTPS_SCHEME = https;
         if (localAddress != null){
             DEFAULT_HTTP_PARAMS.setParameter(ConnRoutePNames.LOCAL_ADDRESS, 
localAddress);
         }
@@ -726,7 +702,7 @@ public class HTTPHC4Impl extends HTTPHCA
             if (resolver == null) {
                 resolver = new SystemDefaultDnsResolver();
             }
-            ClientConnectionManager connManager = new 
MeasuringConnectionManager(SchemeRegistryFactory.createDefault(), resolver);
+            ClientConnectionManager connManager = new 
MeasuringConnectionManager(createSchemeRegistry(), resolver);
             
             httpClient = new DefaultHttpClient(connManager, clientParams) {
                 @Override
@@ -751,10 +727,6 @@ public class HTTPHC4Impl extends HTTPHCA
                 schemeRegistry.register(SLOW_HTTP);
             }
 
-            if (HTTPS_SCHEME != null){
-                schemeRegistry.register(HTTPS_SCHEME);
-            }
-
             // Set up proxy details
             if(useProxy) {
 
@@ -793,6 +765,19 @@ public class HTTPHC4Impl extends HTTPHCA
     }
 
     /**
+     * Setup LazySchemeSocketFactory
+     * @see https://bz.apache.org/bugzilla/show_bug.cgi?id=58099
+     */
+    private static SchemeRegistry createSchemeRegistry() {
+        final SchemeRegistry registry = new SchemeRegistry();
+        registry.register(
+                new Scheme("http", 80, 
PlainSocketFactory.getSocketFactory())); //$NON-NLS-1$
+        registry.register(
+                new Scheme("https", 443, new LazySchemeSocketFactory())); 
//$NON-NLS-1$
+        return registry;
+    }
+
+    /**
      * Setup following elements on httpRequest:
      * <ul>
      * <li>ConnRoutePNames.LOCAL_ADDRESS enabling IP-SPOOFING</li>

Added: 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/LazySchemeSocketFactory.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/LazySchemeSocketFactory.java?rev=1730947&view=auto
==============================================================================
--- 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/LazySchemeSocketFactory.java
 (added)
+++ 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/LazySchemeSocketFactory.java
 Wed Feb 17 21:31:24 2016
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+package org.apache.jmeter.protocol.http.sampler;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.security.GeneralSecurityException;
+
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.conn.scheme.SchemeSocketFactory;
+import org.apache.http.conn.ssl.SSLInitializationException;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.params.HttpParams;
+import org.apache.jmeter.protocol.http.util.HC4TrustAllSSLSocketFactory;
+import org.apache.jorphan.logging.LoggingManager;
+import org.apache.log.Logger;
+
+/**
+ * Lazy SchemeSocketFactory that lazily initializes HTTPS Socket Factory
+ * @since 3.0
+ */
+public final class LazySchemeSocketFactory implements SchemeSocketFactory{
+    private static final Logger LOG = LoggingManager.getLoggerForClass();
+    
+    private volatile SchemeSocketFactory adaptee;
+    
+    /**
+     * 
+     */
+    public LazySchemeSocketFactory() {
+        super();
+    }
+    
+    /**
+     * @param params
+     * @return
+     * @throws IOException
+     * @see 
org.apache.http.conn.scheme.SchemeSocketFactory#createSocket(org.apache.http.params.HttpParams)
+     */
+    @Override
+    public Socket createSocket(HttpParams params) throws IOException {
+        checkAndInit();
+        return adaptee.createSocket(params);
+    }
+    
+    /**
+     * @throws SSLInitializationException
+     */
+    private void checkAndInit() throws SSLInitializationException {
+        if(adaptee == null) {
+            synchronized (this) {
+                if(adaptee==null) {
+                    LOG.info("Setting up HTTPS TrustAll Socket Factory");
+                    try {
+                        adaptee = new HC4TrustAllSSLSocketFactory();
+                    } catch (GeneralSecurityException e) {
+                        LOG.warn("Failed to initialise HTTPS 
HC4TrustAllSSLSocketFactory", e);
+                        adaptee = SSLSocketFactory.getSocketFactory();
+                    }
+                }
+            }
+        }
+    }
+    
+    /**
+     * @param sock
+     * @param remoteAddress
+     * @param localAddress
+     * @param params
+     * @return
+     * @throws IOException
+     * @throws UnknownHostException
+     * @throws ConnectTimeoutException
+     * @see 
org.apache.http.conn.scheme.SchemeSocketFactory#connectSocket(java.net.Socket, 
java.net.InetSocketAddress, java.net.InetSocketAddress, 
org.apache.http.params.HttpParams)
+     */
+    @Override
+    public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress,
+            InetSocketAddress localAddress, HttpParams params)
+            throws IOException, UnknownHostException, ConnectTimeoutException {
+        checkAndInit();
+        return adaptee.connectSocket(sock, remoteAddress, localAddress, 
params);
+    }
+    
+    /**
+     * @param sock
+     * @return
+     * @throws IllegalArgumentException
+     * @see 
org.apache.http.conn.scheme.SchemeSocketFactory#isSecure(java.net.Socket)
+     */
+    @Override
+    public boolean isSecure(Socket sock) throws IllegalArgumentException {
+        checkAndInit();
+        return adaptee.isSecure(sock);
+    }
+}

Propchange: 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/LazySchemeSocketFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java?rev=1730947&r1=1730946&r2=1730947&view=diff
==============================================================================
--- 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java
 (original)
+++ 
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java
 Wed Feb 17 21:31:24 2016
@@ -25,8 +25,9 @@ import org.apache.jmeter.util.JsseSSLMan
 
 /**
  * Apache HttpClient protocol factory to generate "slow" SSL sockets for 
emulating dial-up modems
+ * @deprecated Useless since 3.0
  */
-
+@Deprecated
 public class SlowHC4SSLSocketFactory extends HC4TrustAllSSLSocketFactory {
 
     /**

Modified: jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1730947&r1=1730946&r2=1730947&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Wed Feb 17 21:31:24 2016
@@ -109,6 +109,7 @@ Summary
     <li><bug>58923</bug>normalize URIs when downloading embedded 
resources.</li>
     <li><bug>59005</bug>HTTP Sampler : Added WebDAV verb (SEARCH).</li>
     <li><bug>59006</bug>Change Default proxy recording port to 8888 to align 
it with Recording Template. Contributed by Antonio Gomes Rodrigues (ra0077 at 
gmail.com)</li>
+    <li><bug>58099</bug>Performance : Lazily initialize HttpClient SSL Context 
to avoid its initialization even for HTTP only scenarios</li>
 </ul>
 
 <h3>Other samplers</h3>


Reply via email to