1 | #include "TestStats.h" |
2 | #include <algorithm> |
3 | namespace DB |
4 | { |
5 | |
6 | namespace |
7 | { |
8 | const std::string FOUR_SPACES = " " ; |
9 | } |
10 | |
11 | std::string TestStats::getStatisticByName(const std::string & statistic_name) |
12 | { |
13 | if (statistic_name == "min_time" ) |
14 | return std::to_string(min_time) + "ms" ; |
15 | |
16 | if (statistic_name == "quantiles" ) |
17 | { |
18 | std::string result = "\n" ; |
19 | |
20 | for (double percent = 10; percent <= 90; percent += 10) |
21 | { |
22 | result += FOUR_SPACES + std::to_string((percent / 100)); |
23 | result += ": " + std::to_string(sampler.quantileInterpolated(percent / 100.0)); |
24 | result += "\n" ; |
25 | } |
26 | result += FOUR_SPACES + "0.95: " + std::to_string(sampler.quantileInterpolated(95 / 100.0)) + "\n" ; |
27 | result += FOUR_SPACES + "0.99: " + std::to_string(sampler.quantileInterpolated(99 / 100.0)) + "\n" ; |
28 | result += FOUR_SPACES + "0.999: " + std::to_string(sampler.quantileInterpolated(99.9 / 100.)) + "\n" ; |
29 | result += FOUR_SPACES + "0.9999: " + std::to_string(sampler.quantileInterpolated(99.99 / 100.)); |
30 | |
31 | return result; |
32 | } |
33 | if (statistic_name == "total_time" ) |
34 | return std::to_string(total_time) + "s" ; |
35 | |
36 | if (statistic_name == "queries_per_second" ) |
37 | return std::to_string(queries / total_time); |
38 | |
39 | if (statistic_name == "rows_per_second" ) |
40 | return std::to_string(total_rows_read / total_time); |
41 | |
42 | if (statistic_name == "bytes_per_second" ) |
43 | return std::to_string(total_bytes_read / total_time); |
44 | |
45 | if (statistic_name == "max_rows_per_second" ) |
46 | return std::to_string(max_rows_speed); |
47 | |
48 | if (statistic_name == "max_bytes_per_second" ) |
49 | return std::to_string(max_bytes_speed); |
50 | |
51 | if (statistic_name == "avg_rows_per_second" ) |
52 | return std::to_string(avg_rows_speed_value); |
53 | |
54 | if (statistic_name == "avg_bytes_per_second" ) |
55 | return std::to_string(avg_bytes_speed_value); |
56 | |
57 | return "" ; |
58 | } |
59 | |
60 | |
61 | void TestStats::update_min_time(UInt64 min_time_candidate) |
62 | { |
63 | if (min_time_candidate < min_time) |
64 | { |
65 | min_time = min_time_candidate; |
66 | min_time_watch.restart(); |
67 | } |
68 | } |
69 | |
70 | void TestStats::update_max_speed( |
71 | size_t max_speed_candidate, |
72 | Stopwatch & max_speed_watch, |
73 | UInt64 & max_speed) |
74 | { |
75 | if (max_speed_candidate > max_speed) |
76 | { |
77 | max_speed = max_speed_candidate; |
78 | max_speed_watch.restart(); |
79 | } |
80 | } |
81 | |
82 | |
83 | void TestStats::update_average_speed( |
84 | double new_speed_info, |
85 | Stopwatch & avg_speed_watch, |
86 | size_t & number_of_info_batches, |
87 | double precision, |
88 | double & avg_speed_first, |
89 | double & avg_speed_value) |
90 | { |
91 | avg_speed_value = ((avg_speed_value * number_of_info_batches) + new_speed_info); |
92 | ++number_of_info_batches; |
93 | avg_speed_value /= number_of_info_batches; |
94 | |
95 | if (avg_speed_first == 0) |
96 | avg_speed_first = avg_speed_value; |
97 | |
98 | auto [min, max] = std::minmax(avg_speed_value, avg_speed_first); |
99 | if (1 - min / max >= precision) |
100 | { |
101 | avg_speed_first = avg_speed_value; |
102 | avg_speed_watch.restart(); |
103 | } |
104 | } |
105 | |
106 | void TestStats::add(size_t rows_read_inc, size_t bytes_read_inc) |
107 | { |
108 | total_rows_read += rows_read_inc; |
109 | total_bytes_read += bytes_read_inc; |
110 | last_query_rows_read += rows_read_inc; |
111 | last_query_bytes_read += bytes_read_inc; |
112 | |
113 | double new_rows_speed = last_query_rows_read / watch_per_query.elapsedSeconds(); |
114 | double new_bytes_speed = last_query_bytes_read / watch_per_query.elapsedSeconds(); |
115 | |
116 | /// Update rows speed |
117 | update_max_speed(new_rows_speed, max_rows_speed_watch, max_rows_speed); |
118 | update_average_speed(new_rows_speed, |
119 | avg_rows_speed_watch, |
120 | number_of_rows_speed_info_batches, |
121 | avg_rows_speed_precision, |
122 | avg_rows_speed_first, |
123 | avg_rows_speed_value); |
124 | /// Update bytes speed |
125 | update_max_speed(new_bytes_speed, max_bytes_speed_watch, max_bytes_speed); |
126 | update_average_speed(new_bytes_speed, |
127 | avg_bytes_speed_watch, |
128 | number_of_bytes_speed_info_batches, |
129 | avg_bytes_speed_precision, |
130 | avg_bytes_speed_first, |
131 | avg_bytes_speed_value); |
132 | } |
133 | |
134 | void TestStats::updateQueryInfo() |
135 | { |
136 | ++queries; |
137 | sampler.insert(watch_per_query.elapsedSeconds()); |
138 | update_min_time(watch_per_query.elapsed() / (1000 * 1000)); /// ns to ms |
139 | } |
140 | |
141 | |
142 | TestStats::TestStats() |
143 | { |
144 | watch.reset(); |
145 | watch_per_query.reset(); |
146 | min_time_watch.reset(); |
147 | max_rows_speed_watch.reset(); |
148 | max_bytes_speed_watch.reset(); |
149 | avg_rows_speed_watch.reset(); |
150 | avg_bytes_speed_watch.reset(); |
151 | } |
152 | |
153 | |
154 | void TestStats::startWatches() |
155 | { |
156 | watch.start(); |
157 | watch_per_query.start(); |
158 | min_time_watch.start(); |
159 | max_rows_speed_watch.start(); |
160 | max_bytes_speed_watch.start(); |
161 | avg_rows_speed_watch.start(); |
162 | avg_bytes_speed_watch.start(); |
163 | } |
164 | |
165 | } |
166 | |