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..a2523b5765863ae23bda227f0bd12b8e69b6e3de |
--- /dev/null |
+++ b/test/cctest/test-sampler-api.cc |
@@ -0,0 +1,124 @@ |
+// Copyright 2014 the V8 project authors. All rights reserved. |
+// Redistribution and use in source and binary forms, with or without |
+// modification, are permitted provided that the following conditions are |
+// met: |
+// |
+// * Redistributions of source code must retain the above copyright |
+// notice, this list of conditions and the following disclaimer. |
+// * Redistributions in binary form must reproduce the above |
+// copyright notice, this list of conditions and the following |
+// disclaimer in the documentation and/or other materials provided |
+// with the distribution. |
+// * Neither the name of Google Inc. nor the names of its |
+// contributors may be used to endorse or promote products derived |
+// from this software without specific prior written permission. |
+// |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+// |
+// Tests the public sampling API in include/v8-sampler.h |
+ |
+#include "include/v8-sampler.h" |
+ |
+#include "src/v8.h" |
+ |
+#include "src/base/platform/platform.h" |
+ |
+#include "test/cctest/cctest.h" |
+ |
+using v8::Local; |
+using v8::Script; |
+ |
+using v8::internal::Isolate; |
+ |
+// A thread which collects samples from v8. |
+class SamplerThread : public v8::base::Thread { |
+ public: |
+ explicit SamplerThread(v8::Isolate* isolate); |
+ virtual ~SamplerThread() {} |
+ int biggestSampleStackSize; |
+ |
+ void Stop(); |
+ |
+ // Thread control. |
+ virtual void Run(); |
+ |
+ private: |
+ v8::Isolate* isolate_; |
+ volatile bool done_sampling_; |
+}; |
+ |
+static const int kSamplerThreadStackSize = 64 * 1024; |
+ |
+SamplerThread::SamplerThread(v8::Isolate* isolate) |
+ : Thread(v8::base::Thread::Options("sampler-api-tester", |
+ kSamplerThreadStackSize)), |
+ biggestSampleStackSize(0), |
+ isolate_(isolate), |
+ done_sampling_(false) { |
+} |
+ |
+ |
+void SamplerThread::Stop() { |
+ done_sampling_ = true; |
+ v8::base::OS::Sleep(0); |
+ Join(); |
+} |
+ |
+ |
+void SamplerThread::Run() { |
+ v8::Sample sample; |
+ while (!done_sampling_) { |
+ v8::Sample* sample_ptr = &sample; |
+ sample_ptr = v8::Sampler::GetSample(isolate_, sample_ptr); |
+ if (sample_ptr == NULL) |
+ return; |
+ if (sample_ptr->frames_count > biggestSampleStackSize) |
+ biggestSampleStackSize = sample_ptr->frames_count; |
+ v8::base::OS::Sleep(1); |
+ } |
+} |
+ |
+ |
+// A JavaScript program with stack depth 5 |
+static const char* sampler_api_test_source = "function func(count) {\n" |
+" if (count == 1) {\n" |
+" var a = 0;\n" |
+" for (var i = 0; i < 100000; i++) {\n" |
+" a = i;\n" |
+" }\n" |
+" return a;\n" |
+" }\n" |
+" else return func(count - 1);\n" |
+"}\n" |
+"func(5);\n"; |
+ |
+ |
+// 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) { |
Benedikt Meurer
2014/08/25 04:24:22
Please don't add any more of these fragile tests.
gholap
2014/08/25 17:12:30
Ah! I see your point. Without synchronization, the
|
+ LocalContext env; |
+ v8::HandleScope scope(env->GetIsolate()); |
+ Local<v8::Script> script = v8::Script::Compile( |
+ v8::String::NewFromUtf8(env->GetIsolate(), sampler_api_test_source)); |
+ SamplerThread* sampler = new SamplerThread(env->GetIsolate()); |
+ sampler->Start(); |
+ script->Run(); |
+ sampler->Stop(); |
+ |
+ // Due to v8 issue 3428, sometimes an extra frame is captured. |
+ // Make allowences for that. |
+ CHECK_LE(5, sampler->biggestSampleStackSize); |
+ CHECK_GE(6, sampler->biggestSampleStackSize); |
+} |