Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Side by Side Diff: test/cctest/libsampler/test-sampler.cc

Issue 1922303002: Create libsampler as V8 sampler library. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/libsampler/v8-sampler.cc ('K') | « src/v8.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 is_warming_up_(false) {}
62
63 static void Getter(v8::Local<v8::String> name,
64 const v8::PropertyCallbackInfo<v8::Value>& info) {
65 TestApiCallbacks* data = FromInfo(info);
66 data->Wait();
67 }
68
69 static void Setter(v8::Local<v8::String> name,
70 v8::Local<v8::Value> value,
71 const v8::PropertyCallbackInfo<void>& info) {
72 TestApiCallbacks* data = FromInfo(info);
73 data->Wait();
74 }
75
76 static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) {
77 TestApiCallbacks* data = FromInfo(info);
78 data->Wait();
79 }
80
81 void set_warming_up(bool value) { is_warming_up_ = value; }
82
83 private:
84 void Wait() {
85 if (is_warming_up_) return;
86 double start = v8::base::OS::TimeCurrentMillis();
87 double duration = 0;
88 while (duration < min_duration_ms_) {
89 v8::base::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1));
90 duration = v8::base::OS::TimeCurrentMillis() - start;
91 }
92 }
93
94 template <typename T>
95 static TestApiCallbacks* FromInfo(const T& info) {
96 void* data = v8::External::Cast(*info.Data())->Value();
97 return reinterpret_cast<TestApiCallbacks*>(data);
98 }
99
100 int min_duration_ms_;
101 bool is_warming_up_;
102 };
103
104
105 static void RunSampler(v8::Local<v8::Context> env,
106 v8::Local<v8::Function> function,
107 v8::Local<v8::Value> argv[], int argc,
108 unsigned min_js_samples = 0,
109 unsigned min_external_samples = 0) {
110 Sampler::SetUp();
111 TestSampler* sampler = new TestSampler(env->GetIsolate());
112 TestSamplingThread* thread = new TestSamplingThread(sampler);
113 sampler->IncreaseProfilingDepth();
114 sampler->Start();
115 sampler->StartCountingSamples();
116 thread->StartSynchronously();
117 do {
118 function->Call(env, env->Global(), argc, argv).ToLocalChecked();
119 } while (sampler->js_sample_count() < min_js_samples ||
120 sampler->external_sample_count() < min_external_samples);
121 sampler->Stop();
122 sampler->DecreaseProfilingDepth();
123 thread->Join();
124 Sampler::TearDown();
125 }
126
127 } // namespace
128
129 static const char* sampler_test_source = "function start(count) {\n"
130 " for (var i = 0; i < count; i++) {\n"
131 " var o = instance.foo;\n"
132 " instance.foo = o + 1;\n"
133 " }\n"
134 "}\n";
135
136 static v8::Local<v8::Function> GetFunction(v8::Local<v8::Context> env,
137 const char* name) {
138 return v8::Local<v8::Function>::Cast(
139 env->Global()->Get(env, v8_str(name)).ToLocalChecked());
140 }
141
142
143 TEST(LibSamplerCollectSample) {
144 LocalContext env;
145 v8::Isolate* isolate = env->GetIsolate();
146 v8::HandleScope scope(isolate);
147
148 v8::Local<v8::FunctionTemplate> func_template =
149 v8::FunctionTemplate::New(isolate);
150 v8::Local<v8::ObjectTemplate> instance_template =
151 func_template->InstanceTemplate();
152
153 TestApiCallbacks accessors(1);
154 v8::Local<v8::External> data =
155 v8::External::New(isolate, &accessors);
156 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
157 &TestApiCallbacks::Setter, data);
158 v8::Local<v8::Function> func =
159 func_template->GetFunction(env.local()).ToLocalChecked();
160 v8::Local<v8::Object> instance =
161 func->NewInstance(env.local()).ToLocalChecked();
162 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
163
164 CompileRun(sampler_test_source);
165 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
166
167 /*
168 {
169 // Make sure accessors ICs are in monomorphic state before starting
170 // profiling.
171 accessors.set_warming_up(true);
172 int32_t warm_up_iterations = 3;
173 v8::Local<v8::Value> args[] = {
174 v8::Integer::New(isolate, warm_up_iterations)};
175 function->Call(env.local(), env->Global(), arraysize(args), args)
176 .ToLocalChecked();
177 accessors.set_warming_up(false);
178 }
179 */
180
181 int32_t repeat_count = 100;
182 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
183 RunSampler(env.local(), function, args, arraysize(args), 0, 100);
184 }
185
186 } // namespace sampler
187 } // namespace v8
OLDNEW
« src/libsampler/v8-sampler.cc ('K') | « src/v8.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698