summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/execution_profiler/execution_profiler_test.cpp
blob: f41aaffd90db6663b07447cfb29d50419b744503 (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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/execution_profiler.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <thread>

using Profiler = vespalib::ExecutionProfiler;
using vespalib::Slime;
using vespalib::slime::Cursor;
using vespalib::slime::Inspector;

void fox(Profiler &profiler) {
    profiler.start(profiler.resolve("fox"));
    std::this_thread::sleep_for(1ms);
    profiler.complete();
}

void baz(Profiler &profiler) {
    profiler.start(profiler.resolve("baz"));
    fox(profiler);
    fox(profiler);
    fox(profiler);
    profiler.complete();
}

void bar(Profiler &profiler) {
    profiler.start(profiler.resolve("bar"));
    baz(profiler);
    fox(profiler);
    baz(profiler);
    fox(profiler);
    profiler.complete();
}

void foo(Profiler &profiler) {
    profiler.start(profiler.resolve("foo"));
    bar(profiler);
    baz(profiler);
    fox(profiler);
    profiler.complete();
}

template <typename PathPos>
bool find_path(const Inspector &self, PathPos pos, PathPos end, bool first = false) {
    const Inspector &children = first ? self["roots"] : self["children"];
    if (pos == end) {
        return (children.entries() == 0);
    }
    auto needle = *pos++;
    for (size_t i = 0; i < children.entries(); ++i) {
        if ((children[i]["name"].asString().make_string() == needle.first) &&
            (children[i]["count"].asLong() == needle.second) &&
            (find_path(children[i], pos, end)))
        {
            return true;
        }
    }
    return false;
}

bool find_path(const Slime &slime, const std::vector<std::pair<vespalib::string,int64_t>> &path) {
    return find_path(slime.get(), path.begin(), path.end(), true);
}

TEST(ExecutionProfilerTest, resolve_names) {
    Profiler profiler(64);
    EXPECT_EQ(profiler.resolve("foo"), 0);
    EXPECT_EQ(profiler.resolve("bar"), 1);
    EXPECT_EQ(profiler.resolve("baz"), 2);
    EXPECT_EQ(profiler.resolve("foo"), 0);
    EXPECT_EQ(profiler.resolve("bar"), 1);
    EXPECT_EQ(profiler.resolve("baz"), 2);
}

TEST(ExecutionProfilerTest, empty_report) {
    Profiler profiler(64);
    profiler.resolve("foo");
    profiler.resolve("bar");
    profiler.resolve("baz");
    Slime slime;
    profiler.report(slime.setObject());
    fprintf(stderr, "%s\n", slime.toString().c_str());
    EXPECT_EQ(slime["roots"].entries(), 0);
    EXPECT_TRUE(find_path(slime, {}));
}

TEST(ExecutionProfilerTest, perform_dummy_profiling) {
    Profiler profiler(64);
    for (int i = 0; i < 3; ++i) {
        foo(profiler);
        bar(profiler);
        baz(profiler);
        fox(profiler);
    }
    Slime slime;
    profiler.report(slime.setObject());
    fprintf(stderr, "%s\n", slime.toString().c_str());
    EXPECT_EQ(slime["roots"].entries(), 4);
    EXPECT_TRUE(find_path(slime, {{"foo", 3}, {"bar", 3}, {"baz", 6}, {"fox", 18}}));
    EXPECT_TRUE(find_path(slime, {{"foo", 3}, {"bar", 3}, {"fox", 6}}));
    EXPECT_TRUE(find_path(slime, {{"foo", 3}, {"baz", 3}, {"fox", 9}}));
    EXPECT_TRUE(find_path(slime, {{"foo", 3}, {"fox", 3}}));
    EXPECT_TRUE(find_path(slime, {{"bar", 3}, {"baz", 6}, {"fox", 18}}));
    EXPECT_TRUE(find_path(slime, {{"bar", 3}, {"fox", 6}}));
    EXPECT_TRUE(find_path(slime, {{"baz", 3}, {"fox", 9}}));
    EXPECT_TRUE(find_path(slime, {{"fox", 3}}));
}

TEST(ExecutionProfilerTest, perform_shallow_dummy_profiling) {
    Profiler profiler(2);
    for (int i = 0; i < 3; ++i) {
        foo(profiler);
        bar(profiler);
        baz(profiler);
        fox(profiler);
    }
    Slime slime;
    profiler.report(slime.setObject());
    fprintf(stderr, "%s\n", slime.toString().c_str());
    EXPECT_EQ(slime["roots"].entries(), 4);
    EXPECT_TRUE(find_path(slime, {{"foo", 3}, {"bar", 3}}));
    EXPECT_TRUE(find_path(slime, {{"foo", 3}, {"bar", 3}}));
    EXPECT_TRUE(find_path(slime, {{"foo", 3}, {"baz", 3}}));
    EXPECT_TRUE(find_path(slime, {{"foo", 3}, {"fox", 3}}));
    EXPECT_TRUE(find_path(slime, {{"bar", 3}, {"baz", 6}}));
    EXPECT_TRUE(find_path(slime, {{"bar", 3}, {"fox", 6}}));
    EXPECT_TRUE(find_path(slime, {{"baz", 3}, {"fox", 9}}));
    EXPECT_TRUE(find_path(slime, {{"fox", 3}}));
}

TEST(ExecutionProfilerTest, with_name_mapping) {
    Profiler profiler(64);
    for (int i = 0; i < 3; ++i) {
        foo(profiler);
        bar(profiler);
        baz(profiler);
        fox(profiler);
    }
    Slime slime;
    profiler.report(slime.setObject(), [](const vespalib::string &name)noexcept->vespalib::string {
                                           if ((name == "foo") || (name == "bar")) {
                                               return "magic";
                                           }
                                           return name;
                                       });
    fprintf(stderr, "%s\n", slime.toString().c_str());
    EXPECT_EQ(slime["roots"].entries(), 4);
    EXPECT_TRUE(find_path(slime, {{"magic", 3}, {"magic", 3}, {"baz", 6}, {"fox", 18}}));
    EXPECT_TRUE(find_path(slime, {{"magic", 3}, {"magic", 3}, {"fox", 6}}));
    EXPECT_TRUE(find_path(slime, {{"magic", 3}, {"baz", 3}, {"fox", 9}}));
    EXPECT_TRUE(find_path(slime, {{"magic", 3}, {"fox", 3}}));
    EXPECT_TRUE(find_path(slime, {{"magic", 3}, {"baz", 6}, {"fox", 18}}));
    EXPECT_TRUE(find_path(slime, {{"magic", 3}, {"fox", 6}}));
    EXPECT_TRUE(find_path(slime, {{"baz", 3}, {"fox", 9}}));
    EXPECT_TRUE(find_path(slime, {{"fox", 3}}));
}

GTEST_MAIN_RUN_ALL_TESTS()