Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 // | |
| 28 // Tests the public sampling API in include/v8-sampler.h | |
| 29 | |
| 30 #include "include/v8-sampler.h" | |
| 31 | |
| 32 #include "src/v8.h" | |
| 33 | |
| 34 #include "src/base/platform/platform.h" | |
| 35 | |
| 36 #include "test/cctest/cctest.h" | |
| 37 | |
| 38 using v8::Local; | |
| 39 using v8::Script; | |
| 40 | |
| 41 using v8::internal::Isolate; | |
| 42 | |
| 43 // A thread which collects samples from v8. | |
| 44 class SamplerThread : public v8::base::Thread { | |
| 45 public: | |
| 46 explicit SamplerThread(v8::Isolate* isolate); | |
| 47 virtual ~SamplerThread() {} | |
| 48 int biggestSampleStackSize; | |
| 49 | |
| 50 void Stop(); | |
| 51 | |
| 52 // Thread control. | |
| 53 virtual void Run(); | |
| 54 | |
| 55 private: | |
| 56 v8::Isolate* isolate_; | |
| 57 volatile bool done_sampling_; | |
| 58 }; | |
| 59 | |
| 60 static const int kSamplerThreadStackSize = 64 * 1024; | |
| 61 | |
| 62 SamplerThread::SamplerThread(v8::Isolate* isolate) | |
| 63 : Thread(v8::base::Thread::Options("sampler-api-tester", | |
| 64 kSamplerThreadStackSize)), | |
| 65 biggestSampleStackSize(0), | |
| 66 isolate_(isolate), | |
| 67 done_sampling_(false) { | |
| 68 } | |
| 69 | |
| 70 | |
| 71 void SamplerThread::Stop() { | |
| 72 done_sampling_ = true; | |
| 73 v8::base::OS::Sleep(0); | |
| 74 Join(); | |
| 75 } | |
| 76 | |
| 77 | |
| 78 void SamplerThread::Run() { | |
| 79 v8::Sample sample; | |
| 80 while (!done_sampling_) { | |
| 81 v8::Sample* sample_ptr = &sample; | |
| 82 sample_ptr = v8::Sampler::GetSample(isolate_, sample_ptr); | |
| 83 if (sample_ptr == NULL) | |
| 84 return; | |
| 85 if (sample_ptr->frames_count > biggestSampleStackSize) | |
| 86 biggestSampleStackSize = sample_ptr->frames_count; | |
| 87 v8::base::OS::Sleep(1); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 | |
| 92 // A JavaScript program with stack depth 5 | |
| 93 static const char* sampler_api_test_source = "function func(count) {\n" | |
| 94 " if (count == 1) {\n" | |
| 95 " var a = 0;\n" | |
| 96 " for (var i = 0; i < 100000; i++) {\n" | |
| 97 " a = i;\n" | |
| 98 " }\n" | |
| 99 " return a;\n" | |
| 100 " }\n" | |
| 101 " else return func(count - 1);\n" | |
| 102 "}\n" | |
| 103 "func(5);\n"; | |
| 104 | |
| 105 | |
| 106 // TODO(gholap): Right now, we are just checking whether GetSample | |
| 107 // gets samples and whether the stack depth is reasonable. | |
| 108 // After implementing code event listener API, add tests | |
| 109 // to further verify the correctness of collected samples. | |
| 110 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
| |
| 111 LocalContext env; | |
| 112 v8::HandleScope scope(env->GetIsolate()); | |
| 113 Local<v8::Script> script = v8::Script::Compile( | |
| 114 v8::String::NewFromUtf8(env->GetIsolate(), sampler_api_test_source)); | |
| 115 SamplerThread* sampler = new SamplerThread(env->GetIsolate()); | |
| 116 sampler->Start(); | |
| 117 script->Run(); | |
| 118 sampler->Stop(); | |
| 119 | |
| 120 // Due to v8 issue 3428, sometimes an extra frame is captured. | |
| 121 // Make allowences for that. | |
| 122 CHECK_LE(5, sampler->biggestSampleStackSize); | |
| 123 CHECK_GE(6, sampler->biggestSampleStackSize); | |
| 124 } | |
| OLD | NEW |