Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 631 CHECK_EQ(1, startNode->GetChildrenCount()); | 631 CHECK_EQ(1, startNode->GetChildrenCount()); |
| 632 const v8::CpuProfileNode* delayNode = GetChild(startNode, "delay"); | 632 const v8::CpuProfileNode* delayNode = GetChild(startNode, "delay"); |
| 633 if (delayNode->GetChildrenCount() > 0) { | 633 if (delayNode->GetChildrenCount() > 0) { |
| 634 CHECK_EQ(1, delayNode->GetChildrenCount()); | 634 CHECK_EQ(1, delayNode->GetChildrenCount()); |
| 635 GetChild(delayNode, "loop"); | 635 GetChild(delayNode, "loop"); |
| 636 } | 636 } |
| 637 } | 637 } |
| 638 | 638 |
| 639 cpu_profiler->DeleteAllCpuProfiles(); | 639 cpu_profiler->DeleteAllCpuProfiles(); |
| 640 } | 640 } |
| 641 | |
| 642 | |
| 643 static const char* native_accessor_test_source = "function start(count) {\n" | |
| 644 " for (var i = 0; i < count; i++) {\n" | |
| 645 " var o = instance.foo;\n" | |
| 646 " instance.foo = o + 1;\n" | |
| 647 " }\n" | |
| 648 "}\n"; | |
| 649 | |
| 650 | |
| 651 class FooAccessorsData { | |
| 652 public: | |
| 653 explicit FooAccessorsData(int repeat_count) | |
| 654 : repeat_count_(repeat_count), | |
| 655 total_(0) {} | |
| 656 | |
| 657 static v8::Handle<v8::Value> Getter(v8::Local<v8::String> name, | |
| 658 const v8::AccessorInfo& info) { | |
| 659 FooAccessorsData* data = fromInfo(info); | |
| 660 for (int i = 0; i < data->repeat_count_; i++) { | |
| 661 data->total_ += i; | |
| 662 } | |
| 663 return v8::Int32::New(data->total_); | |
| 664 } | |
| 665 | |
| 666 static void Setter(v8::Local<v8::String> name, | |
| 667 v8::Local<v8::Value> value, | |
| 668 const v8::AccessorInfo& info) { | |
| 669 FooAccessorsData* data = fromInfo(info); | |
| 670 for (int i = 0; i < data->repeat_count_; i++) { | |
| 671 data->total_ += i; | |
| 672 } | |
| 673 } | |
| 674 | |
| 675 private: | |
| 676 static FooAccessorsData* fromInfo(const v8::AccessorInfo& info) { | |
| 677 void* data = v8::External::Cast(*info.Data())->Value(); | |
| 678 return reinterpret_cast<FooAccessorsData*>(data); | |
| 679 } | |
| 680 | |
| 681 int repeat_count_; | |
| 682 int total_; | |
| 683 }; | |
| 684 | |
| 685 | |
| 686 TEST(NativeAccessorNameInProfile1) { | |
| 687 LocalContext env; | |
| 688 v8::HandleScope scope(env->GetIsolate()); | |
| 689 | |
| 690 | |
| 691 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); | |
| 692 v8::Local<v8::ObjectTemplate> instance_template = | |
| 693 func_template->InstanceTemplate(); | |
| 694 | |
| 695 FooAccessorsData accessors(100 * 1000 * 1000); | |
| 696 v8::Local<v8::External> data = v8::External::New(&accessors); | |
| 697 instance_template->SetAccessor( | |
| 698 v8::String::New("foo"), &FooAccessorsData::Getter, | |
| 699 &FooAccessorsData::Setter, data); | |
| 700 v8::Local<v8::Function> func = func_template->GetFunction(); | |
| 701 v8::Local<v8::Object> instance = func->NewInstance(); | |
| 702 env->Global()->Set(v8::String::New("instance"), instance); | |
| 703 | |
| 704 v8::Script::Compile(v8::String::New(native_accessor_test_source))->Run(); | |
| 705 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | |
| 706 env->Global()->Get(v8::String::New("start"))); | |
| 707 | |
| 708 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); | |
| 709 v8::Local<v8::String> profile_name = v8::String::New("my_profile"); | |
| 710 | |
| 711 cpu_profiler->StartCpuProfiling(profile_name); | |
| 712 int32_t repeat_count = 1; | |
| 713 v8::Handle<v8::Value> args[] = { v8::Integer::New(repeat_count) }; | |
| 714 function->Call(env->Global(), ARRAY_SIZE(args), args); | |
| 715 const v8::CpuProfile* profile = cpu_profiler->StopCpuProfiling(profile_name); | |
| 716 | |
| 717 CHECK_NE(NULL, profile); | |
| 718 // Dump collected profile to have a better diagnostic in case of failure. | |
| 719 reinterpret_cast<i::CpuProfile*>( | |
| 720 const_cast<v8::CpuProfile*>(profile))->Print(); | |
| 721 | |
| 722 const v8::CpuProfileNode* root = profile->GetTopDownRoot(); | |
| 723 const v8::CpuProfileNode* startNode = GetChild(root, "start"); | |
| 724 GetChild(startNode, "get foo"); | |
| 725 GetChild(startNode, "set foo"); | |
| 726 | |
| 727 cpu_profiler->DeleteAllCpuProfiles(); | |
| 728 } | |
| 729 | |
| 730 | |
| 731 TEST(NativeAccessorNameInProfile2) { | |
| 732 LocalContext env; | |
| 733 v8::HandleScope scope(env->GetIsolate()); | |
| 734 | |
| 735 | |
| 736 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); | |
| 737 v8::Local<v8::ObjectTemplate> instance_template = | |
| 738 func_template->InstanceTemplate(); | |
| 739 | |
| 740 FooAccessorsData accessors(5 * 1000); | |
| 741 v8::Local<v8::External> data = v8::External::New(&accessors); | |
| 742 instance_template->SetAccessor( | |
| 743 v8::String::New("foo"), &FooAccessorsData::Getter, | |
| 744 &FooAccessorsData::Setter, data); | |
| 745 v8::Local<v8::Function> func = func_template->GetFunction(); | |
| 746 v8::Local<v8::Object> instance = func->NewInstance(); | |
| 747 env->Global()->Set(v8::String::New("instance"), instance); | |
|
loislo
2013/06/03 10:04:50
please extract the common part about creating an o
yurys
2013/06/03 10:07:30
I'm going to do extract this code into a function
| |
| 748 | |
| 749 v8::Script::Compile(v8::String::New(native_accessor_test_source))->Run(); | |
| 750 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | |
| 751 env->Global()->Get(v8::String::New("start"))); | |
| 752 | |
| 753 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); | |
| 754 v8::Local<v8::String> profile_name = v8::String::New("my_profile"); | |
| 755 | |
| 756 cpu_profiler->StartCpuProfiling(profile_name); | |
| 757 int32_t repeat_count = 5 * 1000 * 1000; | |
| 758 #if defined(USE_SIMULATOR) | |
| 759 // Simulators are much slower. | |
| 760 repeat_count = 10 * 1000; | |
| 761 #endif | |
| 762 v8::Handle<v8::Value> args[] = { v8::Integer::New(repeat_count) }; | |
| 763 function->Call(env->Global(), ARRAY_SIZE(args), args); | |
| 764 const v8::CpuProfile* profile = cpu_profiler->StopCpuProfiling(profile_name); | |
| 765 | |
| 766 CHECK_NE(NULL, profile); | |
| 767 // Dump collected profile to have a better diagnostic in case of failure. | |
| 768 reinterpret_cast<i::CpuProfile*>( | |
| 769 const_cast<v8::CpuProfile*>(profile))->Print(); | |
| 770 | |
| 771 const v8::CpuProfileNode* root = profile->GetTopDownRoot(); | |
| 772 const v8::CpuProfileNode* startNode = GetChild(root, "start"); | |
| 773 // TODO(yurys): in LoadIC should be changed to report external callback | |
| 774 // invocation. See r13768 where it was LoadCallbackProperty was removed. | |
| 775 // GetChild(startNode, "get foo"); | |
| 776 GetChild(startNode, "set foo"); | |
| 777 | |
| 778 cpu_profiler->DeleteAllCpuProfiles(); | |
| 779 } | |
| OLD | NEW |