|
9 | 9 | #include "external/envoy/source/common/common/assert.h"
|
10 | 10 | #include "external/envoy/source/common/protobuf/utility.h"
|
11 | 11 |
|
| 12 | +#include "internal_proto/statistic/statistic.pb.h" |
| 13 | + |
12 | 14 | namespace Nighthawk {
|
13 | 15 |
|
14 | 16 | namespace {
|
@@ -104,6 +106,38 @@ StatisticPtr SimpleStatistic::combine(const Statistic& statistic) const {
|
104 | 106 | return combined;
|
105 | 107 | }
|
106 | 108 |
|
| 109 | +absl::StatusOr<std::unique_ptr<std::istream>> SimpleStatistic::serializeNative() const { |
| 110 | + nighthawk::internal::SimpleStatistic proto; |
| 111 | + proto.set_id(id()); |
| 112 | + proto.set_count(count()); |
| 113 | + proto.set_min(min()); |
| 114 | + proto.set_max(max()); |
| 115 | + proto.set_sum_x(sum_x_); |
| 116 | + proto.set_sum_x_2(sum_x2_); |
| 117 | + |
| 118 | + std::string tmp; |
| 119 | + proto.SerializeToString(&tmp); |
| 120 | + auto write_stream = std::make_unique<std::stringstream>(); |
| 121 | + *write_stream << tmp; |
| 122 | + return write_stream; |
| 123 | +} |
| 124 | + |
| 125 | +absl::Status SimpleStatistic::deserializeNative(std::istream& stream) { |
| 126 | + nighthawk::internal::SimpleStatistic proto; |
| 127 | + std::string tmp(std::istreambuf_iterator<char>(stream), {}); |
| 128 | + if (!proto.ParseFromString(tmp)) { |
| 129 | + ENVOY_LOG(error, "Failed to read back SimpleStatistic data."); |
| 130 | + return absl::Status(absl::StatusCode::kInternal, "Failed to read back SimpleStatistic data"); |
| 131 | + } |
| 132 | + id_ = proto.id(); |
| 133 | + count_ = proto.count(); |
| 134 | + min_ = proto.min(); |
| 135 | + max_ = proto.max(); |
| 136 | + sum_x_ = proto.sum_x(); |
| 137 | + sum_x2_ = proto.sum_x_2(); |
| 138 | + return absl::OkStatus(); |
| 139 | +} |
| 140 | + |
107 | 141 | void StreamingStatistic::addValue(uint64_t value) {
|
108 | 142 | double delta, delta_n;
|
109 | 143 | StatisticImpl::addValue(value);
|
@@ -142,6 +176,38 @@ StatisticPtr StreamingStatistic::combine(const Statistic& statistic) const {
|
142 | 176 | return combined;
|
143 | 177 | }
|
144 | 178 |
|
| 179 | +absl::StatusOr<std::unique_ptr<std::istream>> StreamingStatistic::serializeNative() const { |
| 180 | + nighthawk::internal::StreamingStatistic proto; |
| 181 | + proto.set_id(id()); |
| 182 | + proto.set_count(count()); |
| 183 | + proto.set_min(min()); |
| 184 | + proto.set_max(max()); |
| 185 | + proto.set_mean(mean_); |
| 186 | + proto.set_accumulated_variance(accumulated_variance_); |
| 187 | + |
| 188 | + std::string tmp; |
| 189 | + proto.SerializeToString(&tmp); |
| 190 | + auto write_stream = std::make_unique<std::stringstream>(); |
| 191 | + *write_stream << tmp; |
| 192 | + return write_stream; |
| 193 | +} |
| 194 | + |
| 195 | +absl::Status StreamingStatistic::deserializeNative(std::istream& stream) { |
| 196 | + nighthawk::internal::StreamingStatistic proto; |
| 197 | + std::string tmp(std::istreambuf_iterator<char>(stream), {}); |
| 198 | + if (!proto.ParseFromString(tmp)) { |
| 199 | + ENVOY_LOG(error, "Failed to read back StreamingStatistic data."); |
| 200 | + return absl::Status(absl::StatusCode::kInternal, "Failed to read back StreamingStatistic data"); |
| 201 | + } |
| 202 | + id_ = proto.id(); |
| 203 | + count_ = proto.count(); |
| 204 | + min_ = proto.min(); |
| 205 | + max_ = proto.max(); |
| 206 | + mean_ = proto.mean(); |
| 207 | + accumulated_variance_ = proto.accumulated_variance(); |
| 208 | + return absl::OkStatus(); |
| 209 | +} |
| 210 | + |
145 | 211 | InMemoryStatistic::InMemoryStatistic() : streaming_stats_(std::make_unique<StreamingStatistic>()) {}
|
146 | 212 |
|
147 | 213 | void InMemoryStatistic::addValue(uint64_t sample_value) {
|
|
0 commit comments