1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <result.pb.h>
18 
19 #include <time.h>
20 
21 #include <map>
22 #include <memory>
23 #include <set>
24 #include <string>
25 #include <vector>
26 
27 namespace dittosuite {
28 
29 enum class ResultsOutput { kNull, kReport, kCsv, kPb };
30 
31 class Result {
32  public:
33   struct Statistics {
34     double min, max, mean, median, sd;
35   };
36 
37   explicit Result(const std::string& name, int repeat);
38 
39   void AddMeasurement(const std::string& type, const std::vector<double>& samples);
40   void AddSubResult(std::unique_ptr<Result> result);
41   std::vector<double> GetSamples(const std::string& measurement_name) const;
42   int GetRepeat() const;
43   void Print(ResultsOutput results_output, const std::string& instruction_path);
44 
45   void SetStatistics(const std::string& name, const Statistics& stats);
46 
47   dittosuiteproto::Result ToPb();
48   static std::unique_ptr<Result> FromPb(const dittosuiteproto::Result &pb);
49 
50  private:
51   struct TimeUnit {
52     int dividing_factor;  // dividing factor used for transforming the current time
53                           // unit (ns) in another one (ex 1000 for microseconds)
54     std::string name;
55   };
56   struct BandwidthUnit {
57     int dividing_factor;  // dividing factor used for transforming the bandwidth
58                           // unit (KB/s) in another one (ex GB/s)
59     std::string name;
60   };
61   TimeUnit time_unit_;
62   BandwidthUnit bandwidth_unit_;
63   std::string name_;
64   std::map<std::string, std::vector<double>> samples_;
65   std::map<std::string, Statistics> statistics_;
66   std::vector<std::unique_ptr<Result>> sub_results_;
67   int repeat_;
68 
69   void PrintHistograms(const std::string& instruction_path);
70   void PrintStatisticsTables();
71   void MakeStatisticsCsv();
72   void MakeStatisticsPb();
73 
74   void AnalyseMeasurement(const std::string& name);
75   std::vector<int> ComputeNormalizedFrequencyVector(const std::string& measurement_name);
76   std::set<std::string> GetMeasurementsNames();
77   void PrintStatisticsTableContent(const std::string& instruction_path,
78                                    const std::string& measurement_name);
79 
80   std::string ComputeNextInstructionPath(const std::string& instruction_path);
81   void PrintStatisticInCsv(std::ostream& csv_stream, const std::string& instruction_path,
82                            const std::set<std::string>& measurements_names);
83   void PrintHistogramHeader(const std::string& measurement_name);
84   void MakeHistogramFromVector(const std::vector<int>& freq_vector, int min_value);
85   TimeUnit GetTimeUnit(int64_t min_value);
86   BandwidthUnit GetBandwidthUnit(int64_t min_value);
87   void PrintMeasurementStatisticInCsv(std::ostream& csv_stream, const std::string& name);
88 
89   void __ToPb(dittosuiteproto::Result* result_pb);
90   void StoreStatisticsInPb(dittosuiteproto::Metrics* metrics, const std::string& name);
91 };
92 
93 void PrintPb(const dittosuiteproto::Result &pb);
94 
95 }  // namespace dittosuite
96