-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeedTest.cpp
More file actions
107 lines (102 loc) · 2.75 KB
/
SpeedTest.cpp
File metadata and controls
107 lines (102 loc) · 2.75 KB
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
#include <chrono>
#include <iosfwd>
#include "SpeedTest.h"
SpeedTestRecord g_speedtestrecord;
SpeedTestData* SpeedTestRecord::Reg(const SpeedTestPosition& testpos)
{
// 记录 name值有效才记录
if (!brecord || !testpos.name)
{
return NULL;
}
// 先用共享锁 如果存在直接修改
{
READ_LOCK(mutex);
auto it = ((const SpeedTestPositionMap&)records).find(testpos); // 显示的调用const的find
if (it != records.cend())
{
return it->second;
}
}
// 不存在构造一个
SpeedTestData* p = new SpeedTestData;
// 使用写锁
{
WRITE_LOCK(mutex);
records.insert(std::make_pair(testpos, p));
}
return p;
}
std::string SpeedTestRecord::Snapshot(SnapshotType type, const std::string& metricsprefix, const std::map<std::string, std::string>& tags)
{
SpeedTestPositionMap lastdata;
{
READ_LOCK(mutex);
lastdata = records;
}
static const int metirs_num = 3;
static const char* metirs[metirs_num] = { "speedtest_calltimes", "speedtest_elapse", "speedtest_maxelapse" };
std::ostringstream ss;
if (type == Json)
{
ss << "[";
int index = 0;
for (const auto& it : lastdata)
{
int64_t value[metirs_num] = { it.second->calltimes, it.second->elapsedTSC / TSCPerUS(), it.second->elapsedMaxTSC / TSCPerUS() };
ss << ((++index) == 1 ? "[" : ",[");
for (int i = 0; i < metirs_num; ++i)
{
ss << (i == 0 ? "{" : ",{");
ss << "\"metrics\":\"" << metricsprefix << metirs[i] << "\",";
ss << "\"name\":\"" << it.first.name << "\",";
ss << "\"num\":" << it.first.num << ",";
for (const auto& it : tags)
{
ss << "\"" << it.first << "\":\"" << it.second << "\",";
}
ss << "\"value\":" << value[i] << "";
ss << "}";
}
ss << "]";
}
ss << "]";
}
else if (type == Influx)
{
std::string tag;
for (const auto& t : tags)
{
tag += ("," + t.first + "=" + t.second);
}
for (const auto& it : lastdata)
{
int64_t value[metirs_num] = { it.second->calltimes, it.second->elapsedTSC / TSCPerUS(), it.second->elapsedMaxTSC / TSCPerUS() };
for (int i = 0; i < metirs_num; ++i)
{
ss << metricsprefix << metirs[i];
ss << ",name=" << it.first.name << ",num=" << it.first.num << tag;
ss << " value=" << value[i] << "i\n";
}
}
}
else if (type == Prometheus)
{
std::string tag;
for (const auto& t : tags)
{
tag += ("," + t.first + "=\"" + t.second + "\"");
}
for (const auto& it : lastdata)
{
int64_t value[metirs_num] = { it.second->calltimes, it.second->elapsedTSC / TSCPerUS(), it.second->elapsedMaxTSC / TSCPerUS() };
for (int i = 0; i < metirs_num; ++i)
{
ss << metricsprefix << metirs[i];
ss << "{name=\"" << it.first.name << "\",num=\"" << it.first.num << "\"" << tag << "}";
ss << " " << value[i] << "\n";
}
}
}
return ss.str();
}