summaryrefslogtreecommitdiff
path: root/BenchmarkFramework/app/src/main/java/org/linaro/iasenov/benchmarkframework/BaseBenchmark.java
diff options
context:
space:
mode:
Diffstat (limited to 'BenchmarkFramework/app/src/main/java/org/linaro/iasenov/benchmarkframework/BaseBenchmark.java')
-rw-r--r--BenchmarkFramework/app/src/main/java/org/linaro/iasenov/benchmarkframework/BaseBenchmark.java270
1 files changed, 270 insertions, 0 deletions
diff --git a/BenchmarkFramework/app/src/main/java/org/linaro/iasenov/benchmarkframework/BaseBenchmark.java b/BenchmarkFramework/app/src/main/java/org/linaro/iasenov/benchmarkframework/BaseBenchmark.java
new file mode 100644
index 0000000..b78c55e
--- /dev/null
+++ b/BenchmarkFramework/app/src/main/java/org/linaro/iasenov/benchmarkframework/BaseBenchmark.java
@@ -0,0 +1,270 @@
+package org.linaro.iasenov.benchmarkframework;
+
+import android.content.res.AssetManager;
+import android.util.Log;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * Created by iasenov on 8/22/16.
+ */
+public class BaseBenchmark {
+
+ public String benchmarkName;
+ public String version;
+ public String date$;
+ public static long startTest;
+ public static long endTest;
+ public static double testTime;
+
+ public String[] xout = new String[20];
+
+ private String TAG = "BaseBenchmark";
+
+
+ //**********************************************************************************************
+ //Clear test tags and result and fill version and date for the test
+ //**********************************************************************************************
+ public void clear() {
+ Date today = Calendar.getInstance().getTime();
+ SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH.mm");
+ date$ = formatter.format(today);
+
+ xout[0] = version + " " + date$ + "\n"; //Line for test version
+ xout[1] = "\n";
+ xout[2] = "\n"; //Line for test labels
+ xout[3] = "\n"; //Line for test labels
+ xout[4] = "\n"; //Line for test labels
+ xout[5] = "\n"; //Line for test result
+ xout[6] = "\n"; //Line for test result
+ xout[7] = "\n"; //Line for test result
+ xout[8] = "\n"; //Line for test result
+ xout[9] = "\n"; //Line for test result
+ xout[10] = "\n"; //Line for test result
+ xout[11] = "\n"; //Line for test result
+ xout[12] = "\n"; //Line for test result
+ xout[13] = "\n"; //Line for test result
+ xout[14] = "\n"; //Line for test result
+ xout[15] = "\n";
+ xout[16] = "\n"; //Line for total elapsed time
+ }
+
+ //**********************************************************************************************
+ //return string with test result ready to be shown on TextView
+ //**********************************************************************************************
+ public String getBenchmarkResult(int elapsedTimeIdx) {
+ testTime = (double) (endTest - startTest) / 1000;
+
+ if(elapsedTimeIdx == -1)
+ {
+ elapsedTimeIdx = 16;
+ }
+
+ xout[15] = "\n";
+ xout[elapsedTimeIdx] = " Total Elapsed Time " + String.format("%5.1f", testTime)
+ + " seconds\n";
+
+ return xout[0] + xout[1] + xout[2] + xout[3] +
+ xout[4] + xout[5] + xout[6] + xout[7] +
+ xout[8] + xout[9] + xout[10] + xout[11] +
+ xout[12] + xout[13] + xout[14] + xout[15] +
+ xout[16];
+ }
+
+ //**********************************************************************************************
+ //This method is called from MainActivity for related(chosen) test
+ //all test class that extend BaseBenchmark implement this method
+ //**********************************************************************************************
+ public String startBenchmark(int count) {
+ return null;
+ }
+
+ //**********************************************************************************************
+ //Read from logcat by selected "tag" and return string with all lines found
+ //**********************************************************************************************
+ public String readFromLogcat(String tag) {
+ StringBuilder log = null;
+ try {
+ Process process = Runtime.getRuntime().exec("logcat -d -s " + tag);
+ BufferedReader bufferedReader = new BufferedReader(
+ new InputStreamReader(process.getInputStream()));
+
+ log = new StringBuilder();
+
+ String separator = System.getProperty("line.separator");
+ String line;
+ while ((line = bufferedReader.readLine()) != null) {
+ line = line.replace("I/" + tag + "(", "");
+ int index = line.indexOf(")");
+ line = line.substring(index + 3);
+ line = line.replace("null", "");
+ log.append(line);
+ log.append(separator);
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return log.toString();
+ }
+
+ //**********************************************************************************************
+ //Clear logcat
+ //**********************************************************************************************
+ public void clearLogcat() {
+ try {
+ Runtime.getRuntime().exec(new String[]{"logcat", "-c"});
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+ }
+
+
+ //**********************************************************************************************
+ //Read from log file (fill with logcat lines) by selected "tag"
+ //**********************************************************************************************
+ public String readFromLogcatFile(String tag) {
+
+
+ //Get the logcat text file
+ File file = new File(MainActivity.LOG_PATH);
+
+ //Read text from file
+ StringBuilder text = new StringBuilder();
+
+ try {
+ BufferedReader br = new BufferedReader(new FileReader(file));
+ String line;
+
+ while ((line = br.readLine()) != null) {
+ line = line.replace("I/" + tag + "(", "");
+ int index = line.indexOf(")");
+ line = line.substring(index + 3);
+ line = line.replace("null", "");
+ text.append(line);
+ text.append('\n');
+ }
+ br.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return text.toString();
+ }
+
+
+ //**********************************************************************************************
+ //Copy file with InputStream "in" to file with OutputStream "out"
+ //**********************************************************************************************
+ public void copy(InputStream in, OutputStream out)
+ {
+ try {
+ byte[] buffer = new byte[1024];
+ int read;
+ while ((read = in.read(buffer)) != -1) {
+ out.write(buffer, 0, read);
+ }
+ in.close();
+ in = null;
+
+ // write the output file (You have now copied the file)
+ out.flush();
+ out.close();
+ out = null;
+
+
+ } catch (FileNotFoundException fnfe1) {
+ Log.i(TAG, fnfe1.getMessage());
+ }
+ catch (Exception e) {
+ Log.i(TAG, e.getMessage());
+ }
+
+ Log.i(TAG, "Copy end");
+ }
+
+
+
+
+
+ //**********************************************************************************************
+ //Copy "executable_filename" from assets to "EXECUTABLE_PATH" and execute this file
+ //Return String from process's stdout
+ //**********************************************************************************************
+ public String copyAssetsExecFileAndRun(String executable_filename, String param) {
+
+ String executableFilePath = MainActivity.EXECUTABLE_PATH;
+ StringBuilder log = null;
+
+
+ File dir = new File(executableFilePath);
+ dir.mkdirs();
+
+ AssetManager assetManager = MainActivity.mActivity.getAssets();
+
+ InputStream in = null;
+ OutputStream out = null;
+ Log.d(TAG, "Attempting to copy this file: " + executable_filename);
+
+ try {
+ in = assetManager.open(executable_filename);
+
+ Log.i(TAG, "outDir: " + executableFilePath);
+ File outFile = new File(executableFilePath, executable_filename);
+
+ out = new FileOutputStream(outFile);
+
+ Log.i(TAG, "Copy begin");
+ copy(in, out);
+
+ } catch (IOException e) {
+ Log.i(TAG, "Failed to copy asset file: " + executable_filename, e);
+ }
+
+ Log.i(TAG, "Copy success: " + executable_filename);
+
+ try {
+
+ File execFile = new File(executableFilePath + "/" + executable_filename);
+ execFile.setExecutable(true);
+
+ Process process = Runtime.getRuntime().exec(executableFilePath + "/" + executable_filename + " " + param);
+ Log.i(TAG, "process:" + process.toString());
+
+ BufferedReader bufferedReader = new BufferedReader(
+ new InputStreamReader(process.getInputStream()));
+
+ String separator = System.getProperty("line.separator");
+
+ log = new StringBuilder();
+ String line;
+ while ((line = bufferedReader.readLine()) != null) {
+ line = line.replace("null", "");
+ log.append(line);
+ log.append(separator);
+ log.append(separator);
+ Log.i(TAG, "process line:" + line);
+ }
+
+ process.destroy();
+
+ }catch(Exception e){
+ Log.i(TAG, "Failed execute: " + executableFilePath + "/" + executable_filename, e);
+ }
+
+ return log.toString();
+ }
+//**********************************************************************************************
+}//BaseBenchmark
+//********************************************************************************************** \ No newline at end of file