forked from facebook/fbthrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_metrics.cpp
135 lines (110 loc) · 3.56 KB
/
log_metrics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <fatal/type/cat.h>
#include <folly/Conv.h>
#include <folly/init/Init.h>
#include <thrift/lib/cpp2/reflection/demo/gen-cpp2/metrics_fatal_types.h>
#include <thrift/lib/cpp2/reflection/reflection.h>
using namespace apache::thrift;
using namespace static_reflection::demo;
struct metrics_client {
template <typename Metric, typename Value>
void add(Metric const& name, Value const& value) {
metrics_[name] += value;
}
void report() {
for (auto const& i : metrics_) {
std::cout << i.first << ": " << i.second << '\n';
}
}
private:
std::unordered_map<std::string, std::intmax_t> metrics_;
};
// dynamic //
struct export_metric_dynamic {
template <typename Member, std::size_t Index, typename T>
void operator()(
fatal::indexed<Member, Index>,
metrics_client& sink,
T const& metrics,
std::string const& prefix) const {
auto const key = prefix + fatal::z_data<typename Member::name>();
auto const& value = Member::getter::ref(metrics);
sink.add(key, value);
}
};
std::string const kPrefix = "my_app.";
template <typename T>
void export_metrics_dynamic(metrics_client& sink, T const& metrics) {
using info = reflect_struct<T>;
fatal::foreach<typename info::members>(
export_metric_dynamic(),
sink,
metrics,
folly::to<std::string>(
kPrefix, fatal::z_data<typename info::name>(), '.'));
}
// static //
struct export_metric_static {
FATAL_S(prefix, "my_app");
FATAL_S(dot, ".");
template <typename Member, std::size_t Index, typename T>
void operator()(
fatal::indexed<Member, Index>,
metrics_client& sink,
T const& metrics) const {
using info = reflect_struct<T>;
using key = fatal::
cat<prefix, dot, typename info::name, dot, typename Member::name>;
auto const& value = Member::getter::ref(metrics);
sink.add(fatal::z_data<key>(), value);
}
};
template <typename T>
void export_metrics_static(metrics_client& sink, T const& metrics) {
fatal::foreach<typename reflect_struct<T>::members>(
export_metric_static(), sink, metrics);
}
// driver //
int main(int argc, char** argv) {
folly::init(&argc, &argv);
metrics_client sink;
host_info metric;
metric.cpu = 1;
metric.memory = 12;
metric.disk = 1234;
metric.network_bytes_sent = 123;
metric.network_bytes_received = 321;
metric.network_retransmits = 2;
metric.network_connection_reset = 3;
metric.network_packets_dropped = 1;
int iterations = argc > 2 ? atoi(argv[2]) : 1000000;
// try running this binary through `perf stat` for both versions
if (argc > 1 && argv[1] == std::string("dynamic")) {
std::cout << "dynamic version, " << iterations << " iterations\n";
while (iterations--) {
export_metrics_dynamic(sink, metric);
}
} else {
std::cout << "static version, " << iterations << " iterations\n";
while (iterations--) {
export_metrics_static(sink, metric);
}
}
sink.report();
return 0;
}