FileDocCategorySizeDatePackage
SSLPerformanceTest.javaAPI DocAndroid 1.5 API12880Wed May 06 22:42:02 BST 2009android.core

SSLPerformanceTest

public class SSLPerformanceTest extends android.test.AndroidTestCase

Fields Summary
static final byte[]
SESSION_DATA
static final File
dataDir
static final File
filesDir
static final File
dbDir
static final String
CACHE_DIR
static final int
ITERATIONS
Constructors Summary
Methods Summary
private voidcloseDatabase()

        if (DatabaseSessionCache.sDefaultDatabaseHelper != null) {
            DatabaseSessionCache.sDefaultDatabaseHelper.close();
        }
        DatabaseSessionCache.sDefaultDatabaseHelper = null;
        DatabaseSessionCache.sHookInitializationDone = false;
        DatabaseSessionCache.mNeedsCacheLoad = true;
    
private voidcloseDirectoryCache()

        try {
            Method reset = FileClientSessionCache.class
                    .getDeclaredMethod("reset");
            reset.setAccessible(true);
            reset.invoke(null);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    
private voiddeleteDatabase()

        closeDatabase();
        if (!new File(dbDir, "ssl_sessions.db").delete()) {
            System.err.println("Failed to delete database.");
        }
    
private voiddeleteDirectory()

        closeDirectoryCache();

        File dir = getCacheDirectory();
        if (!dir.exists()) {
            return;
        }
        for (File file : dir.listFiles()) {
            file.delete();
        }
        if (!dir.delete()) {
            System.err.println("Failed to delete directory.");
        }
    
private java.io.FilegetCacheDirectory()

        return new File(getContext().getFilesDir(), CACHE_DIR);
    
private voidgetVerisignDotCom(org.apache.harmony.xnet.provider.jsse.SSLContextImpl sslContext)

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https",
                new SSLSocketFactory(sslContext.engineGetSocketFactory()),
                443));

        ClientConnectionManager manager =
                new SingleClientConnManager(null, schemeRegistry);

        new DefaultHttpClient(manager, null).execute(
                new HttpGet("https://www.verisign.com"),
                new ResponseHandler<Object>() {
                    public Object handleResponse(HttpResponse response)
                            throws ClientProtocolException, IOException {
                        return null;
                    }
                });
    
private voidputSessionsIn(org.apache.harmony.xnet.provider.jsse.SSLClientSessionCache cache)

        for (int i = 0; i < 10; i++) {
            cache.putSessionData(new FakeSession("host" + i), SESSION_DATA);
        }
    
public voidtestCreateNewEmptyDatabase()


       
        deleteDatabase();

        Stopwatch stopwatch = new Stopwatch();

        DatabaseSessionCache cache = new DatabaseSessionCache(getContext());
        cache.getSessionData("crazybob.org", 443);

        stopwatch.stop();
    
public voidtestCreateNewEmptyDirectory()

        deleteDirectory();

        Stopwatch stopwatch = new Stopwatch();

        SSLClientSessionCache cache = FileClientSessionCache.usingDirectory(
                getCacheDirectory());
        cache.getSessionData("crazybob.org", 443);

        stopwatch.stop();
    
public voidtestEngineInit()

        Stopwatch stopwatch = new Stopwatch();

        new SSLContextImpl().engineInit(null, null, null);

        stopwatch.stop();
    
public voidtestGetSessionFromDatabase()

        deleteDatabase();

        DatabaseSessionCache cache = new DatabaseSessionCache(getContext());
        cache.putSessionData(new FakeSession("foo"), SESSION_DATA);
        closeDatabase();

        cache = new DatabaseSessionCache(getContext());
        cache.getSessionData("crazybob.org", 443);

        Stopwatch stopwatch = new Stopwatch();

        byte[] sessionData = cache.getSessionData("foo", 443);

        stopwatch.stop();

        assertTrue(Arrays.equals(SESSION_DATA, sessionData));
    
public voidtestGetSessionFromDirectory()

        deleteDirectory();

        SSLClientSessionCache cache = FileClientSessionCache.usingDirectory(
                getCacheDirectory());
        cache.putSessionData(new FakeSession("foo"), SESSION_DATA);
        closeDirectoryCache();

        cache = FileClientSessionCache.usingDirectory(
                getCacheDirectory());
        cache.getSessionData("crazybob.org", 443);

        Stopwatch stopwatch = new Stopwatch();

        byte[] sessionData = cache.getSessionData("foo", 443);

        stopwatch.stop();
        
        assertTrue(Arrays.equals(SESSION_DATA, sessionData));
    
public voidtestOpenDatabaseWith10Sessions()

        deleteDatabase();

        DatabaseSessionCache cache = new DatabaseSessionCache(getContext());
        putSessionsIn(cache);
        closeDatabase();

        System.err.println("Size of ssl_sessions.db w/ 10 sessions: "
                + new File(dbDir, "ssl_sessions.db").length());

        Stopwatch stopwatch = new Stopwatch();

        cache = new DatabaseSessionCache(getContext());
        cache.getSessionData("crazybob.org", 443);

        stopwatch.stop();
    
public voidtestOpenDirectoryWith10Sessions()

        deleteDirectory();

        SSLClientSessionCache cache = FileClientSessionCache.usingDirectory(
                getCacheDirectory());
        putSessionsIn(cache);
        closeDirectoryCache();

        Stopwatch stopwatch = new Stopwatch();

        cache = FileClientSessionCache.usingDirectory(
                getCacheDirectory());
        cache.getSessionData("crazybob.org", 443);

        stopwatch.stop();
    
public voidtestPutSessionIntoDatabase()

        deleteDatabase();

        DatabaseSessionCache cache = new DatabaseSessionCache(getContext());
        cache.getSessionData("crazybob.org", 443);

        Stopwatch stopwatch = new Stopwatch();

        cache.putSessionData(new FakeSession("foo"), SESSION_DATA);

        stopwatch.stop();
    
public voidtestPutSessionIntoDirectory()

        deleteDirectory();

        SSLClientSessionCache cache = FileClientSessionCache.usingDirectory(
                getCacheDirectory());
        cache.getSessionData("crazybob.org", 443);

        Stopwatch stopwatch = new Stopwatch();

        cache.putSessionData(new FakeSession("foo"), SESSION_DATA);

        stopwatch.stop();
    
public voidtestWebRequestWithFileCache()

        deleteDirectory();

        SSLContextImpl sslContext = new SSLContextImpl();
        sslContext.engineInit(null, null, null,
                FileClientSessionCache.usingDirectory(getCacheDirectory()),
                null);

        // Make sure www.google.com is in the cache.
        getVerisignDotCom(sslContext);

        // Re-initialize so we hit the file cache.
        sslContext.engineInit(null, null, null,
                FileClientSessionCache.usingDirectory(getCacheDirectory()),
                null);

        Stopwatch stopwatch = new Stopwatch();

        getVerisignDotCom(sslContext);

        stopwatch.stop();
    
public voidtestWebRequestWithInMemoryCache()

        deleteDirectory();

        SSLContextImpl sslContext = new SSLContextImpl();
        sslContext.engineInit(null, null, null);

        // Make sure www.google.com is in the cache.
        getVerisignDotCom(sslContext);

        Stopwatch stopwatch = new Stopwatch();

        getVerisignDotCom(sslContext);

        stopwatch.stop();
    
public voidtestWebRequestWithoutCache()

        SSLContextImpl sslContext = new SSLContextImpl();
        sslContext.engineInit(null, null, null);

        Stopwatch stopwatch = new Stopwatch();

        getVerisignDotCom(sslContext);

        stopwatch.stop();