blob: f4d41e4e7c748087d0e9e6c4fe98e1e88e7b96d0 [file] [log] [blame]
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/event_tracer.h"
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000012
Markus Handell18523c32020-07-08 17:55:58 +020013#include "rtc_base/synchronization/mutex.h"
Yves Gerey50b0baf2019-09-06 10:03:54 +020014#include "rtc_base/thread_annotations.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/trace_event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "test/gtest.h"
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000017
18namespace {
19
20class TestStatistics {
21 public:
Yves Gerey50b0baf2019-09-06 10:03:54 +020022 void Reset() {
Markus Handell18523c32020-07-08 17:55:58 +020023 webrtc::MutexLock lock(&mutex_);
Yves Gerey50b0baf2019-09-06 10:03:54 +020024 events_logged_ = 0;
25 }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000026
Yves Gerey50b0baf2019-09-06 10:03:54 +020027 void Increment() {
Markus Handell18523c32020-07-08 17:55:58 +020028 webrtc::MutexLock lock(&mutex_);
Yves Gerey50b0baf2019-09-06 10:03:54 +020029 ++events_logged_;
30 }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000031
Yves Gerey50b0baf2019-09-06 10:03:54 +020032 int Count() const {
Markus Handell18523c32020-07-08 17:55:58 +020033 webrtc::MutexLock lock(&mutex_);
Yves Gerey50b0baf2019-09-06 10:03:54 +020034 return events_logged_;
35 }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000036
37 static TestStatistics* Get() {
Yves Gerey50b0baf2019-09-06 10:03:54 +020038 // google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
39 static auto& test_stats = *new TestStatistics();
40 return &test_stats;
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000041 }
42
43 private:
Markus Handell18523c32020-07-08 17:55:58 +020044 mutable webrtc::Mutex mutex_;
45 int events_logged_ RTC_GUARDED_BY(mutex_) = 0;
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000046};
47
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000048} // namespace
49
50namespace webrtc {
51
52TEST(EventTracerTest, EventTracerDisabled) {
Yves Gerey665174f2018-06-19 15:03:05 +020053 { TRACE_EVENT0("test", "EventTracerDisabled"); }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000054 EXPECT_FALSE(TestStatistics::Get()->Count());
55 TestStatistics::Get()->Reset();
56}
57
Doudou Kisabaka2dec4962019-11-28 14:24:31 +010058#if RTC_TRACE_EVENTS_ENABLED
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000059TEST(EventTracerTest, ScopedTraceEvent) {
Doudou Kisabaka2dec4962019-11-28 14:24:31 +010060 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 });
Yves Gerey665174f2018-06-19 15:03:05 +020075 { TRACE_EVENT0("test", "ScopedTraceEvent"); }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000076 EXPECT_EQ(2, TestStatistics::Get()->Count());
77 TestStatistics::Get()->Reset();
78}
Doudou Kisabaka2dec4962019-11-28 14:24:31 +010079#endif
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000080
81} // namespace webrtc