aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/library/ytprof/example/main.cpp
blob: bc9dec690a1af31a1d83776237b2dc30760fe317 (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
#include <yt/yt/core/concurrency/poller.h>
#include <yt/yt/core/concurrency/thread_pool_poller.h>
#include <yt/yt/core/concurrency/action_queue.h>
#include <yt/yt/core/concurrency/thread_pool.h>
#include <yt/yt/core/http/server.h>

#include <yt/yt/library/ytprof/http/handler.h>
#include <yt/yt/library/ytprof/heap_profiler.h>

#include <absl/debugging/stacktrace.h>

using namespace NYT;
using namespace NYT::NHttp;
using namespace NYT::NConcurrency;
using namespace NYT::NYTProf;

int main(int argc, char* argv[])
{
    absl::SetStackUnwinder(AbslStackUnwinder);
    tcmalloc::MallocExtension::SetProfileSamplingRate(2_MB);

    try {
        if (argc != 2 && argc != 3) {
            throw yexception() << "usage: " << argv[0] << " PORT";
        }

        auto port = FromString<int>(argv[1]);
        auto poller = CreateThreadPoolPoller(1, "Example");
        auto server = CreateServer(port, poller);

        Register(server, "");
        server->Start();

        THashMap<TString, std::vector<int>> data;
        for (int i = 0; i < 1024 * 16; i++) {
            data[ToString(i)].resize(1024);
        }

        auto burnCpu = [] {
            ui64 value = 0;
            while (true) {
                THash<TString> hasher;
                for (int i = 0; i < 10000000; i++) {
                    value += hasher(ToString(i));
                }

                std::vector<TString> data;
                for (int i = 0; i < 10000; i++) {
                    data.push_back(TString(1024, 'x'));
                }

                if (value == 1) {
                    Sleep(TDuration::Seconds(1));
                }
            }
        };

        auto pool = CreateThreadPool(64, "Pool");
        for (int i = 0; i < 64; i++) {
            pool->GetInvoker()->Invoke(BIND(burnCpu));
        }

        burnCpu();
    } catch (const std::exception& ex) {
        Cerr << ex.what() << Endl;
        _exit(1);
    }

    return 0;
}