aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/wilson/wilson_profile_span.h
blob: f12747e4ac07286c578ad3858263aa32e859e55c (plain) (blame)
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
#pragma once
#include "wilson_span.h"
#include <library/cpp/json/writer/json_value.h>

namespace NWilson {

class TProfileSpan: public TSpan {
private:
    using TBase = TSpan;
    class TMinMaxPair {
    private:
        std::optional<TInstant> MinMinInstance;
        std::optional<TInstant> MaxMinInstance;
        std::optional<TInstant> MaxInstance;
    public:
        static TMinMaxPair BuildMin(const TInstant value);
        static TMinMaxPair BuildMax(const TInstant value);
        void AddMax(const TInstant instance);
        void AddMin(const TInstant instance);
        TString ToString() const;
    };
    mutable NJson::TJsonValue ResultTimes = NJson::JSON_MAP;
    std::map<TString, TMinMaxPair> PairInstances;
    std::vector<NJson::TJsonValue*> CurrentJsonPath;
    mutable TInstant LastNoGuards = Now();
    const TInstant StartTime = Now();
    bool Enabled = true;

    void FlushNoGuards() const;
    TProfileSpan() = default;
public:
    TProfileSpan(const ui8 verbosity, TTraceId parentId, std::optional<TString> name);
    ~TProfileSpan();

    TProfileSpan BuildChildrenSpan(std::optional<TString> name, const ui8 verbosity = 0) const;

    using TBase::TBase;
    TString ProfileToString() const;

    TProfileSpan& SetEnabled(const bool value) {
        Enabled = value;
        return *this;
    }

    class TGuard {
    private:
        TProfileSpan& Owner;
        const TInstant Start = Now();
        NJson::TJsonValue* CurrentNodeDuration;
    public:
        TGuard(const TString& event, TProfileSpan& owner, const TString& info);
        ~TGuard();
    };

    template <class TEventId, class T = TString>
    TGuard StartStackTimeGuard(const TEventId event, const T& info = Default<T>()) {
        return TGuard(::ToString(event), *this, ::ToString(info));
    }

    template <class TEventId, class T = TString>
    void AddMin(const TEventId event, const T& info = Default<T>()) {
        AddMin(::ToString(event), ::ToString(info));
    }

    template <class TEventId, class T = TString>
    void AddMax(const TEventId event, const T& info = Default<T>()) {
        AddMax(::ToString(event), ::ToString(info));
    }

    void AddMin(const TString& eventId, const TString& info);
    void AddMax(const TString& eventId, const TString& info);

};

} // NWilson