tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/event_tracer.h" |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [diff] [blame] | 13 | #include "rtc_base/critical_section.h" |
| 14 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/trace_event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "test/gtest.h" |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class TestStatistics { |
| 21 | public: |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [diff] [blame] | 22 | void Reset() { |
| 23 | rtc::CritScope cs(&crit_); |
| 24 | events_logged_ = 0; |
| 25 | } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 26 | |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [diff] [blame] | 27 | void Increment() { |
| 28 | rtc::CritScope cs(&crit_); |
| 29 | ++events_logged_; |
| 30 | } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 31 | |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [diff] [blame] | 32 | int Count() const { |
| 33 | rtc::CritScope cs(&crit_); |
| 34 | return events_logged_; |
| 35 | } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 36 | |
| 37 | static TestStatistics* Get() { |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [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 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | private: |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [diff] [blame] | 44 | rtc::CriticalSection crit_; |
| 45 | int events_logged_ RTC_GUARDED_BY(crit_) = 0; |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [diff] [blame] | 48 | const unsigned char* GetCategoryEnabledHandler(const char* /*name*/) { |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 49 | return reinterpret_cast<const unsigned char*>("test"); |
| 50 | } |
| 51 | |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [diff] [blame] | 52 | void TraceEventHandler(char /*phase*/, |
| 53 | const unsigned char* /*category_enabled*/, |
| 54 | const char* /*name*/, |
| 55 | unsigned long long /*id*/, |
| 56 | int /*num_args*/, |
| 57 | const char** /*arg_names*/, |
| 58 | const unsigned char* /*arg_types*/, |
| 59 | const unsigned long long* /*arg_values*/, |
| 60 | unsigned char /*flags*/) { |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 61 | TestStatistics::Get()->Increment(); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | namespace webrtc { |
| 67 | |
| 68 | TEST(EventTracerTest, EventTracerDisabled) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 69 | { TRACE_EVENT0("test", "EventTracerDisabled"); } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 70 | EXPECT_FALSE(TestStatistics::Get()->Count()); |
| 71 | TestStatistics::Get()->Reset(); |
| 72 | } |
| 73 | |
| 74 | TEST(EventTracerTest, ScopedTraceEvent) { |
Yves Gerey | 50b0baf | 2019-09-06 10:03:54 +0200 | [diff] [blame] | 75 | SetupEventTracer(&GetCategoryEnabledHandler, &TraceEventHandler); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 76 | { TRACE_EVENT0("test", "ScopedTraceEvent"); } |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 77 | EXPECT_EQ(2, TestStatistics::Get()->Count()); |
| 78 | TestStatistics::Get()->Reset(); |
| 79 | } |
| 80 | |
| 81 | } // namespace webrtc |