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..c8367c5eccd9b6cdf9f6f2f61f4d7f751d66c042 |
--- /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* SamplerSemaphore = new v8::base::Semaphore(0); |
Benedikt Meurer
2014/08/29 04:23:49
Nit: Variable naming, SamplerSemaphore -> sampler_
|
+ |
+ // V8 thread (the JavaScript code) waits on this semaphore. |
+ v8::base::Semaphore* V8Semaphore = new v8::base::Semaphore(0); |
Benedikt Meurer
2014/08/29 04:23:49
Nit: Variable naming.
|
+ |
+ // 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. |
+ SamplerSemaphore->Signal(); |
+ |
+ // Wait for the sampler to finish collecting a sample. |
+ V8Semaphore->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() { |
+ SamplerSemaphore->Wait(); // Wait for JS to reach full stack depth. |
+ isolate_->GetSample(&sample); |
+ V8Semaphore->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); |
+} |