const char* src =
"function loop(timeout) {\n"
" this.mmm = 0;\n"
" var start = Date.now();\n"
" do {\n"
" var n = 1000;\n"
" while(n > 1) {\n"
" n--;\n"
" this.mmm += n * n * n;\n"
" }\n"
" } while (Date.now() - start < timeout);\n"
"}\n"
"function delay() { loop(10); }\n"
"function bar() { delay(); }\n"
"function baz() { delay(); }\n"
"function foo() {\n"
" delay();\n"
" bar();\n"
" delay();\n"
" baz();\n"
"}\n"
"function start(duration) {\n"
" var start = Date.now();\n"
" do {\n"
" foo();\n"
" } while (Date.now() - start < duration);\n"
"}\n";
Script::Compile(context, v8::String::NewFromUtf8(isolate, src).ToLocalChecked()).ToLocalChecked()->Run(context).ToLocalChecked();
Local<v8::Function> startFunc = context->Global()->Get(context, v8::String::NewFromUtf8(isolate, "start").ToLocalChecked()).ToLocalChecked().As<v8::Function>();
Local<v8::String> title = v8::String::NewFromUtf8(isolate, "my_trace").ToLocalChecked();
CpuProfiler* profiler = CpuProfiler::New(isolate);
profiler->StartProfiling(title, false);
Local<Value> result;
Local<Value> args[] = { Number::New(isolate, 200) };
assert(startFunc->Call(context, context->Global(), 1, args).ToLocal(&result));
const CpuProfile* profile = profiler->StopProfiling(title);
const CpuProfileNode* root = profile->GetTopDownRoot();
int count = root->GetChildrenCount();
for (int i = 0; i < count; ++i) {
const CpuProfileNode* child = root->GetChild(i);
v8::String::Utf8Value str(isolate, child->GetFunctionName());
const char* funcName = *str;
printf("%s\n", funcName);
}