1 /*
2 Copyright 2019 Google LLC
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     https://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 APIB_REPORTING_H
18 #define APIB_REPORTING_H
19 
20 #include <cstdint>
21 #include <memory>
22 #include <ostream>
23 #include <string>
24 #include <vector>
25 
26 #include "apib/apib_iothread.h"
27 
28 namespace apib {
29 
30 // Per-thread counters. We swap these in and out of IOThreads so that we can
31 // efficiently count with a minimum of global synchronization
32 class Counters {
33  public:
34   int_fast32_t successfulRequests = 0LL;
35   int_fast32_t failedRequests = 0LL;
36   int_fast64_t bytesRead = 0LL;
37   int_fast64_t bytesWritten = 0LL;
38   std::vector<int_fast64_t> latencies;
39 };
40 
41 class BenchmarkResults {
42  public:
43   int32_t completedRequests;
44   int32_t successfulRequests;
45   int32_t unsuccessfulRequests;
46   int32_t socketErrors;
47   int32_t connectionsOpened;
48   int64_t totalBytesSent;
49   int64_t totalBytesReceived;
50 
51   // Consolidated times in seconds
52   double elapsedTime;
53 
54   // Consolidated latencies in milliseconds
55   double averageLatency;
56   double latencyStdDev;
57   double latencies[101];
58 
59   // Throughput in requests / second
60   double averageThroughput;
61 
62   // Megabits / second
63   double averageSendBandwidth;
64   double averageReceiveBandwidth;
65 };
66 
67 class BenchmarkIntervalResults {
68  public:
69   int32_t successfulRequests;
70   // In seconds
71   double elapsedTime;
72   double intervalTime;
73   // In tps, for this interval
74   double averageThroughput;
75 };
76 
77 // One time initialization
78 extern void RecordInit(const std::string& monitorHost,
79                        const std::string& monitor2Host);
80 
81 // Start a reporting run
82 extern void RecordStart(bool startReporting, const ThreadList& threads);
83 // Stop it
84 extern void RecordStop(const ThreadList& threads);
85 
86 // Get results since last interval -- may be called while running
87 extern BenchmarkIntervalResults ReportIntervalResults(
88     const ThreadList& threads);
89 // Get total results -- must be called after stop
90 extern BenchmarkResults ReportResults();
91 // And clean it up. Don't call before reporting.
92 extern void EndReporting();
93 
94 // Record an error connecting
95 extern void RecordSocketError();
96 // Report any time we open a connection
97 extern void RecordConnectionOpen();
98 
99 // Call ReportResults and print to a file
100 extern void PrintShortResults(std::ostream& out, const std::string& runName,
101                               size_t numThreads, int connections);
102 extern void PrintFullResults(std::ostream& out);
103 // Call ReportIntervalResults and print to a file
104 extern void ReportInterval(std::ostream& out, const ThreadList& threads,
105                            int totalDuration, bool warmup);
106 // If ReportInterval is not being called, call this instead to ensure
107 // that the CPU samples are happening regularly so
108 // that we get a good average.
109 extern void SampleCPU();
110 // Print a CSV header for the "short" reporting format
111 extern void PrintReportingHeader(std::ostream& out);
112 
113 }  // namespace apib
114 
115 #endif  // APIB_REPORTING_H