blob: d0783c3f2df7b9e9f8416552157b0e4f5bf15ec7 [file] [log] [blame]
tommi@webrtc.org7c64ed22015-03-17 14:25:371/*
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 04:47:3111#include "rtc_base/event_tracer.h"
tommi@webrtc.org7c64ed22015-03-17 14:25:3712
Markus Handell18523c32020-07-08 15:55:5813#include "rtc_base/synchronization/mutex.h"
Yves Gerey50b0baf2019-09-06 08:03:5414#include "rtc_base/thread_annotations.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "rtc_base/trace_event.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "test/gtest.h"
tommi@webrtc.org7c64ed22015-03-17 14:25:3717
18namespace {
19
20class TestStatistics {
21 public:
Yves Gerey50b0baf2019-09-06 08:03:5422 void Reset() {
Markus Handell18523c32020-07-08 15:55:5823 webrtc::MutexLock lock(&mutex_);
Yves Gerey50b0baf2019-09-06 08:03:5424 events_logged_ = 0;
25 }
tommi@webrtc.org7c64ed22015-03-17 14:25:3726
Yves Gerey50b0baf2019-09-06 08:03:5427 void Increment() {
Markus Handell18523c32020-07-08 15:55:5828 webrtc::MutexLock lock(&mutex_);
Yves Gerey50b0baf2019-09-06 08:03:5429 ++events_logged_;
30 }
tommi@webrtc.org7c64ed22015-03-17 14:25:3731
Yves Gerey50b0baf2019-09-06 08:03:5432 int Count() const {
Markus Handell18523c32020-07-08 15:55:5833 webrtc::MutexLock lock(&mutex_);
Yves Gerey50b0baf2019-09-06 08:03:5434 return events_logged_;
35 }
tommi@webrtc.org7c64ed22015-03-17 14:25:3736
37 static TestStatistics* Get() {
Yves Gerey50b0baf2019-09-06 08:03:5438 // 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:3741 }
42
43 private:
Markus Handell18523c32020-07-08 15:55:5844 mutable webrtc::Mutex mutex_;
45 int events_logged_ RTC_GUARDED_BY(mutex_) = 0;
tommi@webrtc.org7c64ed22015-03-17 14:25:3746};
47
tommi@webrtc.org7c64ed22015-03-17 14:25:3748} // namespace
49
50namespace webrtc {
51
52TEST(EventTracerTest, EventTracerDisabled) {
Yves Gerey665174f2018-06-19 13:03:0553 { TRACE_EVENT0("test", "EventTracerDisabled"); }
tommi@webrtc.org7c64ed22015-03-17 14:25:3754 EXPECT_FALSE(TestStatistics::Get()->Count());
55 TestStatistics::Get()->Reset();
56}
57
Doudou Kisabaka2dec4962019-11-28 13:24:3158#if RTC_TRACE_EVENTS_ENABLED
tommi@webrtc.org7c64ed22015-03-17 14:25:3759TEST(EventTracerTest, ScopedTraceEvent) {
Doudou Kisabaka2dec4962019-11-28 13:24:3160 SetupEventTracer(
61 [](const char* /*name*/) {
62 return reinterpret_cast<const unsigned char*>("test");
63 },
Jared Siskin802e8e52023-04-20 00:35:2864 [](char /*phase*/, const unsigned char* /*category_enabled*/,
65 const char* /*name*/, unsigned long long /*id*/, int /*num_args*/,
66 const char** /*arg_names*/, const unsigned char* /*arg_types*/,
Doudou Kisabaka2dec4962019-11-28 13:24:3167 const unsigned long long* /*arg_values*/,
Jared Siskin802e8e52023-04-20 00:35:2868 unsigned char /*flags*/) { TestStatistics::Get()->Increment(); });
Yves Gerey665174f2018-06-19 13:03:0569 { TRACE_EVENT0("test", "ScopedTraceEvent"); }
tommi@webrtc.org7c64ed22015-03-17 14:25:3770 EXPECT_EQ(2, TestStatistics::Get()->Count());
71 TestStatistics::Get()->Reset();
72}
Doudou Kisabaka2dec4962019-11-28 13:24:3173#endif
tommi@webrtc.org7c64ed22015-03-17 14:25:3774
75} // namespace webrtc