Commit 92111cc9 authored by akwizgran's avatar akwizgran

Measure upload and download throughput.

parents
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
Interferometer
\ No newline at end of file
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<codeStyleSettings language="XML">
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:android</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:id</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>style</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>ANDROID_ATTRIBUTE_ORDER</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>.*</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<compositeConfiguration>
<compositeBuild compositeDefinitionSource="SCRIPT" />
</compositeConfiguration>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="resolveModulePerSourceSet" value="false" />
<option name="testRunner" value="PLATFORM" />
</GradleProjectSettings>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeSettings">
<configurations>
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
<option name="id" value="Android" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "org.briarproject.interferometer"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
}
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.briarproject.interferometer">
<uses-feature
android:name="android.hardware.bluetooth"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
\ No newline at end of file
package org.briarproject.interferometer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.briarproject.interferometer.MainViewModel.ConnectionState;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DOWNLOADING;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.UPLOADING;
public class MainActivity extends AppCompatActivity {
private MainViewModel viewModel;
private EditText serverAddressEditText;
private TextView throughputTextView;
private String throughputFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
serverAddressEditText = findViewById(R.id.server_address_edit_text);
throughputTextView = findViewById(R.id.throughput_text_view);
throughputFormat = getString(R.string.throughput_format);
showThroughput(0);
Button uploadButton = findViewById(R.id.upload_button);
viewModel.getConnectionState().observe(this, state -> {
if (state == UPLOADING) uploadButton.setText(R.string.stop_upload);
else uploadButton.setText(R.string.upload);
uploadButton.setEnabled(state != DOWNLOADING);
});
Button downloadButton = findViewById(R.id.download_button);
viewModel.getConnectionState().observe(this, state -> {
if (state == DOWNLOADING) downloadButton.setText(R.string.stop_download);
else downloadButton.setText(R.string.download);
downloadButton.setEnabled(state != UPLOADING);
});
viewModel.getThroughput().observe(this, this::showThroughput);
}
private void showThroughput(long throughput) {
String text = String.format(throughputFormat, throughput);
throughputTextView.setText(text);
}
public void onUploadClick(View view) {
ConnectionState state = viewModel.getConnectionState().getValue();
if (state == UPLOADING) viewModel.disconnect();
else viewModel.startUpload(serverAddressEditText.getText().toString());
}
public void onDownloadClick(View view) {
ConnectionState state = viewModel.getConnectionState().getValue();
if (state == DOWNLOADING) viewModel.disconnect();
else viewModel.startDownload(serverAddressEditText.getText().toString());
}
}
package org.briarproject.interferometer;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DISCONNECTED;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DOWNLOADING;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.UPLOADING;
public class MainViewModel extends ViewModel {
enum ConnectionState {DISCONNECTED, DOWNLOADING, UPLOADING}
private static final byte CONNECTION_TYPE_UPLOAD = 0;
private static final byte CONNECTION_TYPE_DOWNLOAD = 1;
private static final int BUFFER_SIZE = 4096;
private static final int MS_PER_UPDATE = 250;
private static final int BYTES_TO_TRANSFER = 100_000_000;
private static final String TAG = "Interferometer";
private final ExecutorService executorService = Executors.newCachedThreadPool();
private final MutableLiveData<ConnectionState> connectionState = new MutableLiveData<>();
private final MutableLiveData<Long> throughput = new MutableLiveData<>();
@Nullable
private Socket socket = null;
LiveData<ConnectionState> getConnectionState() {
return connectionState;
}
LiveData<Long> getThroughput() {
return throughput;
}
@UiThread
void startUpload(String serverAddress) {
closeSocket();
HostPort hostPort = parseServerAddress(serverAddress);
if (hostPort == null) return;
socket = new Socket();
executorService.execute(() -> upload(socket, hostPort));
}
@UiThread
void startDownload(String serverAddress) {
closeSocket();
HostPort hostPort = parseServerAddress(serverAddress);
if (hostPort == null) return;
socket = new Socket();
executorService.execute(() -> download(socket, hostPort));
}
@UiThread
void disconnect() {
closeSocket();
}
@UiThread
@Override
protected void onCleared() {
executorService.shutdown();
closeSocket();
}
@UiThread
private void closeSocket() {
if (socket != null) {
connectionState.setValue(DISCONNECTED);
throughput.setValue(0L);
tryToClose(socket);
socket = null;
}
}
private void tryToClose(Socket s) {
try {
s.close();
} catch (IOException e) {
// Ignored
}
}
@Nullable
private HostPort parseServerAddress(String s) {
int index = s.indexOf(":");
if (index < 1 || index == s.length() - 1) return null;
String host = s.substring(0, index);
try {
int port = Integer.parseInt(s.substring(index + 1));
return new HostPort(host, port);
} catch (NumberFormatException e) {
return null;
}
}
private void upload(Socket s, HostPort hostPort) {
connectionState.postValue(UPLOADING);
try {
Log.i(TAG, "Resolving " + hostPort.host);
InetAddress addr = InetAddress.getByName(hostPort.host);
Log.i(TAG, "Connecting to " + hostPort);
s.connect(new InetSocketAddress(addr, hostPort.port));
Log.i(TAG, "Connected, requesting upload");
OutputStream out = s.getOutputStream();
out.write(CONNECTION_TYPE_UPLOAD);
Random random = new Random();
byte[] buf = new byte[BUFFER_SIZE];
long lastUpdate = 0;
int bytesSinceLastUpdate = 0;
int bytesTotal = 0;
while (bytesTotal < BYTES_TO_TRANSFER) {
int len = Math.min(BUFFER_SIZE, BYTES_TO_TRANSFER - bytesTotal);
random.nextBytes(buf);
out.write(buf, 0, len);
bytesSinceLastUpdate += len;
bytesTotal += len;
long now = System.nanoTime();
long msSinceLastUpdate = (now - lastUpdate) / 1_000_000;
if (msSinceLastUpdate >= MS_PER_UPDATE) {
long bytesPerSecond = bytesSinceLastUpdate * 1000L / msSinceLastUpdate;
throughput.postValue(bytesPerSecond);
lastUpdate = now;
bytesSinceLastUpdate = 0;
}
}
Log.i(TAG, "Upload to " + hostPort + " finished");
} catch (IOException e) {
Log.w(TAG, "Exception from " + hostPort + ": " + e);
} finally {
connectionState.postValue(DISCONNECTED);
throughput.postValue(0L);
tryToClose(s);
}
}
private void download(Socket s, HostPort hostPort) {
connectionState.postValue(DOWNLOADING);
try {
Log.i(TAG, "Resolving " + hostPort.host);
InetAddress addr = InetAddress.getByName(hostPort.host);
Log.i(TAG, "Connecting to " + hostPort + " for download");
s.connect(new InetSocketAddress(addr, hostPort.port));
Log.i(TAG, "Connected, requesting download");
OutputStream out = s.getOutputStream();
out.write(CONNECTION_TYPE_DOWNLOAD);
InputStream in = s.getInputStream();
byte[] buf = new byte[BUFFER_SIZE];
long lastUpdate = 0;
int bytesSinceLastUpdate = 0;
int bytesTotal = 0;
while (bytesTotal < BYTES_TO_TRANSFER) {
int len = Math.min(BUFFER_SIZE, BYTES_TO_TRANSFER - bytesTotal);
int read = in.read(buf, 0, len);
if (read == -1) {
Log.w(TAG, "Download from " + hostPort + " finished early");
return;
}
bytesSinceLastUpdate += read;
bytesTotal += read;
long now = System.nanoTime();
long msSinceLastUpdate = (now - lastUpdate) / 1_000_000;
if (msSinceLastUpdate >= MS_PER_UPDATE) {
long bytesPerSecond = bytesSinceLastUpdate * 1000L / msSinceLastUpdate;
throughput.postValue(bytesPerSecond);
lastUpdate = now;
bytesSinceLastUpdate = 0;
}
}
Log.i(TAG, "Download from " + hostPort + " finished");
} catch (IOException e) {
Log.w(TAG, "Exception from " + hostPort + ": " + e);
} finally {
connectionState.postValue(DISCONNECTED);
throughput.postValue(0L);
tryToClose(s);
}
}
private static class HostPort {
private final String host;
private final int port;
private HostPort(String host, int port) {
this.host = host;
this.port = port;
}
@Override
@NonNull
public String toString() {
return host + ":" + port;
}
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeWidth="1"
android:strokeColor="#00000000">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#008577"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />