1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * \author guonix
17 * \date Dec 2020
18 * \brief
19 */
20
21#include "common/profiler.h"
22#include <gtest/gtest.h>
23
24using namespace proxima::be;
25
26TEST(ProfilerTest, TestDisabledProfiler) {
27 Profiler profiler(false);
28
29 EXPECT_FALSE(profiler.enabled());
30 profiler.start();
31 EXPECT_EQ(profiler.open_stage("abc"), 0);
32 EXPECT_EQ(profiler.close_stage(), 0);
33 EXPECT_EQ(profiler.add("abc", 10), 0);
34 profiler.stop();
35 EXPECT_EQ(profiler.as_json_string(), std::string("{}"));
36}
37
38TEST(ProfilerTest, TestEnabledProfiler) {
39 Profiler profiler(true);
40
41 EXPECT_TRUE(profiler.enabled());
42 profiler.start();
43 EXPECT_EQ(profiler.open_stage("abc"), 0);
44 EXPECT_EQ(profiler.close_stage(), 0);
45 EXPECT_EQ(profiler.add("abc", 10), 0);
46 EXPECT_EQ(profiler.close_stage(), 0);
47 EXPECT_TRUE(profiler.close_stage() != 0);
48 EXPECT_TRUE(profiler.open_stage("def") != 0);
49 profiler.start();
50 EXPECT_EQ(profiler.open_stage("def"), 0);
51 profiler.stop();
52
53 std::string json_str = profiler.as_json_string();
54 EXPECT_TRUE(json_str.size() != 0);
55 ailego::JsonValue val = ailego::JsonValue();
56
57 ailego::JsonValue tmp;
58 EXPECT_TRUE(tmp.parse(json_str));
59}