Chromium Code Reviews| Index: test/cctest/test-trace-event.cc |
| diff --git a/test/cctest/test-trace-event.cc b/test/cctest/test-trace-event.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3abd03ddde3f7afda115a79222a876aa52902630 |
| --- /dev/null |
| +++ b/test/cctest/test-trace-event.cc |
| @@ -0,0 +1,281 @@ |
| +// Copyright 2015 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +#include <stdlib.h> |
| + |
| +#include "src/v8.h" |
| + |
| +#include "src/list.h" |
| +#include "src/list-inl.h" |
| +#include "test/cctest/cctest.h" |
| + |
| +using v8::IdleTask; |
| +using v8::Task; |
| +using v8::Isolate; |
| + |
| +#include "src/tracing/trace-event.h" |
| + |
| +#define GET_TRACE_OBJECTS_LIST platform.GetMockTraceObjects() |
| + |
| +#define GET_TRACE_OBJECT(Index) GET_TRACE_OBJECTS_LIST->at(Index) |
| + |
| + |
| +struct MockTraceObject { |
| + char phase; |
| + const char* name; |
| + uint64_t id; |
| + uint64_t context_id; |
| + uint64_t bind_id; |
| + int numArgs; |
|
noordhuis
2015/10/07 18:31:54
Style nit: mildly inconsistent mix of camelCase an
fmeawad
2015/11/19 02:00:47
Done.
|
| + unsigned int flags; |
| + MockTraceObject(char phase, const char* name, uint64_t id, |
| + uint64_t context_id, uint64_t bind_id, int numArgs, int flags) |
| + : phase(phase), |
| + name(name), |
| + id(id), |
| + context_id(context_id), |
| + bind_id(bind_id), |
| + numArgs(numArgs), |
| + flags(flags) {} |
| +}; |
| + |
| +typedef v8::internal::List<MockTraceObject*> MockTraceObjectList; |
| + |
| +class MockTracingPlatform : public v8::Platform { |
| + public: |
| + explicit MockTracingPlatform(v8::Platform* platform) {} |
| + virtual ~MockTracingPlatform() { |
| + for (int i = 0; i < trace_object_list_.length(); ++i) { |
| + delete trace_object_list_[i]; |
| + } |
| + trace_object_list_.Clear(); |
| + } |
| + void CallOnBackgroundThread(Task* task, |
| + ExpectedRuntime expected_runtime) override {} |
| + |
| + void CallOnForegroundThread(Isolate* isolate, Task* task) override {} |
| + |
| + void CallDelayedOnForegroundThread(Isolate* isolate, Task* task, |
| + double delay_in_seconds) override {} |
| + |
| + double MonotonicallyIncreasingTime() override { return 0.0; } |
| + |
| + void CallIdleOnForegroundThread(Isolate* isolate, IdleTask* task) override {} |
| + |
| + bool IdleTasksEnabled(Isolate* isolate) override { return false; } |
| + |
| + bool PendingIdleTask() { return false; } |
| + |
| + void PerformIdleTask(double idle_time_in_seconds) {} |
| + |
| + bool PendingDelayedTask() { return false; } |
| + |
| + void PerformDelayedTask() {} |
| + |
| + virtual uint64_t AddTraceEvent(char phase, const uint8_t* categoryEnabledFlag, |
| + const char* name, uint64_t id, |
| + uint64_t context_id, uint64_t bind_id, |
| + int numArgs, const char** argNames, |
| + const uint8_t* argTypes, |
| + const uint64_t* argValues, |
| + unsigned int flags) override { |
| + MockTraceObject* to = new MockTraceObject(phase, name, id, context_id, |
| + bind_id, numArgs, flags); |
| + trace_object_list_.Add(to); |
| + return 0; |
| + } |
| + |
| + virtual void UpdateTraceEventDuration(const uint8_t* categoryEnabledFlag, |
| + const char* name, |
| + uint64_t handle) override {} |
| + |
| + virtual const uint8_t* GetCategoryGroupEnabled(const char* name) override { |
| + if (strcmp(name, "v8-cat")) { |
| + static uint8_t no = 0; |
| + return &no; |
| + } else { |
| + static uint8_t yes = 0x7; |
| + return &yes; |
| + } |
| + } |
| + |
| + virtual const char* GetCategoryGroupName( |
| + const uint8_t* categoryEnabledFlag) override { |
| + static const char* dummy = "dummy"; |
| + return dummy; |
| + } |
| + |
| + MockTraceObjectList* GetMockTraceObjects() { return &trace_object_list_; } |
| + |
| + private: |
| + MockTraceObjectList trace_object_list_; |
| +}; |
| + |
| + |
| +TEST(TraceEventDisabledCategory) { |
| + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| + MockTracingPlatform platform(old_platform); |
| + i::V8::SetPlatformForTesting(&platform); |
| + |
| + // Disabled category, will not add events. |
| + TRACE_EVENT_BEGIN0("cat", "e1"); |
| + TRACE_EVENT_END0("cat", "e1"); |
| + CHECK_EQ(0, GET_TRACE_OBJECTS_LIST->length()); |
| + |
| + i::V8::SetPlatformForTesting(old_platform); |
| +} |
| + |
| + |
| +TEST(TraceEventNoArgs) { |
| + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| + MockTracingPlatform platform(old_platform); |
| + i::V8::SetPlatformForTesting(&platform); |
| + |
| + // Enabled category will add 2 events. |
| + TRACE_EVENT_BEGIN0("v8-cat", "e1"); |
| + TRACE_EVENT_END0("v8-cat", "e1"); |
| + |
| + CHECK_EQ(2, GET_TRACE_OBJECTS_LIST->length()); |
| + CHECK_EQ('B', GET_TRACE_OBJECT(0)->phase); |
| + CHECK_EQ("e1", GET_TRACE_OBJECT(0)->name); |
| + CHECK_EQ(0, GET_TRACE_OBJECT(0)->numArgs); |
| + |
| + CHECK_EQ('E', GET_TRACE_OBJECT(1)->phase); |
| + CHECK_EQ("e1", GET_TRACE_OBJECT(1)->name); |
| + CHECK_EQ(0, GET_TRACE_OBJECT(1)->numArgs); |
| + |
| + i::V8::SetPlatformForTesting(old_platform); |
| +} |
| + |
| + |
| +TEST(TraceEventWithOneArg) { |
| + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| + MockTracingPlatform platform(old_platform); |
| + i::V8::SetPlatformForTesting(&platform); |
| + |
| + TRACE_EVENT_BEGIN1("v8-cat", "e1", "arg1", 42); |
| + TRACE_EVENT_END1("v8-cat", "e1", "arg1", 42); |
| + TRACE_EVENT_BEGIN1("v8-cat", "e2", "arg1", "abc"); |
| + TRACE_EVENT_END1("v8-cat", "e2", "arg1", "abc"); |
| + |
| + CHECK_EQ(4, GET_TRACE_OBJECTS_LIST->length()); |
| + |
| + CHECK_EQ(1, GET_TRACE_OBJECT(0)->numArgs); |
| + CHECK_EQ(1, GET_TRACE_OBJECT(1)->numArgs); |
| + CHECK_EQ(1, GET_TRACE_OBJECT(2)->numArgs); |
| + CHECK_EQ(1, GET_TRACE_OBJECT(3)->numArgs); |
| + |
| + i::V8::SetPlatformForTesting(old_platform); |
| +} |
| + |
| + |
| +TEST(TraceEventWithTwoArgs) { |
| + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| + MockTracingPlatform platform(old_platform); |
| + i::V8::SetPlatformForTesting(&platform); |
| + |
| + TRACE_EVENT_BEGIN2("v8-cat", "e1", "arg1", 42, "arg2", "abc"); |
| + TRACE_EVENT_END2("v8-cat", "e1", "arg1", 42, "arg2", "abc"); |
| + TRACE_EVENT_BEGIN2("v8-cat", "e2", "arg1", "abc", "arg2", 43); |
| + TRACE_EVENT_END2("v8-cat", "e2", "arg1", "abc", "arg2", 43); |
| + |
| + CHECK_EQ(4, GET_TRACE_OBJECTS_LIST->length()); |
| + |
| + CHECK_EQ(2, GET_TRACE_OBJECT(0)->numArgs); |
| + CHECK_EQ(2, GET_TRACE_OBJECT(1)->numArgs); |
| + CHECK_EQ(2, GET_TRACE_OBJECT(2)->numArgs); |
| + CHECK_EQ(2, GET_TRACE_OBJECT(3)->numArgs); |
| + |
| + i::V8::SetPlatformForTesting(old_platform); |
| +} |
| + |
| + |
| +TEST(ScopedTraceEvent) { |
| + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| + MockTracingPlatform platform(old_platform); |
| + i::V8::SetPlatformForTesting(&platform); |
| + |
| + { TRACE_EVENT0("v8-cat", "e"); } |
| + |
| + CHECK_EQ(1, GET_TRACE_OBJECTS_LIST->length()); |
| + CHECK_EQ(0, GET_TRACE_OBJECT(0)->numArgs); |
| + |
| + { TRACE_EVENT1("v8-cat", "e1", "arg1", "abc"); } |
| + |
| + CHECK_EQ(2, GET_TRACE_OBJECTS_LIST->length()); |
| + CHECK_EQ(1, GET_TRACE_OBJECT(1)->numArgs); |
| + |
| + { TRACE_EVENT2("v8-cat", "e1", "arg1", "abc", "arg2", 42); } |
| + |
| + CHECK_EQ(3, GET_TRACE_OBJECTS_LIST->length()); |
| + CHECK_EQ(2, GET_TRACE_OBJECT(2)->numArgs); |
| + |
| + i::V8::SetPlatformForTesting(old_platform); |
| +} |
| + |
| + |
| +TEST(TestWithContextId) { |
| + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| + MockTracingPlatform platform(old_platform); |
| + i::V8::SetPlatformForTesting(&platform); |
| + |
| + TRACE_EVENT_BEGIN_WITH_CONTEXT_ID0("v8-cat", "i1", 42); |
| + TRACE_EVENT_BEGIN_WITH_CONTEXT_ID0("v8-cat", "i2", 43); |
| + |
| + CHECK_EQ(2, GET_TRACE_OBJECTS_LIST->length()); |
| + CHECK_EQ(42, GET_TRACE_OBJECT(0)->context_id); |
| + CHECK_EQ(TRACE_EVENT_FLAG_HAS_CONTEXT_ID, GET_TRACE_OBJECT(0)->flags); |
| + CHECK_EQ(43, GET_TRACE_OBJECT(1)->context_id); |
| + CHECK_EQ(TRACE_EVENT_FLAG_HAS_CONTEXT_ID, GET_TRACE_OBJECT(1)->flags); |
| + |
| + i::V8::SetPlatformForTesting(old_platform); |
| +} |
| + |
| + |
| +TEST(TestEventWithFlow) { |
| + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| + MockTracingPlatform platform(old_platform); |
| + i::V8::SetPlatformForTesting(&platform); |
| + |
| + static uint64_t bind_id = 21; |
| + { |
| + TRACE_EVENT_WITH_FLOW0("v8-cat", "f1", bind_id, TRACE_EVENT_FLAG_FLOW_OUT); |
| + } |
| + { |
| + TRACE_EVENT_WITH_FLOW0( |
| + "v8-cat", "f2", bind_id, |
| + TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT); |
| + } |
| + { TRACE_EVENT_WITH_FLOW0("v8-cat", "f3", bind_id, TRACE_EVENT_FLAG_FLOW_IN); } |
| + |
| + CHECK_EQ(3, GET_TRACE_OBJECTS_LIST->length()); |
| + CHECK_EQ(bind_id, GET_TRACE_OBJECT(0)->bind_id); |
| + CHECK_EQ(TRACE_EVENT_FLAG_FLOW_OUT, GET_TRACE_OBJECT(0)->flags); |
| + CHECK_EQ(bind_id, GET_TRACE_OBJECT(1)->bind_id); |
| + CHECK_EQ(TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, |
| + GET_TRACE_OBJECT(1)->flags); |
| + CHECK_EQ(bind_id, GET_TRACE_OBJECT(2)->bind_id); |
| + CHECK_EQ(TRACE_EVENT_FLAG_FLOW_IN, GET_TRACE_OBJECT(2)->flags); |
| + |
| + i::V8::SetPlatformForTesting(old_platform); |
| +} |
| + |
| + |
| +TEST(TestEventWithId) { |
| + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| + MockTracingPlatform platform(old_platform); |
| + i::V8::SetPlatformForTesting(&platform); |
| + |
| + static uint64_t event_id = 21; |
| + TRACE_EVENT_ASYNC_BEGIN0("v8-cat", "a1", event_id); |
| + TRACE_EVENT_ASYNC_END0("v8-cat", "a1", event_id); |
| + |
| + CHECK_EQ(2, GET_TRACE_OBJECTS_LIST->length()); |
| + CHECK_EQ(TRACE_EVENT_PHASE_ASYNC_BEGIN, GET_TRACE_OBJECT(0)->phase); |
| + CHECK_EQ(event_id, GET_TRACE_OBJECT(0)->id); |
| + CHECK_EQ(TRACE_EVENT_PHASE_ASYNC_END, GET_TRACE_OBJECT(1)->phase); |
| + CHECK_EQ(event_id, GET_TRACE_OBJECT(1)->id); |
| + |
| + i::V8::SetPlatformForTesting(old_platform); |
| +} |
| +#undef INTERNAL_GET_TRACE_CONTEXT_ID |