Session manager performance
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 16 Nov 2010 17:48:07 +0000 (17:48 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 16 Nov 2010 17:48:07 +0000 (17:48 +0000)
Big performance improvement for Windows. Don't try to create randomIS on every single session creation call

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1035709 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/session/ManagerBase.java
test/org/apache/catalina/session/Benchmarks.java

index 42cc409..d9fc7a5 100644 (file)
@@ -73,8 +73,9 @@ public abstract class ManagerBase extends LifecycleMBeanBase
 
     // ----------------------------------------------------- Instance Variables
 
-    protected DataInputStream randomIS=null;
-    protected String devRandomSource="/dev/urandom";
+    protected DataInputStream randomIS = null;
+    protected String devRandomSource = "/dev/urandom";
+    protected boolean devRandomSourceIsValid = true;
 
     /**
      * The default message digest algorithm to use if we cannot use
@@ -244,9 +245,13 @@ public abstract class ManagerBase extends LifecycleMBeanBase
         
         @Override
         public DataInputStream run(){
+            devRandomSourceIsValid = true;
             try {
                 File f=new File( devRandomSource );
-                if( ! f.exists() ) return null;
+                if( ! f.exists() ) {
+                    devRandomSourceIsValid = false;
+                    return null;
+                }
                 randomIS= new DataInputStream( new FileInputStream(f));
                 randomIS.readLong();
                 if( log.isDebugEnabled() )
@@ -261,7 +266,7 @@ public abstract class ManagerBase extends LifecycleMBeanBase
                         log.warn("Failed to close randomIS.");
                     }
                 }
-                devRandomSource = null;
+                devRandomSourceIsValid = false;
                 randomIS=null;
                 return null;
             }            
@@ -554,6 +559,8 @@ public abstract class ManagerBase extends LifecycleMBeanBase
      *  - so use it if available.
      */
     public void setRandomFile( String s ) {
+        // Assume the source is valid until proved otherwise
+        devRandomSourceIsValid = true;
         // as a hack, you can use a static file - and generate the same
         // session ids ( good for strange debugging )
         if (Globals.IS_SECURITY_ENABLED){
@@ -562,7 +569,10 @@ public abstract class ManagerBase extends LifecycleMBeanBase
             try{
                 devRandomSource=s;
                 File f=new File( devRandomSource );
-                if( ! f.exists() ) return;
+                if (!f.exists()) {
+                    devRandomSourceIsValid = false;
+                    return;
+                }
                 randomIS= new DataInputStream( new FileInputStream(f));
                 randomIS.readLong();
                 if( log.isDebugEnabled() )
@@ -576,7 +586,7 @@ public abstract class ManagerBase extends LifecycleMBeanBase
                         log.warn("Failed to close randomIS.");
                     }
                 }
-                devRandomSource = null;
+                devRandomSourceIsValid = false;
                 randomIS=null;
             }
         }
@@ -951,7 +961,7 @@ public abstract class ManagerBase extends LifecycleMBeanBase
 
     protected void getRandomBytes(byte bytes[]) {
         // Generate a byte array containing a session identifier
-        if (devRandomSource != null && randomIS == null) {
+        if (devRandomSourceIsValid && randomIS == null) {
             setRandomFile(devRandomSource);
         }
         if (randomIS != null) {
@@ -965,7 +975,7 @@ public abstract class ManagerBase extends LifecycleMBeanBase
             } catch (Exception ex) {
                 // Ignore
             }
-            devRandomSource = null;
+            devRandomSourceIsValid = false;
             
             try {
                 randomIS.close();
index 17019cc..51650cd 100644 (file)
@@ -108,10 +108,10 @@ public class Benchmarks extends TestCase {
     
     /*
      * Results on markt's 4-core dev box
-     *  1 thread  -  ~2,300ms
-     *  2 threads -  ~4,600ms
-     *  4 threads - ~12,300ms
-     * 16 threads - ~51,000ms
+     *  1 thread  -  ~860ms
+     *  2 threads -  ~800ms
+     *  4 threads - ~1,600ms
+     * 16 threads - ~6,900ms
      */
     public void testManagerBaseCreateSession() {
         doTestManagerBaseCreateSession(1, 100000);