Skip to content

Commit 9294a11

Browse files
committed
tidy(trace): 简化to_json代码
1 parent 1d6dfd2 commit 9294a11

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

modules/trace/tools/to_json/main.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ using namespace tbox;
3232

3333
//! 统计
3434
struct Stat {
35+
uint64_t name_index = 0;
36+
uint64_t module_index = 0;
37+
3538
size_t times = 0; //! 次数
3639
uint64_t dur_acc_us = 0; //! 累积时长
3740
uint64_t dur_min_us = std::numeric_limits<uint64_t>::max(); //! 最小时长
@@ -212,20 +215,15 @@ int main(int argc, char **argv)
212215
//! 第一次遍历记录文件
213216
ReadAllRecordFiles(records_dir, record_file_name_vec,
214217
[&] (uint64_t start_ts_us, uint64_t duration_us, uint64_t name_index, uint64_t module_index, uint64_t thread_index) {
215-
std::string name = "unknown-name", thread = "unknown-thread", module = "unknown-module";
216-
217-
if (name_index < name_vec.size())
218-
name = name_vec[name_index];
219-
220-
if (thread_index < thread_vec.size())
221-
thread = thread_vec[thread_index];
222-
223-
if (module_index < module_vec.size())
224-
module = module_vec[module_index];
218+
auto &name = name_vec.at(name_index);
219+
auto &module = module_vec.at(module_index);
220+
auto &thread = thread_vec.at(thread_index);
225221

226222
writer.writeRecorder(name, module, thread, start_ts_us, duration_us);
227223

228224
auto &stat = stat_vec.at(name_index);
225+
stat.name_index = name_index;
226+
stat.module_index = module_index;
229227
++stat.times;
230228
stat.dur_acc_us += duration_us;
231229
if (stat.dur_max_us < duration_us) {
@@ -252,25 +250,19 @@ int main(int argc, char **argv)
252250
if (duration_us < stat.dur_warn_line_us)
253251
return;
254252

255-
std::string name = "unknown-name", module = "unknown-module";
256-
257-
if (name_index < name_vec.size())
258-
name = name_vec[name_index];
259-
260-
if (module_index < module_vec.size())
261-
module = module_vec[module_index];
253+
auto &name = name_vec.at(name_index);
254+
auto &module = module_vec.at(module_index);
262255

263256
++stat.dur_warn_count;
264257
writer.writeRecorder(name, module, "WARN", start_ts_us, duration_us);
265258
}
266259
);
267260

268261
//! 标记出最大时间点
269-
auto size = name_vec.size();
270-
for (size_t i = 0; i < size; ++i) {
271-
auto &name = name_vec.at(i);
272-
auto &stat = stat_vec.at(i);
273-
writer.writeRecorder(name, "", "MAX", stat.dur_max_ts_us, stat.dur_max_us);
262+
for (auto &stat : stat_vec) {
263+
auto &name = name_vec.at(stat.name_index);
264+
auto &module = module_vec.at(stat.module_index);
265+
writer.writeRecorder(name, module, "MAX", stat.dur_max_ts_us, stat.dur_max_us);
274266
}
275267

276268
writer.writeFooter();

0 commit comments

Comments
 (0)