1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_base/event_tracer.h"
12
13 #include "rtc_base/synchronization/mutex.h"
14 #include "rtc_base/thread_annotations.h"
15 #include "rtc_base/trace_event.h"
16 #include "test/gtest.h"
17
18 namespace {
19
20 class TestStatistics {
21 public:
Reset()22 void Reset() {
23 webrtc::MutexLock lock(&mutex_);
24 events_logged_ = 0;
25 }
26
Increment()27 void Increment() {
28 webrtc::MutexLock lock(&mutex_);
29 ++events_logged_;
30 }
31
Count() const32 int Count() const {
33 webrtc::MutexLock lock(&mutex_);
34 return events_logged_;
35 }
36
Get()37 static TestStatistics* Get() {
38 // google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
39 static auto& test_stats = *new TestStatistics();
40 return &test_stats;
41 }
42
43 private:
44 mutable webrtc::Mutex mutex_;
45 int events_logged_ RTC_GUARDED_BY(mutex_) = 0;
46 };
47
48 } // namespace
49
50 namespace webrtc {
51
TEST(EventTracerTest,EventTracerDisabled)52 TEST(EventTracerTest, EventTracerDisabled) {
53 { TRACE_EVENT0("test", "EventTracerDisabled"); }
54 EXPECT_FALSE(TestStatistics::Get()->Count());
55 TestStatistics::Get()->Reset();
56 }
57
58 #if RTC_TRACE_EVENTS_ENABLED
TEST(EventTracerTest,ScopedTraceEvent)59 TEST(EventTracerTest, ScopedTraceEvent) {
60 SetupEventTracer(
61 [](const char* /*name*/) {
62 return reinterpret_cast<const unsigned char*>("test");
63 },
64 [](char /*phase*/,
65 const unsigned char* /*category_enabled*/,
66 const char* /*name*/,
67 unsigned long long /*id*/,
68 int /*num_args*/,
69 const char** /*arg_names*/,
70 const unsigned char* /*arg_types*/,
71 const unsigned long long* /*arg_values*/,
72 unsigned char /*flags*/) {
73 TestStatistics::Get()->Increment();
74 });
75 { TRACE_EVENT0("test", "ScopedTraceEvent"); }
76 EXPECT_EQ(2, TestStatistics::Get()->Count());
77 TestStatistics::Get()->Reset();
78 }
79 #endif
80
81 } // namespace webrtc
82