OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // | 2 // |
3 // Tests of profiles generator and utilities. | 3 // Tests of profiles generator and utilities. |
4 | 4 |
5 #ifdef ENABLE_LOGGING_AND_PROFILING | 5 #ifdef ENABLE_LOGGING_AND_PROFILING |
6 | 6 |
7 #include "v8.h" | 7 #include "v8.h" |
8 #include "profile-generator-inl.h" | 8 #include "profile-generator-inl.h" |
9 #include "cctest.h" | 9 #include "cctest.h" |
| 10 #include "../include/v8-profiler.h" |
10 | 11 |
11 namespace i = v8::internal; | 12 namespace i = v8::internal; |
12 | 13 |
13 using i::CodeEntry; | 14 using i::CodeEntry; |
14 using i::CodeMap; | 15 using i::CodeMap; |
15 using i::CpuProfile; | 16 using i::CpuProfile; |
| 17 using i::CpuProfiler; |
16 using i::CpuProfilesCollection; | 18 using i::CpuProfilesCollection; |
17 using i::ProfileNode; | 19 using i::ProfileNode; |
18 using i::ProfileTree; | 20 using i::ProfileTree; |
19 using i::ProfileGenerator; | 21 using i::ProfileGenerator; |
20 using i::SampleRateCalculator; | 22 using i::SampleRateCalculator; |
21 using i::TickSample; | 23 using i::TickSample; |
22 using i::TokenEnumerator; | 24 using i::TokenEnumerator; |
23 using i::Vector; | 25 using i::Vector; |
24 | 26 |
25 | 27 |
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 2; | 663 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 2; |
662 calc3.UpdateMeasurements(time); | 664 calc3.UpdateMeasurements(time); |
663 // (1.0 + 0.5) / 2 | 665 // (1.0 + 0.5) / 2 |
664 CHECK_EQ(kSamplingIntervalMs * 0.75, calc3.ticks_per_ms()); | 666 CHECK_EQ(kSamplingIntervalMs * 0.75, calc3.ticks_per_ms()); |
665 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 1.5; | 667 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 1.5; |
666 calc3.UpdateMeasurements(time); | 668 calc3.UpdateMeasurements(time); |
667 // (1.0 + 0.5 + 0.5) / 3 | 669 // (1.0 + 0.5 + 0.5) / 3 |
668 CHECK_EQ(kSamplingIntervalMs * 0.66666, calc3.ticks_per_ms()); | 670 CHECK_EQ(kSamplingIntervalMs * 0.66666, calc3.ticks_per_ms()); |
669 } | 671 } |
670 | 672 |
| 673 |
| 674 // --- P r o f i l e r E x t e n s i o n --- |
| 675 |
| 676 class ProfilerExtension : public v8::Extension { |
| 677 public: |
| 678 ProfilerExtension() : v8::Extension("v8/profiler", kSource) { } |
| 679 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 680 v8::Handle<v8::String> name); |
| 681 static v8::Handle<v8::Value> StartProfiling(const v8::Arguments& args); |
| 682 static v8::Handle<v8::Value> StopProfiling(const v8::Arguments& args); |
| 683 private: |
| 684 static const char* kSource; |
| 685 }; |
| 686 |
| 687 |
| 688 const char* ProfilerExtension::kSource = |
| 689 "native function startProfiling();" |
| 690 "native function stopProfiling();"; |
| 691 |
| 692 v8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunction( |
| 693 v8::Handle<v8::String> name) { |
| 694 if (name->Equals(v8::String::New("startProfiling"))) { |
| 695 return v8::FunctionTemplate::New(ProfilerExtension::StartProfiling); |
| 696 } else if (name->Equals(v8::String::New("stopProfiling"))) { |
| 697 return v8::FunctionTemplate::New(ProfilerExtension::StopProfiling); |
| 698 } else { |
| 699 CHECK(false); |
| 700 return v8::Handle<v8::FunctionTemplate>(); |
| 701 } |
| 702 } |
| 703 |
| 704 |
| 705 v8::Handle<v8::Value> ProfilerExtension::StartProfiling( |
| 706 const v8::Arguments& args) { |
| 707 if (args.Length() > 0) |
| 708 v8::CpuProfiler::StartProfiling(args[0].As<v8::String>()); |
| 709 else |
| 710 v8::CpuProfiler::StartProfiling(v8::String::New("")); |
| 711 return v8::Undefined(); |
| 712 } |
| 713 |
| 714 |
| 715 v8::Handle<v8::Value> ProfilerExtension::StopProfiling( |
| 716 const v8::Arguments& args) { |
| 717 if (args.Length() > 0) |
| 718 v8::CpuProfiler::StopProfiling(args[0].As<v8::String>()); |
| 719 else |
| 720 v8::CpuProfiler::StopProfiling(v8::String::New("")); |
| 721 return v8::Undefined(); |
| 722 } |
| 723 |
| 724 |
| 725 static ProfilerExtension kProfilerExtension; |
| 726 v8::DeclareExtension kProfilerExtensionDeclaration(&kProfilerExtension); |
| 727 static v8::Persistent<v8::Context> env; |
| 728 |
| 729 static const ProfileNode* PickChild(const ProfileNode* parent, |
| 730 const char* name) { |
| 731 for (int i = 0; i < parent->children()->length(); ++i) { |
| 732 const ProfileNode* child = parent->children()->at(i); |
| 733 if (strcmp(child->entry()->name(), name) == 0) return child; |
| 734 } |
| 735 return NULL; |
| 736 } |
| 737 |
| 738 |
| 739 TEST(RecordStackTraceAtStartProfiling) { |
| 740 if (env.IsEmpty()) { |
| 741 v8::HandleScope scope; |
| 742 const char* extensions[] = { "v8/profiler" }; |
| 743 v8::ExtensionConfiguration config(1, extensions); |
| 744 env = v8::Context::New(&config); |
| 745 } |
| 746 v8::HandleScope scope; |
| 747 env->Enter(); |
| 748 |
| 749 CHECK_EQ(0, CpuProfiler::GetProfilesCount()); |
| 750 CompileRun( |
| 751 "function c() { startProfiling(); }\n" |
| 752 "function b() { c(); }\n" |
| 753 "function a() { b(); }\n" |
| 754 "a();\n" |
| 755 "stopProfiling();"); |
| 756 CHECK_EQ(1, CpuProfiler::GetProfilesCount()); |
| 757 CpuProfile* profile = |
| 758 CpuProfiler::GetProfile(NULL, 0); |
| 759 const ProfileTree* topDown = profile->top_down(); |
| 760 const ProfileNode* current = topDown->root(); |
| 761 // The tree should look like this: |
| 762 // (root) |
| 763 // (anonymous function) |
| 764 // a |
| 765 // b |
| 766 // c |
| 767 current = PickChild(current, "(anonymous function)"); |
| 768 CHECK_NE(NULL, const_cast<ProfileNode*>(current)); |
| 769 current = PickChild(current, "a"); |
| 770 CHECK_NE(NULL, const_cast<ProfileNode*>(current)); |
| 771 current = PickChild(current, "b"); |
| 772 CHECK_NE(NULL, const_cast<ProfileNode*>(current)); |
| 773 current = PickChild(current, "c"); |
| 774 CHECK_NE(NULL, const_cast<ProfileNode*>(current)); |
| 775 CHECK_EQ(0, current->children()->length()); |
| 776 } |
| 777 |
671 #endif // ENABLE_LOGGING_AND_PROFILING | 778 #endif // ENABLE_LOGGING_AND_PROFILING |
OLD | NEW |