summaryrefslogtreecommitdiff
path: root/BenchmarkFramework/app/src/main/java/org/linaro/iasenov/benchmarkframework/BaseBenchmark.java
blob: 3082ddee9301e28a2250edfd4a9a8870e24a3710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by iasenov on 8/22/16.
 */
public class BaseBenchmark {

    public String benchmarkName;
    public String version;
    public String date$;
    public String executable_name;
    public static long startTest;
    public static long endTest;
    public static double testTime;

    public List<String> TESTCOMMANDS;   //List with Specific test commands
    public String[] xout = new String[20];

    private String TAG = "BaseBenchmark";

    //List with All tests commands
    public List<String> ALLCOMMANDS = Arrays.asList(
            "harness -t bounce",
            "harness -t memchr",
            "harness -t memcpy",
            "harness -t memset",
            "harness -t strchr",
            "harness -t strcmp",
            "harness -t strcpy",
            "harness -t strlen",
            "dhry N",
//            "iozone -a -i 0 -b output.xls",
            "iozone -E -C 70 -L -S -W"


    );



    //**********************************************************************************************
    //Get all commands by executable_name
    //**********************************************************************************************
    public List<String> getCommands(String executable_name)
    {

        List<String> cmdListStr = new ArrayList<String>();
        int len = executable_name.length();

        for(String command: ALLCOMMANDS)
          {
              if(command.length() >= len) {

                  String subCmd = command.substring(0, len);
                  //Log.i(TAG, "subCmd: " + subCmd);

                  if (executable_name.equals(subCmd)) {
                      cmdListStr.add(command.substring(len + 1));
                      //Log.i(TAG, "add command: " + command.substring(len + 1));
                  }
              }
          }

        return cmdListStr;
    }



    //**********************************************************************************************
    //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, List<String> TESTCOMMANDS) {

        String executableFilePath = MainActivity.EXECUTABLE_PATH;
        StringBuilder log = null;
        File dir_local = new File(executableFilePath);
        String currentCommand = "";
        String result = "";


        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);

        if(TESTCOMMANDS.size() == 0) TESTCOMMANDS.add(""); //add empty command to run test once

        for(String command: TESTCOMMANDS) {
            currentCommand = command;

            try {

                File execFile = new File(executableFilePath + "/" + executable_filename);
                execFile.setExecutable(true);

                Process process = Runtime.getRuntime().exec(executableFilePath + "/" + executable_filename + " " + currentCommand, null, dir_local);
                Log.i(TAG, "process:" + process.toString());
                Log.i(TAG, "Execute: "+ executable_filename + " " + currentCommand);

                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, "*****************" + line);
                }

                result = result + log.toString();
                process.destroy();

            } catch (Exception e) {
                Log.i(TAG, "Failed execute: " + executableFilePath + "/" + executable_filename, e);
            }
        }

        return result;
    }
//**********************************************************************************************
}//BaseBenchmark
//**********************************************************************************************