1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.rs.blasbenchmark;
18
19
20import android.os.Bundle;
21import android.util.Log;
22
23import com.example.android.rs.blasbenchmark.BlasTestList.TestName;
24import com.example.android.rs.blasbenchmark.BlasTestRunner;
25
26import android.test.ActivityInstrumentationTestCase2;
27import android.test.suitebuilder.annotation.LargeTest;
28
29/**
30 * BLAS benchmark test.
31 * To run the test, please use command
32 *
33 * adb shell am instrument -e iteration <n> -w com.example.android.rs.blasbenchmark/.BlasTestRunner
34 *
35 */
36public class BlasTest extends ActivityInstrumentationTestCase2<BlasBenchmark> {
37    private final String TAG = "BLAS Test";
38    private final String TEST_NAME = "Testname";
39    private final String ITERATIONS = "Iterations";
40    private final String BENCHMARK = "Benchmark";
41    private static int INSTRUMENTATION_IN_PROGRESS = 2;
42    private int mIteration;
43    private BlasBenchmark mActivity;
44
45    public BlasTest() {
46        super(BlasBenchmark.class);
47    }
48
49    @Override
50    public void setUp() throws Exception {
51        super.setUp();
52        setActivityInitialTouchMode(false);
53        mActivity = getActivity();
54        BlasTestRunner mRunner = (BlasTestRunner) getInstrumentation();
55        mIteration = mRunner.mIteration;
56        assertTrue("please enter a valid iteration value", mIteration > 0);
57   }
58
59    @Override
60    public void tearDown() throws Exception {
61        super.tearDown();
62    }
63
64    class TestAction implements Runnable {
65        TestName mTestName;
66        float mResult;
67        public TestAction(TestName testName) {
68            mTestName = testName;
69        }
70        public void run() {
71            mActivity.changeTest(mTestName, false);
72            //mResult = mActivity.getBenchmark();
73            Log.v(TAG, "Benchmark for test \"" + mTestName.toString() + "\" is: " + mResult);
74            synchronized(this) {
75                this.notify();
76            }
77        }
78        public float getBenchmark() {
79            return mResult;
80        }
81    }
82
83    // Set the benchmark thread to run on ui thread
84    // Synchronized the thread such that the test will wait for the benchmark thread to finish
85    public void runOnUiThread(Runnable action) {
86        synchronized(action) {
87            mActivity.runOnUiThread(action);
88            try {
89                action.wait();
90            } catch (InterruptedException e) {
91                Log.v(TAG, "waiting for action running on UI thread is interrupted: " +
92                        e.toString());
93            }
94        }
95    }
96
97    public void runTest(TestAction ta, String testName) {
98        float sum = 0;
99        for (int i = 0; i < mIteration; i++) {
100            runOnUiThread(ta);
101            float bmValue = ta.getBenchmark();
102            Log.v(TAG, "results for iteration " + i + " is " + bmValue);
103            sum += bmValue;
104        }
105        float avgResult = sum/mIteration;
106
107        // post result to INSTRUMENTATION_STATUS
108        Bundle results = new Bundle();
109        results.putString(TEST_NAME, testName);
110        results.putInt(ITERATIONS, mIteration);
111        results.putFloat(BENCHMARK, avgResult);
112        getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, results);
113    }
114
115    // Test case 0: SGEMM Test Small
116    @LargeTest
117    public void testSGEMMSmall() {
118        TestAction ta = new TestAction(TestName.SGEMM_SMALL);
119        runTest(ta, TestName.SGEMM_SMALL.name());
120    }
121
122    // Test case 1: SGEMM Test Medium
123    @LargeTest
124    public void testSGEMMedium() {
125        TestAction ta = new TestAction(TestName.SGEMM_MEDIUM);
126        runTest(ta, TestName.SGEMM_MEDIUM.name());
127    }
128
129    // Test case 2: SGEMM Test Large
130    @LargeTest
131    public void testSGEMMLarge() {
132        TestAction ta = new TestAction(TestName.SGEMM_LARGE);
133        runTest(ta, TestName.SGEMM_LARGE.name());
134    }
135
136    // Test case 3: 8Bit GEMM Test Small
137    @LargeTest
138    public void testBNNMSmall() {
139        TestAction ta = new TestAction(TestName.BNNM_SMALL);
140        runTest(ta, TestName.BNNM_SMALL.name());
141    }
142
143    // Test case 4: 8Bit GEMM Test Medium
144    @LargeTest
145    public void testBNNMMMedium() {
146        TestAction ta = new TestAction(TestName.BNNM_MEDIUM);
147        runTest(ta, TestName.BNNM_MEDIUM.name());
148    }
149
150    // Test case 5: 8Bit GEMM Test Large
151    @LargeTest
152    public void testBNNMLarge() {
153        TestAction ta = new TestAction(TestName.BNNM_LARGE);
154        runTest(ta, TestName.BNNM_LARGE.name());
155    }
156}
157