diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-05-21 14:16:57 +0200 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-05-21 14:16:57 +0200 |
commit | 14ba0ed08735052acde18830ea4cf51d1e30c7b9 (patch) | |
tree | 9a44d160d1640208e7e7af664d536af8bc7cfabd /same-android | |
parent | f62786575d01811b4af057c35f8ed4c7a43a4429 (diff) |
Add Experiment1Activity.
Diffstat (limited to 'same-android')
3 files changed, 149 insertions, 1 deletions
diff --git a/same-android/AndroidManifest.xml b/same-android/AndroidManifest.xml index d7d8f95..54a6356 100644 --- a/same-android/AndroidManifest.xml +++ b/same-android/AndroidManifest.xml @@ -22,6 +22,7 @@ <activity android:name=".StateViewerActivity"/> <activity android:name=".GraphicsActivity"/> <activity android:name="com.orbekk.same.android.benchmark.RepeatedSetVariableActivity"/> + <activity android:name="com.orbekk.same.android.benchmark.Experiment1Activity"/> <activity android:name="com.orbekk.same.android.benchmark.ExampleProtobufServerActivity"/> <activity android:name="com.orbekk.same.android.benchmark.ExampleHttpServerActivity"/> <service android:name=".SameService" android:process="com.orbekk.same.android.SameService"/> diff --git a/same-android/src/main/java/com/orbekk/same/android/MainActivity.java b/same-android/src/main/java/com/orbekk/same/android/MainActivity.java index d79f21a..7bd494e 100644 --- a/same-android/src/main/java/com/orbekk/same/android/MainActivity.java +++ b/same-android/src/main/java/com/orbekk/same/android/MainActivity.java @@ -32,6 +32,7 @@ import android.widget.ArrayAdapter; import android.widget.ListView; import com.orbekk.same.android.benchmark.ExampleProtobufServerActivity; +import com.orbekk.same.android.benchmark.Experiment1Activity; import com.orbekk.same.android.benchmark.RepeatedSetVariableActivity; public class MainActivity extends Activity { @@ -44,9 +45,12 @@ public class MainActivity extends Activity { activities.put("Variable test", VariableTestActivity.class); activities.put("State monitor", StateViewerActivity.class); activities.put("Graphics demo", GraphicsActivity.class); - activities.put("Benchmark", RepeatedSetVariableActivity.class); + activities.put("Benchmark (logger)", RepeatedSetVariableActivity.class); + activities.put("ExampleProtobufServer (temp)", ExampleProtobufServerActivity.class); + + activities.put("Experiment 1", Experiment1Activity.class); } public final static List<String> activityList = diff --git a/same-android/src/main/java/com/orbekk/same/android/benchmark/Experiment1Activity.java b/same-android/src/main/java/com/orbekk/same/android/benchmark/Experiment1Activity.java new file mode 100644 index 0000000..ffcf930 --- /dev/null +++ b/same-android/src/main/java/com/orbekk/same/android/benchmark/Experiment1Activity.java @@ -0,0 +1,143 @@ +/** + * Copyright 2012 Kjetil Ørbekk <kjetil.orbekk@gmail.com> + * + * 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 com.orbekk.same.android.benchmark; + +import java.io.IOException; +import java.net.UnknownHostException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.Toast; + +import com.google.protobuf.RpcCallback; +import com.orbekk.protobuf.Rpc; +import com.orbekk.protobuf.RpcChannel; +import com.orbekk.same.Types; +import com.orbekk.same.Variable; +import com.orbekk.same.Variable.OnChangeListener; +import com.orbekk.same.android.ClientInterfaceBridge; +import com.orbekk.stats.Common; +import com.orbekk.stats.Experiments.Experiment1; +import com.orbekk.stats.Experiments.Empty; +import com.orbekk.stats.Experiments.Experiment1; +import com.orbekk.stats.Experiments.SimpleTiming; + +public class Experiment1Activity 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() { + RpcChannel channel = null; + try { + RpcCallback<Empty> done = new RpcCallback<Empty>() { + @Override public void run(Empty unused) { + } + }; + channel = RpcChannel.create(Common.HOSTNAME, Common.PORT); + Experiment1 exp1 = Experiment1.newStub(channel); + Rpc rpc = new Rpc(); + rpc.setTimeout(5000); + int warmupIterationsLeft = WARMUP_ITERATIONS; + for (Long sample : timer.getTimes()) { + if (warmupIterationsLeft-- > 0) { + continue; + } + SimpleTiming timing = SimpleTiming.newBuilder() + .setTiming(sample) + .setNumDevices(-1) + .build(); + exp1.registerSample(rpc, timing, done); + } + rpc.await(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + if (channel != null) { + channel.close(); + } + } + } + + /** 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.INTEGER); + variable.addOnChangeListener(changeListener); + timer.start(); + variable.set(0); + } + + public void onStop() { + super.onStop(); + client.disconnect(); + } + +} |