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 1071 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1082 names[0] = v8::String::New(ProfileGenerator::kGarbageCollectorEntryName); | 1082 names[0] = v8::String::New(ProfileGenerator::kGarbageCollectorEntryName); |
1083 names[1] = v8::String::New(ProfileGenerator::kProgramEntryName); | 1083 names[1] = v8::String::New(ProfileGenerator::kProgramEntryName); |
1084 names[2] = v8::String::New("start"); | 1084 names[2] = v8::String::New("start"); |
1085 names[3] = v8::String::New(i::ProfileGenerator::kUnresolvedFunctionName); | 1085 names[3] = v8::String::New(i::ProfileGenerator::kUnresolvedFunctionName); |
1086 // Don't allow |bar| and |call| nodes to be at the top level. | 1086 // Don't allow |bar| and |call| nodes to be at the top level. |
1087 CheckChildrenNames(root, names); | 1087 CheckChildrenNames(root, names); |
1088 } | 1088 } |
1089 | 1089 |
1090 const v8::CpuProfileNode* startNode = GetChild(root, "start"); | 1090 const v8::CpuProfileNode* startNode = GetChild(root, "start"); |
1091 { | 1091 { |
1092 ScopedVector<v8::Handle<v8::String> > names(1); | 1092 ScopedVector<v8::Handle<v8::String> > names(2); |
1093 names[0] = v8::String::New("bar"); | 1093 names[0] = v8::String::New("bar"); |
1094 names[1] = v8::String::New("call"); | |
1094 CheckChildrenNames(startNode, names); | 1095 CheckChildrenNames(startNode, names); |
1095 } | 1096 } |
1096 | 1097 |
1097 const v8::CpuProfileNode* unresolvedNode = | 1098 const v8::CpuProfileNode* unresolvedNode = |
1098 FindChild(root, i::ProfileGenerator::kUnresolvedFunctionName); | 1099 FindChild(root, i::ProfileGenerator::kUnresolvedFunctionName); |
1099 if (unresolvedNode) { | 1100 if (unresolvedNode) { |
1100 ScopedVector<v8::Handle<v8::String> > names(1); | 1101 ScopedVector<v8::Handle<v8::String> > names(1); |
1101 names[0] = v8::String::New("call"); | 1102 names[0] = v8::String::New("call"); |
1102 CheckChildrenNames(unresolvedNode, names); | 1103 CheckChildrenNames(unresolvedNode, names); |
1103 } | 1104 } |
1104 | 1105 |
1105 cpu_profiler->DeleteAllCpuProfiles(); | 1106 cpu_profiler->DeleteAllCpuProfiles(); |
1106 } | 1107 } |
1107 | 1108 |
1109 | |
1110 static const char* function_apply_test_source = "function bar(iterations) {\n" | |
1111 "}\n" | |
1112 "function test() {\n" | |
1113 " bar.apply(this, [10 * 1000]);\n" | |
1114 "}\n" | |
1115 "function start(duration) {\n" | |
1116 " var start = Date.now();\n" | |
1117 " while (Date.now() - start < duration) {\n" | |
1118 " try {\n" | |
1119 " test();\n" | |
1120 " } catch(e) {}\n" | |
1121 " }\n" | |
1122 "}"; | |
1123 | |
1124 | |
1125 // [Top down]: | |
1126 // 94 0 (root) [-1] #0 1 | |
1127 // 2 2 (garbage collector) [-1] #0 7 | |
1128 // 82 49 start [-1] #16 3 | |
1129 // 1 0 (unresolved function) [-1] #0 8 | |
1130 // 1 1 apply [-1] #0 9 | |
1131 // 32 21 test [-1] #16 4 | |
1132 // 2 2 bar [-1] #16 6 | |
1133 // 9 9 apply [-1] #0 5 | |
1134 // 10 10 (program) [-1] #0 2 | |
1135 TEST(FunctionApplySample) { | |
1136 LocalContext env; | |
1137 v8::HandleScope scope(env->GetIsolate()); | |
1138 | |
1139 v8::Script::Compile(v8::String::New(function_apply_test_source))->Run(); | |
1140 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | |
1141 env->Global()->Get(v8::String::New("start"))); | |
1142 | |
1143 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); | |
1144 v8::Local<v8::String> profile_name = v8::String::New("my_profile"); | |
1145 | |
1146 cpu_profiler->StartCpuProfiling(profile_name); | |
1147 int32_t duration_ms = 100; | |
1148 #if defined(_WIN32) || defined(_WIN64) | |
1149 // 100ms is not enough on Windows. See | |
1150 // https://code.google.com/p/v8/issues/detail?id=2628 | |
1151 duration_ms = 400; | |
1152 #endif | |
1153 v8::Handle<v8::Value> args[] = { v8::Integer::New(duration_ms) }; | |
1154 function->Call(env->Global(), ARRAY_SIZE(args), args); | |
1155 const v8::CpuProfile* profile = cpu_profiler->StopCpuProfiling(profile_name); | |
1156 | |
1157 CHECK_NE(NULL, profile); | |
1158 // Dump collected profile to have a better diagnostic in case of failure. | |
1159 reinterpret_cast<i::CpuProfile*>( | |
1160 const_cast<v8::CpuProfile*>(profile))->Print(); | |
1161 | |
1162 const v8::CpuProfileNode* root = profile->GetTopDownRoot(); | |
1163 { | |
1164 ScopedVector<v8::Handle<v8::String> > names(3); | |
1165 names[0] = v8::String::New(ProfileGenerator::kGarbageCollectorEntryName); | |
1166 names[1] = v8::String::New(ProfileGenerator::kProgramEntryName); | |
1167 names[2] = v8::String::New("start"); | |
1168 // Don't allow |test|, |bar| and |apply| nodes to be at the top level. | |
1169 CheckChildrenNames(root, names); | |
1170 } | |
1171 | |
1172 const v8::CpuProfileNode* startNode = GetChild(root, "start"); | |
1173 { | |
1174 ScopedVector<v8::Handle<v8::String> > names(2); | |
1175 names[0] = v8::String::New("test"); | |
1176 names[1] = v8::String::New(ProfileGenerator::kUnresolvedFunctionName); | |
1177 CheckChildrenNames(startNode, names); | |
1178 } | |
1179 | |
1180 { | |
1181 const v8::CpuProfileNode* testNode = GetChild(startNode, "test"); | |
1182 ScopedVector<v8::Handle<v8::String> > names(2); | |
1183 names[0] = v8::String::New("bar"); | |
1184 names[1] = v8::String::New("apply"); | |
1185 CheckChildrenNames(testNode, names); | |
1186 } | |
1187 | |
1188 if (const v8::CpuProfileNode* unresolvedNode = | |
1189 FindChild(startNode, ProfileGenerator::kUnresolvedFunctionName)) { | |
1190 ScopedVector<v8::Handle<v8::String> > names(1); | |
1191 names[0] = v8::String::New("apply"); | |
1192 CheckChildrenNames(unresolvedNode, names); | |
1193 GetChild(unresolvedNode, "apply"); | |
1194 } | |
1195 | |
1196 cpu_profiler->DeleteAllCpuProfiles(); | |
1197 } | |
1198 | |
Jakob Kummerow
2013/07/03 11:41:50
nit: if Rietveld isn't lying and this is an empty
yurys
2013/07/03 13:52:08
Done.
| |
OLD | NEW |