tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/event_tracer.h" |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 12 | |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 13 | #include "rtc_base/synchronization/mutex.h" |
Yves Gerey | 50b0baf | 2019-09-06 08:03:54 | [diff] [blame] | 14 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "rtc_base/trace_event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "test/gtest.h" |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class TestStatistics { |
| 21 | public: |
Yves Gerey | 50b0baf | 2019-09-06 08:03:54 | [diff] [blame] | 22 | void Reset() { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 23 | webrtc::MutexLock lock(&mutex_); |
Yves Gerey | 50b0baf | 2019-09-06 08:03:54 | [diff] [blame] | 24 | events_logged_ = 0; |
| 25 | } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 26 | |
Yves Gerey | 50b0baf | 2019-09-06 08:03:54 | [diff] [blame] | 27 | void Increment() { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 28 | webrtc::MutexLock lock(&mutex_); |
Yves Gerey | 50b0baf | 2019-09-06 08:03:54 | [diff] [blame] | 29 | ++events_logged_; |
| 30 | } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 31 | |
Yves Gerey | 50b0baf | 2019-09-06 08:03:54 | [diff] [blame] | 32 | int Count() const { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 33 | webrtc::MutexLock lock(&mutex_); |
Yves Gerey | 50b0baf | 2019-09-06 08:03:54 | [diff] [blame] | 34 | return events_logged_; |
| 35 | } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 36 | |
| 37 | static TestStatistics* Get() { |
Yves Gerey | 50b0baf | 2019-09-06 08:03:54 | [diff] [blame] | 38 | // google.github.io/styleguide/cppguide.html#Static_and_Global_Variables |
| 39 | static auto& test_stats = *new TestStatistics(); |
| 40 | return &test_stats; |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | private: |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 44 | mutable webrtc::Mutex mutex_; |
| 45 | int events_logged_ RTC_GUARDED_BY(mutex_) = 0; |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 46 | }; |
| 47 | |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 48 | } // namespace |
| 49 | |
| 50 | namespace webrtc { |
| 51 | |
| 52 | TEST(EventTracerTest, EventTracerDisabled) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 53 | { TRACE_EVENT0("test", "EventTracerDisabled"); } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 54 | EXPECT_FALSE(TestStatistics::Get()->Count()); |
| 55 | TestStatistics::Get()->Reset(); |
| 56 | } |
| 57 | |
Doudou Kisabaka | 2dec496 | 2019-11-28 13:24:31 | [diff] [blame] | 58 | #if RTC_TRACE_EVENTS_ENABLED |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 59 | TEST(EventTracerTest, ScopedTraceEvent) { |
Doudou Kisabaka | 2dec496 | 2019-11-28 13:24:31 | [diff] [blame] | 60 | SetupEventTracer( |
| 61 | [](const char* /*name*/) { |
| 62 | return reinterpret_cast<const unsigned char*>("test"); |
| 63 | }, |
Jared Siskin | 802e8e5 | 2023-04-20 00:35:28 | [diff] [blame] | 64 | [](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 Kisabaka | 2dec496 | 2019-11-28 13:24:31 | [diff] [blame] | 67 | const unsigned long long* /*arg_values*/, |
Jared Siskin | 802e8e5 | 2023-04-20 00:35:28 | [diff] [blame] | 68 | unsigned char /*flags*/) { TestStatistics::Get()->Increment(); }); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 69 | { TRACE_EVENT0("test", "ScopedTraceEvent"); } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 70 | EXPECT_EQ(2, TestStatistics::Get()->Count()); |
| 71 | TestStatistics::Get()->Reset(); |
| 72 | } |
Doudou Kisabaka | 2dec496 | 2019-11-28 13:24:31 | [diff] [blame] | 73 | #endif |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 74 | |
| 75 | } // namespace webrtc |