summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--same-android/AndroidManifest.xml1
-rw-r--r--same-android/res/menu/main_menu.xml4
-rw-r--r--same-android/src/main/java/com/orbekk/same/MainActivity.java4
-rw-r--r--same-android/src/main/java/com/orbekk/same/android/benchmark/RepeatedSetVariableActivity.java85
-rw-r--r--same-android/src/main/java/com/orbekk/same/android/benchmark/Timer.java52
-rw-r--r--same/src/main/java/com/orbekk/same/SameController.java3
6 files changed, 148 insertions, 1 deletions
diff --git a/same-android/AndroidManifest.xml b/same-android/AndroidManifest.xml
index 6331a1c..40ceaf7 100644
--- a/same-android/AndroidManifest.xml
+++ b/same-android/AndroidManifest.xml
@@ -22,6 +22,7 @@
<activity android:name=".VariableTestActivity"/>
<activity android:name=".StateViewerActivity"/>
<activity android:name=".GraphicsActivity"/>
+ <activity android:name="com.orbekk.same.android.benchmark.RepeatedSetVariableActivity"/>
<service android:name=".SameService" android:process="com.orbekk.same.SameService"/>
</application>
diff --git a/same-android/res/menu/main_menu.xml b/same-android/res/menu/main_menu.xml
index 4235d2b..342ca60 100644
--- a/same-android/res/menu/main_menu.xml
+++ b/same-android/res/menu/main_menu.xml
@@ -14,5 +14,9 @@
<item
android:id="@+id/graphics_demo"
android:title="Graphics demo"/>
+
+ <item
+ android:id="@+id/benchmark"
+ android:title="Benchmark"/>
</menu> \ No newline at end of file
diff --git a/same-android/src/main/java/com/orbekk/same/MainActivity.java b/same-android/src/main/java/com/orbekk/same/MainActivity.java
index 5c1f144..5d76c1e 100644
--- a/same-android/src/main/java/com/orbekk/same/MainActivity.java
+++ b/same-android/src/main/java/com/orbekk/same/MainActivity.java
@@ -3,6 +3,8 @@ package com.orbekk.same;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.orbekk.same.android.benchmark.RepeatedSetVariableActivity;
+
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
@@ -39,6 +41,8 @@ public class MainActivity extends Activity {
case R.id.graphics_demo:
startActivity(new Intent(this, GraphicsActivity.class));
break;
+ case R.id.benchmark:
+ startActivity(new Intent(this, RepeatedSetVariableActivity.class));
default:
logger.error("Unknown menu entry: {}", item);
}
diff --git a/same-android/src/main/java/com/orbekk/same/android/benchmark/RepeatedSetVariableActivity.java b/same-android/src/main/java/com/orbekk/same/android/benchmark/RepeatedSetVariableActivity.java
new file mode 100644
index 0000000..7940939
--- /dev/null
+++ b/same-android/src/main/java/com/orbekk/same/android/benchmark/RepeatedSetVariableActivity.java
@@ -0,0 +1,85 @@
+package com.orbekk.same.android.benchmark;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.widget.Toast;
+
+import com.orbekk.same.Types;
+import com.orbekk.same.Variable;
+import com.orbekk.same.Variable.OnChangeListener;
+import com.orbekk.same.android.ClientInterfaceBridge;
+
+public class RepeatedSetVariableActivity extends Activity {
+ private Logger logger = LoggerFactory.getLogger(getClass());
+ public static final int WARMUP_ITERATIONS = 10;
+ public static final int ITERATIONS = 100;
+ private ClientInterfaceBridge client;
+
+ private int warmupIterationsPerformed;
+ private int iterationsPerformed;
+ private Timer timer;
+
+ private Variable<Integer> variable;
+
+ private OnChangeListener<Integer> changeListener =
+ new OnChangeListener<Integer>() {
+ @Override
+ public void valueChanged(Variable<Integer> variable) {
+ variable.update();
+ timer.stop();
+ iterationFinished();
+ if (iterationsPerformed < ITERATIONS) {
+ timer.start();
+ variable.set(variable.get() + 1);
+ } else {
+ finalizeBenchmark();
+ }
+ }
+ };
+
+ private void finalizeBenchmark() {
+ logger.info("Benchmark finished. Samples: " + timer);
+ }
+
+ /** Returns whether or not we should continue. */
+ private void iterationFinished() {
+ if (warmupIterationsPerformed < WARMUP_ITERATIONS) {
+ warmupIterationsPerformed += 1;
+ } else {
+ iterationsPerformed += 1;
+ }
+ }
+
+ @Override public void onCreate(Bundle savedBundle) {
+ super.onCreate(savedBundle);
+ }
+
+ public void onResume() {
+ super.onResume();
+ Toast.makeText(this, "Starting benchmark", Toast.LENGTH_LONG).show();
+
+ timer = new Timer(WARMUP_ITERATIONS + ITERATIONS);
+ warmupIterationsPerformed = 0;
+ iterationsPerformed = 0;
+ client = new ClientInterfaceBridge(this);
+ client.connect();
+ initializeVariable();
+ }
+
+ public void initializeVariable() {
+ variable = client.createVariableFactory()
+ .create("BenchmarkVariable", Types.fromType(Integer.class));
+ variable.addOnChangeListener(changeListener);
+ timer.start();
+ variable.set(0);
+ }
+
+ public void onStop() {
+ super.onStop();
+ client.disconnect();
+ }
+
+}
diff --git a/same-android/src/main/java/com/orbekk/same/android/benchmark/Timer.java b/same-android/src/main/java/com/orbekk/same/android/benchmark/Timer.java
new file mode 100644
index 0000000..a3e32b8
--- /dev/null
+++ b/same-android/src/main/java/com/orbekk/same/android/benchmark/Timer.java
@@ -0,0 +1,52 @@
+package com.orbekk.same.android.benchmark;
+
+import java.util.ArrayList;
+
+public class Timer {
+ ArrayList<Long> samples;
+ boolean running = false;
+ long startTime = 0;
+
+ public Timer() {
+ samples = new ArrayList<Long>();
+ }
+
+ public Timer(int capacity) {
+ samples = new ArrayList<Long>(capacity);
+ }
+
+ public boolean isRunning() {
+ return running;
+ }
+
+ public void start() {
+ startTime = System.currentTimeMillis();
+ running = true;
+ }
+
+ public void stop() {
+ long stopTime = System.currentTimeMillis();
+ running = false;
+ samples.add(stopTime - startTime);
+ }
+
+ public long getLastMeasurement() {
+ return samples.get(samples.size() - 1);
+ }
+
+ public ArrayList<Long> getTimes() {
+ return new ArrayList<Long>(samples);
+ }
+
+ public long getSum() {
+ long sum = 0;
+ for (long s : samples) {
+ sum += s;
+ }
+ return sum;
+ }
+
+ public String toString() {
+ return "Timer[sum(" + getSum() + "), samples(" + samples + ")]";
+ }
+}
diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java
index 1261816..03044cf 100644
--- a/same/src/main/java/com/orbekk/same/SameController.java
+++ b/same/src/main/java/com/orbekk/same/SameController.java
@@ -12,6 +12,7 @@ import com.orbekk.paxos.PaxosServiceImpl;
import com.orbekk.same.config.Configuration;
import com.orbekk.same.http.ServerContainer;
import com.orbekk.same.http.StateServlet;
+import com.orbekk.same.http.JettyServerBuilder;
import com.orbekk.same.http.TjwsServerBuilder;
public class SameController {
@@ -60,7 +61,7 @@ public class SameController {
StateServlet stateServlet = new StateServlet(client.getInterface(),
new VariableFactory(client.getInterface()));
- ServerContainer server = new TjwsServerBuilder(port)
+ ServerContainer server = new JettyServerBuilder(port)
.withServlet(stateServlet, "/_/state")
.withService(client.getService(), ClientService.class)
.withService(master.getService(), MasterService.class)