Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Caliper benchmarks for running on Android devices (#10). #278

Merged
merged 6 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ bin

# Vim
.*.sw[nop]

# Vogar benchmarks
vogar-results/*
21 changes: 21 additions & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ LOCAL_REQUIRED_MODULES := libjavacrypto
LOCAL_JARJAR_RULES := $(LOCAL_PATH)/jarjar-rules.txt
LOCAL_JAVA_LANGUAGE_VERSION := 1.7
include $(BUILD_STATIC_JAVA_LIBRARY)

bundled_benchmark_java_files := $(call all-java-files-under,testing/src/main/java)
bundled_benchmark_java_files := $(foreach j,$(bundled_benchmark_java_files),\
$(if $(findstring testing/src/main/java/libcore/,$(j)),,$(j)))
bundled_benchmark_java_files += $(call all-java-files-under,benchmark-base/src/main/java)
bundled_benchmark_java_files += $(call all-java-files-under,benchmark-caliper/src/main/java)
bundled_benchmark_java_files += $(call all-java-files-under,benchmark-android/src/main/java)

# Make the conscrypt-benchmarks library.
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(bundled_benchmark_java_files)
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JAVA_LIBRARIES := core-oj core-libart junit bouncycastle caliper-api-target
LOCAL_STATIC_JAVA_LIBRARIES := core-tests-support conscrypt-nojarjar
LOCAL_JAVACFLAGS := $(local_javac_flags)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := conscrypt-benchmarks
LOCAL_REQUIRED_MODULES := libjavacrypto
LOCAL_JARJAR_RULES := $(LOCAL_PATH)/jarjar-rules.txt
LOCAL_JAVA_LANGUAGE_VERSION := 1.7
include $(BUILD_STATIC_JAVA_LIBRARY)
endif

# Platform conscrypt crypto JNI library
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3' // jcenter has the latest
classpath libraries.android_tools
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
Expand Down
136 changes: 136 additions & 0 deletions benchmark-android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath libraries.android_tools
}
}

description = 'Conscrypt: Android Benchmarks'

ext {
androidHome = "$System.env.ANDROID_HOME"
androidSdkInstalled = file("$androidHome").exists()
androidVersionCode = 1
androidVersionName = "$version"
androidMinSdkVersion = 24
androidTargetSdkVersion = 25
androidBuildToolsVersion = "25.0.0"
androidBuildToolsDir = "${androidHome}/build-tools/${androidBuildToolsVersion}"
}

if (androidSdkInstalled) {
apply plugin: 'com.android.library'

android {
compileSdkVersion androidTargetSdkVersion
buildToolsVersion androidBuildToolsVersion

compileOptions {
sourceCompatibility androidMinJavaVersion;
targetCompatibility androidMinJavaVersion
}

defaultConfig {
minSdkVersion androidMinSdkVersion
targetSdkVersion androidTargetSdkVersion
versionCode androidVersionCode
versionName androidVersionName
}
lintOptions {
// Some Caliper classes reference packages that don't exist on Android
disable 'InvalidPackage'
}
sourceSets.main {
java {
srcDirs = [
"${rootDir}/benchmark-base/src/main/java",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just make base a dependency rather than including the source?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the benchmark-base subproject, so benchmark-android and benchmark-openjdk both include its source directly. This is the same pattern used with common/, it doesn't get compiled directly but android/ and openjdk/ include its source. It makes it a bit easier because there are circular dependencies between the platform-specific bits and the platform-agnostic bits, so this just lets them get compiled together.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that's fine. FYI the main reason we were including source in common is because we wanted a single resulting jar.

"src/main/java"
]
}
}
}

dependencies {
compile project(':conscrypt-android'),
project(':conscrypt-testing')

compile 'com.google.caliper:caliper:1.0-beta-2'
compile libraries.bouncycastle_provider

}

// This task bundles up everything we're going to send to the device into a single jar.
// We need to include all the Conscrypt code plus the Bouncy Castle jar because the platform
// version of Bouncy Castle is jarjared.
task depsJar(type: Jar, dependsOn: 'assembleRelease') {
archiveName = 'bundled-deps.jar'
from {
configurations.compile.filter {
// Find the jars from our project plus BC
it.name.endsWith(".jar") && (it.path.startsWith("${rootDir}") || it.path.contains('org.bouncycastle'))
}.collect {
zipTree(it)
}
}
from {
// Also include the classes.jar from our Android libraries
['.', "${rootDir}/android"].collect {
zipTree(it + '/build/intermediates/bundles/default/classes.jar')
}
}
// Bouncy Castle signs their jar, which causes our combined jar to fail to verify. Just
// strip out the signature files.
exclude "META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA"
}

task runBenchmarks(dependsOn: depsJar) {
doLast {
// First, determine which ABI the device uses so that we can send the right native lib
new ByteArrayOutputStream().withStream { stream ->
exec {
commandLine = ['adb', 'shell', 'getprop', 'ro.product.cpu.abi']
standardOutput = stream
}
ext.androidDeviceAbi = stream.toString().trim()
ext.androidDevice64Bit = ext.androidDeviceAbi.contains('64')
}
def nativeLibPath = "/system/lib${androidDevice64Bit ? '64' : ''}/libconscrypt_jni.so"
// Send the native library to the device
exec {
executable "${androidHome}/platform-tools/adb"
args 'push'
args "${rootDir}/android/build/intermediates/bundles/default/jni/${androidDeviceAbi}/libconscrypt_jni.so"
args nativeLibPath
}
// Execute the benchmarks
exec {
workingDir "${rootDir}"
environment PATH: "${androidBuildToolsDir}:$System.env.PATH"
environment JACK_JAR: "${androidBuildToolsDir}/jack.jar"

executable 'java'
args '-cp', 'benchmark-android/vogar.jar', 'vogar.Vogar'
args '--classpath', 'benchmark-android/build/libs/bundled-deps.jar'
args '--benchmark'
args '--language=JN'
args '--mode=app_process'
args 'org.conscrypt.CaliperAlpnBenchmark'
args 'org.conscrypt.CaliperEngineHandshakeBenchmark'
args 'org.conscrypt.CaliperEngineWrapBenchmark'
}
// Clean up the native library
exec {
commandLine = ['adb', 'shell', 'rm', '-f', nativeLibPath]
}
}
}
} else {
logger.warn('Android SDK has not been detected. The Android Benchmark module will not be built.')

// Disable all tasks
tasks.collect {
it.enabled = false
}
}
6 changes: 6 additions & 0 deletions benchmark-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.conscrypt.benchmarks.android">

<application/>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2017 The Android Open Source Project
*
* Licensed 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.conscrypt;

import static org.conscrypt.TestUtils.PROTOCOL_TLS_V1_2;
import static org.conscrypt.TestUtils.initClientSslContext;
import static org.conscrypt.TestUtils.initEngine;
import static org.conscrypt.TestUtils.initServerSslContext;

import java.security.KeyStore.PrivateKeyEntry;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import libcore.java.security.TestKeyStore;

/**
* Enumeration of various types of engines for use with engine-based benchmarks.
*/
@SuppressWarnings({"ImmutableEnumChecker", "unused"})
public enum AndroidEngineType implements EngineType {
CONSCRYPT_UNPOOLED {
private final SSLContext clientContext = newConscryptClientContext();
private final SSLContext serverContext = newConscryptServerContext();

@Override
public SSLEngine newClientEngine(String cipher, boolean useAlpn) {
SSLEngine engine = initEngine(clientContext.createSSLEngine(), cipher, true);
if (useAlpn) {
Conscrypt.Engines.setAlpnProtocols(
engine, new String[] {"h2"});
}
return engine;
}

@Override
public SSLEngine newServerEngine(String cipher, boolean useAlpn) {
SSLEngine engine = initEngine(serverContext.createSSLEngine(), cipher, false);
if (useAlpn) {
Conscrypt.Engines.setAlpnProtocols(
engine, new String[] {"h2"});
}
return engine;
}

private SSLContext newContext() {
try {
return SSLContext.getInstance(PROTOCOL_TLS_V1_2, new OpenSSLProvider());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

@Override
public void dispose(SSLEngine engine) {
engine.closeOutbound();
}
};

@Override
public void dispose(SSLEngine engine) {}

private static SSLContext newConscryptClientContext() {
return TestUtils.newClientSslContext(TestUtils.getConscryptProvider());
}

private static SSLContext newConscryptServerContext() {
return TestUtils.newServerSslContext(TestUtils.getConscryptProvider());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2017 The Android Open Source Project
*
* Licensed 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.conscrypt;

import com.google.caliper.BeforeExperiment;
import com.google.caliper.Param;
import org.conscrypt.EngineHandshakeBenchmark.Config;

/**
* Cipher benchmarks. Only runs on AES currently because of the combinatorial
* explosion of the test as it stands.
*/
public class CaliperAlpnBenchmark {
private final CaliperConfig config = new CaliperConfig();

@Param({TestUtils.TEST_CIPHER})
public String a_cipher;

@Param
public BufferType b_buffer;

@Param
public AndroidEngineType c_engine;

private EngineHandshakeBenchmark benchmark;

@BeforeExperiment
public void setUp() throws Exception {
benchmark = new EngineHandshakeBenchmark(config);
}

public void timeHandshake(int reps) throws Exception {
for (int i = 0; i < reps; ++i) {
benchmark.handshake();
}
}

private final class CaliperConfig implements Config {
@Override
public BufferType bufferType() {
return b_buffer;
}

@Override
public EngineType engineType() {
return c_engine;
}

@Override
public String cipher() {
return a_cipher;
}

@Override
public boolean useAlpn() {
return true;
}
}
}
Loading