1 /*
2  * Copyright (C) 2019 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 
17 #ifndef __BENCHMARK_TEST_ENVIRONMENT_H__
18 #define __BENCHMARK_TEST_ENVIRONMENT_H__
19 
20 #include <gtest/gtest.h>
21 
22 #include <getopt.h>
23 
24 using namespace std;
25 
26 class BenchmarkTestEnvironment : public ::testing::Environment {
27   public:
BenchmarkTestEnvironment()28     BenchmarkTestEnvironment()
29         : res("/data/local/tmp/MediaBenchmark/res/"),
30           statsFile("/data/local/tmp/MediaBenchmark/res/stats.csv") {}
31 
32     // Parses the command line argument
33     int initFromOptions(int argc, char **argv);
34 
setRes(const char * _res)35     void setRes(const char *_res) { res = _res; }
36 
getRes()37     const string getRes() const { return res; }
38 
setStatsFile(const string module)39     void setStatsFile(const string module) { statsFile = getRes() + module; }
40 
getStatsFile()41     const string getStatsFile() const { return statsFile; }
42 
43     bool writeStatsHeader();
44 
45   private:
46     string res;
47     string statsFile;
48 };
49 
initFromOptions(int argc,char ** argv)50 int BenchmarkTestEnvironment::initFromOptions(int argc, char **argv) {
51     static struct option options[] = {{"path", required_argument, 0, 'P'}, {0, 0, 0, 0}};
52 
53     while (true) {
54         int index = 0;
55         int c = getopt_long(argc, argv, "P:", options, &index);
56         if (c == -1) {
57             break;
58         }
59 
60         switch (c) {
61             case 'P': {
62                 setRes(optarg);
63                 break;
64             }
65             default:
66                 break;
67         }
68     }
69 
70     if (optind < argc) {
71         fprintf(stderr,
72                 "unrecognized option: %s\n\n"
73                 "usage: %s <gtest options> <test options>\n\n"
74                 "test options are:\n\n"
75                 "-P, --path: Resource files directory location\n",
76                 argv[optind ?: 1], argv[0]);
77         return 2;
78     }
79     return 0;
80 }
81 
82 /**
83  * Writes the stats header to a file
84  * <p>
85  * \param statsFile    file where the stats data is to be written
86  **/
writeStatsHeader()87 bool BenchmarkTestEnvironment::writeStatsHeader() {
88     char statsHeader[] =
89         "currentTime, fileName, operation, componentName, NDK/SDK, sync/async, setupTime, "
90         "destroyTime, minimumTime, maximumTime, averageTime, timeToProcess1SecContent, "
91         "totalBytesProcessedPerSec, timeToFirstFrame, totalSizeInBytes, totalTime\n";
92     FILE *fpStats = fopen(statsFile.c_str(), "w");
93     if(!fpStats) {
94         return false;
95     }
96     int32_t numBytes = fwrite(statsHeader, sizeof(char), sizeof(statsHeader), fpStats);
97     fclose(fpStats);
98     if(numBytes != sizeof(statsHeader)) {
99         return false;
100     }
101     return true;
102 }
103 
104 #endif  // __BENCHMARK_TEST_ENVIRONMENT_H__
105