forked from Tencent/Pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat.cpp
More file actions
144 lines (113 loc) · 4.4 KB
/
Copy pathstat.cpp
File metadata and controls
144 lines (113 loc) · 4.4 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
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
136
137
138
139
140
141
142
143
/*
* Tencent is pleased to support the open source community by making Pebble available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 "framework/stat.h"
namespace pebble {
void Stat::Clear() {
m_message_counts = 0;
m_failure_message_counts = 0;
m_resource_stat_temp.clear();
m_message_stat_temp.clear();
m_resource_stat_result.clear();
m_message_stat_result.clear();
}
int32_t Stat::AddResourceItem(const std::string& name, float value) {
if (name.empty()) {
return -1;
}
ResourceStatItem& item = m_resource_stat_result[name];
ResourceStatTempData& temp = m_resource_stat_temp[name];
temp._result = &item;
temp._count++;
temp._total_value += value;
value > item._max_value ? item._max_value = value : value;
value < item._min_value ? item._min_value = value : value;
return 0;
}
int32_t Stat::AddMessageItem(const std::string& name, int32_t result, int32_t time_cost_ms) {
if (name.empty()) {
return -1;
}
m_message_counts++;
MessageStatItem& item = m_message_stat_result[name];
MessageStatTempData& temp = m_message_stat_temp[name];
temp._result = &item;
temp._total_count++;
if (result != 0) {
temp._failure_count++;
m_failure_message_counts++;
}
temp._total_cost_ms += time_cost_ms;
uint32_t time_cost = static_cast<uint32_t>(time_cost_ms);
time_cost > item._max_cost_ms ? item._max_cost_ms = time_cost : time_cost;
time_cost < item._min_cost_ms ? item._min_cost_ms = time_cost : time_cost;
item._result[result]++;
return 0;
}
int32_t Stat::AddMessageItem(const std::string& name) {
if (name.empty()) {
return -1;
}
MessageStatItem& item = m_message_stat_result[name];
MessageStatTempData& temp = m_message_stat_temp[name];
temp._result = &item;
item._result[0];
return 0;
}
const ResourceStatItem* Stat::GetResourceResultByName(const std::string& name) {
cxx::unordered_map<std::string, ResourceStatTempData>::iterator it;
it = m_resource_stat_temp.find(name);
if (m_resource_stat_temp.end() == it) {
return NULL;
}
CalculateResourceStatResult(&(it->second));
return it->second._result;
}
const MessageStatItem* Stat::GetMessageResultByName(const std::string& name) {
cxx::unordered_map<std::string, MessageStatTempData>::iterator it;
it = m_message_stat_temp.find(name);
if (m_message_stat_temp.end() == it) {
return NULL;
}
CalculateMessageStatResult(&(it->second));
return it->second._result;
}
const ResourceStatResult* Stat::GetAllResourceResults() {
cxx::unordered_map<std::string, ResourceStatTempData>::iterator it;
for (it = m_resource_stat_temp.begin(); it != m_resource_stat_temp.end(); ++it) {
CalculateResourceStatResult(&(it->second));
}
return &m_resource_stat_result;
}
const MessageStatResult* Stat::GetAllMessageResults() {
cxx::unordered_map<std::string, MessageStatTempData>::iterator it;
for (it = m_message_stat_temp.begin(); it != m_message_stat_temp.end(); ++it) {
CalculateMessageStatResult(&(it->second));
}
return &m_message_stat_result;
}
void Stat::CalculateResourceStatResult(ResourceStatTempData* resource_stat_temp) {
if (0 == resource_stat_temp->_count) {
return;
}
ResourceStatItem* result = resource_stat_temp->_result;
result->_average_value = resource_stat_temp->_total_value / resource_stat_temp->_count;
}
void Stat::CalculateMessageStatResult(MessageStatTempData* message_stat_temp) {
if (0 == message_stat_temp->_total_count) {
return;
}
MessageStatItem* result = message_stat_temp->_result;
result->_failure_rate = message_stat_temp->_failure_count / message_stat_temp->_total_count;
result->_average_cost_ms = message_stat_temp->_total_cost_ms / message_stat_temp->_total_count;
}
} // namespace pebble