summaryrefslogtreecommitdiff
path: root/same-android/src/main/java/com
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-01 21:26:57 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-01 21:26:57 +0100
commitb6f6a0afdacead5c05a939511b5d0bee021f641e (patch)
treec94f10dea65c169f303f14580b5a742ca2fd14f3 /same-android/src/main/java/com
parent5a06b9427bbdd776d691ad1736cd147aa3914031 (diff)
Add Benchmarking activity.
Diffstat (limited to 'same-android/src/main/java/com')
-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
3 files changed, 141 insertions, 0 deletions
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 + ")]";
+ }
+}