OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // Tests of sampler functionalities. |
| 5 |
| 6 #include "src/libsampler/v8-sampler.h" |
| 7 |
| 8 #include "src/base/platform/platform.h" |
| 9 #include "test/cctest/cctest.h" |
| 10 |
| 11 |
| 12 namespace v8 { |
| 13 namespace sampler { |
| 14 |
| 15 namespace { |
| 16 |
| 17 class TestSamplingThread : public base::Thread { |
| 18 public: |
| 19 static const int kSamplerThreadStackSize = 64 * 1024; |
| 20 |
| 21 explicit TestSamplingThread(Sampler* sampler) |
| 22 : Thread(base::Thread::Options("TestSamplingThread", |
| 23 kSamplerThreadStackSize)), |
| 24 sampler_(sampler) {} |
| 25 |
| 26 // Implement Thread::Run(). |
| 27 virtual void Run() { |
| 28 while (true) { |
| 29 if (!sampler_->IsProfiling()) break; |
| 30 sampler_->DoSample(); |
| 31 base::OS::Sleep(base::TimeDelta::FromMilliseconds(10)); |
| 32 } |
| 33 } |
| 34 |
| 35 private: |
| 36 Sampler* sampler_; |
| 37 }; |
| 38 |
| 39 |
| 40 class TestSampler : public Sampler { |
| 41 public: |
| 42 explicit TestSampler(Isolate* isolate) : Sampler(isolate) {} |
| 43 |
| 44 void SampleStack(const v8::RegisterState& regs) override { |
| 45 void* frames[Sampler::kMaxFramesCount]; |
| 46 SampleInfo sample_info; |
| 47 isolate()->GetStackSample(regs, reinterpret_cast<void**>(frames), |
| 48 Sampler::kMaxFramesCount, &sample_info); |
| 49 if (is_counting_samples_) { |
| 50 if (sample_info.vm_state == JS) ++js_sample_count_; |
| 51 if (sample_info.vm_state == EXTERNAL) ++external_sample_count_; |
| 52 } |
| 53 } |
| 54 }; |
| 55 |
| 56 |
| 57 class TestApiCallbacks { |
| 58 public: |
| 59 explicit TestApiCallbacks(int min_duration_ms) |
| 60 : min_duration_ms_(min_duration_ms) {} |
| 61 |
| 62 static void Getter(v8::Local<v8::String> name, |
| 63 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 64 TestApiCallbacks* data = FromInfo(info); |
| 65 data->Wait(); |
| 66 } |
| 67 |
| 68 static void Setter(v8::Local<v8::String> name, |
| 69 v8::Local<v8::Value> value, |
| 70 const v8::PropertyCallbackInfo<void>& info) { |
| 71 TestApiCallbacks* data = FromInfo(info); |
| 72 data->Wait(); |
| 73 } |
| 74 |
| 75 static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 76 TestApiCallbacks* data = FromInfo(info); |
| 77 data->Wait(); |
| 78 } |
| 79 |
| 80 private: |
| 81 void Wait() { |
| 82 double start = v8::base::OS::TimeCurrentMillis(); |
| 83 double duration = 0; |
| 84 while (duration < min_duration_ms_) { |
| 85 v8::base::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1)); |
| 86 duration = v8::base::OS::TimeCurrentMillis() - start; |
| 87 } |
| 88 } |
| 89 |
| 90 template <typename T> |
| 91 static TestApiCallbacks* FromInfo(const T& info) { |
| 92 void* data = v8::External::Cast(*info.Data())->Value(); |
| 93 return reinterpret_cast<TestApiCallbacks*>(data); |
| 94 } |
| 95 |
| 96 int min_duration_ms_; |
| 97 }; |
| 98 |
| 99 |
| 100 static void RunSampler(v8::Local<v8::Context> env, |
| 101 v8::Local<v8::Function> function, |
| 102 v8::Local<v8::Value> argv[], int argc, |
| 103 unsigned min_js_samples = 0, |
| 104 unsigned min_external_samples = 0) { |
| 105 Sampler::SetUp(); |
| 106 TestSampler* sampler = new TestSampler(env->GetIsolate()); |
| 107 TestSamplingThread* thread = new TestSamplingThread(sampler); |
| 108 sampler->IncreaseProfilingDepth(); |
| 109 sampler->Start(); |
| 110 sampler->StartCountingSamples(); |
| 111 thread->StartSynchronously(); |
| 112 do { |
| 113 function->Call(env, env->Global(), argc, argv).ToLocalChecked(); |
| 114 } while (sampler->js_sample_count() < min_js_samples || |
| 115 sampler->external_sample_count() < min_external_samples); |
| 116 sampler->Stop(); |
| 117 sampler->DecreaseProfilingDepth(); |
| 118 thread->Join(); |
| 119 Sampler::TearDown(); |
| 120 } |
| 121 |
| 122 } // namespace |
| 123 |
| 124 static const char* sampler_test_source = "function start(count) {\n" |
| 125 " for (var i = 0; i < count; i++) {\n" |
| 126 " var o = instance.foo;\n" |
| 127 " instance.foo = o + 1;\n" |
| 128 " }\n" |
| 129 "}\n"; |
| 130 |
| 131 static v8::Local<v8::Function> GetFunction(v8::Local<v8::Context> env, |
| 132 const char* name) { |
| 133 return v8::Local<v8::Function>::Cast( |
| 134 env->Global()->Get(env, v8_str(name)).ToLocalChecked()); |
| 135 } |
| 136 |
| 137 |
| 138 TEST(LibSamplerCollectSample) { |
| 139 LocalContext env; |
| 140 v8::Isolate* isolate = env->GetIsolate(); |
| 141 v8::HandleScope scope(isolate); |
| 142 |
| 143 v8::Local<v8::FunctionTemplate> func_template = |
| 144 v8::FunctionTemplate::New(isolate); |
| 145 v8::Local<v8::ObjectTemplate> instance_template = |
| 146 func_template->InstanceTemplate(); |
| 147 |
| 148 TestApiCallbacks accessors(1); |
| 149 v8::Local<v8::External> data = |
| 150 v8::External::New(isolate, &accessors); |
| 151 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter, |
| 152 &TestApiCallbacks::Setter, data); |
| 153 v8::Local<v8::Function> func = |
| 154 func_template->GetFunction(env.local()).ToLocalChecked(); |
| 155 v8::Local<v8::Object> instance = |
| 156 func->NewInstance(env.local()).ToLocalChecked(); |
| 157 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust(); |
| 158 |
| 159 CompileRun(sampler_test_source); |
| 160 v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
| 161 |
| 162 int32_t repeat_count = 100; |
| 163 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)}; |
| 164 RunSampler(env.local(), function, args, arraysize(args), 100, 100); |
| 165 } |
| 166 |
| 167 } // namespace sampler |
| 168 } // namespace v8 |
OLD | NEW |