| Index: test/cctest/test-sampler-api.cc
|
| diff --git a/test/cctest/test-sampler-api.cc b/test/cctest/test-sampler-api.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..55d54107628bb58832e91cf01786ddf01b87aa7e
|
| --- /dev/null
|
| +++ b/test/cctest/test-sampler-api.cc
|
| @@ -0,0 +1,120 @@
|
| +// Copyright 2014 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.
|
| +//
|
| +// Tests the public sampling API in include/v8.h
|
| +
|
| +#include <string>
|
| +
|
| +#include "include/v8.h"
|
| +
|
| +#include "src/v8.h"
|
| +
|
| +#include "src/base/platform/platform.h"
|
| +
|
| +#include "test/cctest/cctest.h"
|
| +
|
| +using v8::Local;
|
| +
|
| +namespace {
|
| + // Sampler thread waits on this semaphore.
|
| + v8::base::Semaphore* sampler_semaphore = new v8::base::Semaphore(0);
|
| +
|
| + // V8 thread (the JavaScript code) waits on this semaphore.
|
| + v8::base::Semaphore* v8_semaphore = new v8::base::Semaphore(0);
|
| +
|
| + // The JavaScript calls this function when on full stack depth.
|
| + void SignalAndWaitForSampler(
|
| + const v8::FunctionCallbackInfo<v8::Value>& args) {
|
| + // Tell the sampler that it can take a sample now.
|
| + sampler_semaphore->Signal();
|
| +
|
| + // Wait for the sampler to finish collecting a sample.
|
| + v8_semaphore->Wait();
|
| + }
|
| +}
|
| +
|
| +
|
| +// A thread which collects samples from v8.
|
| +class V8_FINAL SamplerThread : public v8::base::Thread {
|
| + public:
|
| + explicit SamplerThread(v8::Isolate* isolate)
|
| + : Thread(v8::base::Thread::Options("sampler-api-tester")),
|
| + isolate_(isolate) {
|
| + }
|
| +
|
| + virtual ~SamplerThread() {}
|
| +
|
| + virtual void Run() {
|
| + sampler_semaphore->Wait(); // Wait for JS to reach full stack depth.
|
| + isolate_->GetSample(&sample);
|
| + v8_semaphore->Signal(); // Tell JS that sample collection is done.
|
| + }
|
| +
|
| + v8::Sample sample;
|
| +
|
| + private:
|
| + v8::Isolate* isolate_;
|
| +};
|
| +
|
| +
|
| +// A JavaScript function which takes stack depth
|
| +// (minimum value 2) as an argument.
|
| +// When at the bottom of the recursion,
|
| +// the JavaScript code calls into C++ test code,
|
| +// waiting for the sampler to take a sample.
|
| +static const char* sampler_api_test_function = "function func(depth) {"
|
| +" if (depth == 2) SignalAndWaitForSampler();"
|
| +" else return func(depth - 1);"
|
| +"}";
|
| +
|
| +
|
| +// TODO(gholap): Right now, we are just checking whether GetSample
|
| +// gets samples and whether the stack depth is reasonable.
|
| +// After implementing code event listener API, add tests
|
| +// to further verify the correctness of collected samples.
|
| +TEST(StackDepthIsConsistent) {
|
| + v8::Isolate* isolate = CcTest::isolate();
|
| + v8::HandleScope scope(isolate);
|
| + v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
|
| + global->Set(
|
| + v8::String::NewFromUtf8(isolate, "SignalAndWaitForSampler"),
|
| + v8::FunctionTemplate::New(isolate, SignalAndWaitForSampler));
|
| +
|
| + LocalContext env(isolate, NULL, global);
|
| + std::string source(sampler_api_test_function);
|
| + source.append("func(8);");
|
| + Local<v8::Script> script = v8::Script::Compile(
|
| + v8::String::NewFromUtf8(isolate, source.c_str()));
|
| + SamplerThread sampler(isolate);
|
| +
|
| + sampler.Start();
|
| + script->Run();
|
| + sampler.Join();
|
| +
|
| + CHECK_EQ(8, sampler.sample.frames_count);
|
| +}
|
| +
|
| +
|
| +TEST(StackDepthDoesNotExceedMaxValue) {
|
| + v8::Isolate* isolate = CcTest::isolate();
|
| + v8::HandleScope scope(isolate);
|
| + v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
|
| + global->Set(
|
| + v8::String::NewFromUtf8(isolate, "SignalAndWaitForSampler"),
|
| + v8::FunctionTemplate::New(isolate, SignalAndWaitForSampler));
|
| +
|
| + LocalContext env(isolate, NULL, global);
|
| + std::string source(sampler_api_test_function);
|
| + source.append("func(300);");
|
| + Local<v8::Script> script = v8::Script::Compile(
|
| + v8::String::NewFromUtf8(isolate, source.c_str()));
|
| + SamplerThread sampler(isolate);
|
| +
|
| + sampler.Start();
|
| + script->Run();
|
| + sampler.Join();
|
| +
|
| + int MAX_FRAMES_COUNT = v8::Sample::kMaxFramesCount;
|
| + CHECK_EQ(MAX_FRAMES_COUNT, sampler.sample.frames_count);
|
| +}
|
|
|