TimeRecorder.cpp 2.3 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "TimeRecorder.h"
G
groot 已提交
7
#include "utils/Log.h"
G
groot 已提交
8 9 10


namespace zilliz {
J
jinhai 已提交
11
namespace milvus {
G
groot 已提交
12 13 14 15
namespace server {

TimeRecorder::TimeRecorder(const std::string &header,
                           int64_t log_level) :
S
starlord 已提交
16 17
    header_(header),
    log_level_(log_level) {
G
groot 已提交
18 19 20
    start_ = last_ = stdclock::now();
}

S
starlord 已提交
21 22
TimeRecorder::~TimeRecorder() {
}
G
groot 已提交
23

S
starlord 已提交
24 25 26 27
std::string
TimeRecorder::GetTimeSpanStr(double span) {
    std::string str_sec = std::to_string(span * 0.000001) + ((span > 1000000) ? " seconds" : " second");
    std::string str_ms = std::to_string(span * 0.001) + " ms";
G
groot 已提交
28

S
starlord 已提交
29
    return str_sec + " [" + str_ms + "]";
G
groot 已提交
30 31 32 33
}

void
TimeRecorder::PrintTimeRecord(const std::string &msg, double span) {
S
starlord 已提交
34 35 36 37 38 39
    std::string str_log;
    if (!header_.empty()) str_log += header_ + ": ";
    str_log += msg;
    str_log += " (";
    str_log += TimeRecorder::GetTimeSpanStr(span);
    str_log += ")";
G
groot 已提交
40 41 42

    switch (log_level_) {
        case 0: {
S
starlord 已提交
43
            SERVER_LOG_TRACE << str_log;
G
groot 已提交
44 45 46
            break;
        }
        case 1: {
S
starlord 已提交
47
            SERVER_LOG_DEBUG << str_log;
G
groot 已提交
48 49 50
            break;
        }
        case 2: {
S
starlord 已提交
51
            SERVER_LOG_INFO << str_log;
G
groot 已提交
52 53 54
            break;
        }
        case 3: {
S
starlord 已提交
55
            SERVER_LOG_WARNING << str_log;
G
groot 已提交
56 57 58
            break;
        }
        case 4: {
S
starlord 已提交
59
            SERVER_LOG_ERROR << str_log;
G
groot 已提交
60 61 62
            break;
        }
        case 5: {
S
starlord 已提交
63
            SERVER_LOG_FATAL << str_log;
G
groot 已提交
64 65 66
            break;
        }
        default: {
S
starlord 已提交
67
            SERVER_LOG_INFO << str_log;
G
groot 已提交
68 69 70 71 72
            break;
        }
    }
}

S
starlord 已提交
73 74
double
TimeRecorder::RecordSection(const std::string &msg) {
G
groot 已提交
75
    stdclock::time_point curr = stdclock::now();
S
starlord 已提交
76
    double span = (std::chrono::duration<double, std::micro>(curr - last_)).count();
G
groot 已提交
77 78
    last_ = curr;

S
starlord 已提交
79 80
    PrintTimeRecord(msg, span);
    return span;
G
groot 已提交
81 82
}

S
starlord 已提交
83 84
double
TimeRecorder::ElapseFromBegin(const std::string &msg) {
G
groot 已提交
85
    stdclock::time_point curr = stdclock::now();
S
starlord 已提交
86
    double span = (std::chrono::duration<double, std::micro>(curr - start_)).count();
G
groot 已提交
87

S
starlord 已提交
88 89
    PrintTimeRecord(msg, span);
    return span;
G
groot 已提交
90 91 92 93 94
}

}
}
}