1 /*
2  * Copyright (C) 2018 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 package com.android.nn.benchmark.util;
18 
19 import android.util.Log;
20 
21 import com.android.nn.benchmark.core.BenchmarkResult;
22 
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 
30 /**
31  * Serialize benchmark results into CSV file for further processing.
32  */
33 public class CSVWriter implements AutoCloseable {
34     private static final String TAG = CSVWriter.class.getSimpleName();
35     private final BufferedWriter writer;
36 
CSVWriter(File csvFile)37     public CSVWriter(File csvFile) throws IOException {
38         writer = new BufferedWriter(new FileWriter(csvFile, true));
39     }
40 
41     static final String RESULT_FORMAT_COMMENT = "#testInfo,backendType"
42             + ",inferenceIterations,inferenceTotalTimeSec"
43             + ",inferenceTimeFreqStartSec,inferenceTimeFreqStepSec"
44             + ",inferenceTimeFreqBucketCount,inferenceTimeFreqBucket1,..."
45             + ",maxSingleError,testSetSize,evaluatorsCount,validationErrorsCount,evaluatorKey1,..."
46             + ",evaluatorResult1,...,validationError1,..."
47             + ",hasCompileWithoutCacheResults"
48             + ",compileWithoutCacheIterations,compileWithoutCacheTotalTimeSec"
49             + ",compileWithoutCacheTimeFreqStartSec,compileWithoutCacheTimeFreqStepSec"
50             + ",compileWithoutCacheTimeFreqBucketCount,compileWithoutCacheTimeFreqBucket1,..."
51             + ",hasSaveToCacheResults"
52             + ",saveToCacheIterations,saveToCacheTotalTimeSec"
53             + ",saveToCacheTimeFreqStartSec,saveToCacheTimeFreqStepSec"
54             + ",saveToCacheTimeFreqBucketCount,saveToCacheTimeFreqBucket1,..."
55             + ",hasPrepareFromCacheResults"
56             + ",prepareFromCacheIterations,prepareFromCacheTotalTimeSec"
57             + ",prepareFromCacheTimeFreqStartSec,prepareFromCacheTimeFreqStepSec"
58             + ",prepareFromCacheTimeFreqBucketCount,prepareFromCacheTimeFreqBucket1,..."
59             + ",compilationCacheSizeBytes";
60 
deviceInfoCsvLine()61     String deviceInfoCsvLine() {
62         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
63         StringBuilder sb = new StringBuilder();
64         sb.append(sdf.format(new Date())).append(',');
65         sb.append(android.os.Build.DISPLAY).append('\n');
66         return sb.toString();
67     }
68 
write(BenchmarkResult benchmarkResult)69     public void write(BenchmarkResult benchmarkResult) throws IOException {
70         writer.write(benchmarkResult.toCsvLine());
71     }
72 
writeHeader()73     public void writeHeader() throws IOException {
74         writer.write(deviceInfoCsvLine());
75         writer.write(RESULT_FORMAT_COMMENT);
76         writer.write('\n');
77     }
78 
79     @Override
close()80     public void close() {
81         try {
82             writer.close();
83         } catch (IOException e) {
84             Log.e(TAG, "Failure to close writer", e);
85         }
86     }
87 }
88