Revision: 15220
Author:   [email protected]
Date:     Wed Jun 19 23:46:56 2013
Log:      Introduce THREADED_PROFILED_TEST macro

A bunch of tests in test-api.cc need to be executed two times: with CPU profiler on and off to check different code paths. There are only two such tests at the moment but I'm going to add more and having this logic duplicated in all these tests look awkward. The tests are executed as part of Threading* tests and there is no much sense in running CPU profiler in that case. This change addresses both problems by introducing THREADED_PROFILED_TEST macro which mosly resembles THREADED_TEST except that when such test is executed as a standalone test (not part of a test-api/Threading*) it will first run the test normally and the start CPU profiler and run the test again.

BUG=244580
[email protected]

Review URL: https://codereview.chromium.org/17072002
http://code.google.com/p/v8/source/detail?r=15220

Modified:
 /branches/bleeding_edge/test/cctest/test-api.cc

=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc     Fri Jun 14 03:49:57 2013
+++ /branches/bleeding_edge/test/cctest/test-api.cc     Wed Jun 19 23:46:56 2013
@@ -71,6 +71,25 @@
 using ::v8::UniqueId;
 using ::v8::V8;
 using ::v8::Value;
+
+
+#define THREADED_PROFILED_TEST(Name)                                 \
+  static void Test##Name();                                          \
+  TEST(Name##WithProfiler) {                                         \
+    RunWithProfiler(&Test##Name);                                    \
+  }                                                                  \
+  THREADED_TEST(Name)
+
+void RunWithProfiler(void (*test)()) {
+  LocalContext env;
+  v8::HandleScope scope(env->GetIsolate());
+  v8::Local<v8::String> profile_name = v8::String::New("my_profile1");
+  v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
+
+  cpu_profiler->StartCpuProfiling(profile_name);
+  (*test)();
+  cpu_profiler->DeleteAllCpuProfiles();
+}


 static void ExpectString(const char* code, const char* expected) {
@@ -938,56 +957,33 @@
 template<typename Handler>
 static void TestFunctionTemplateInitializer(Handler handler,
                                             Handler handler_2) {
-  for (int i = 0; i < 2; i++) {
-    bool is_profiling = (i > 0);
-    // Test constructor calls.
-    {
-      LocalContext env;
-      v8::HandleScope scope(env->GetIsolate());
+  // Test constructor calls.
+  {
+    LocalContext env;
+    v8::HandleScope scope(env->GetIsolate());

-      v8::Local<v8::String> profile_name = v8::String::New("my_profile1");
-      v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
-      if (is_profiling) {
-        cpu_profiler->StartCpuProfiling(profile_name);
-      }
-
-      Local<v8::FunctionTemplate> fun_templ =
-          v8::FunctionTemplate::New(handler);
-      Local<Function> fun = fun_templ->GetFunction();
-      env->Global()->Set(v8_str("obj"), fun);
-      Local<Script> script = v8_compile("obj()");
-      for (int i = 0; i < 30; i++) {
-        CHECK_EQ(102, script->Run()->Int32Value());
-      }
-
-      if (is_profiling) {
-        cpu_profiler->StopCpuProfiling(profile_name);
-      }
+    Local<v8::FunctionTemplate> fun_templ =
+        v8::FunctionTemplate::New(handler);
+    Local<Function> fun = fun_templ->GetFunction();
+    env->Global()->Set(v8_str("obj"), fun);
+    Local<Script> script = v8_compile("obj()");
+    for (int i = 0; i < 30; i++) {
+      CHECK_EQ(102, script->Run()->Int32Value());
     }
- // Use SetCallHandler to initialize a function template, should work like
-    // the previous one.
-    {
-      LocalContext env;
-      v8::HandleScope scope(env->GetIsolate());
+  }
+  // Use SetCallHandler to initialize a function template, should work like
+  // the previous one.
+  {
+    LocalContext env;
+    v8::HandleScope scope(env->GetIsolate());

-      v8::Local<v8::String> profile_name = v8::String::New("my_profile2");
-      v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
-      if (is_profiling) {
-        cpu_profiler->StartCpuProfiling(profile_name);
-      }
-
-      Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New();
-      fun_templ->SetCallHandler(handler_2);
-      Local<Function> fun = fun_templ->GetFunction();
-      env->Global()->Set(v8_str("obj"), fun);
-      Local<Script> script = v8_compile("obj()");
-      for (int i = 0; i < 30; i++) {
-        CHECK_EQ(102, script->Run()->Int32Value());
-      }
-
-      if (is_profiling) {
-        cpu_profiler->DeleteAllCpuProfiles();
-      }
+    Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New();
+    fun_templ->SetCallHandler(handler_2);
+    Local<Function> fun = fun_templ->GetFunction();
+    env->Global()->Set(v8_str("obj"), fun);
+    Local<Script> script = v8_compile("obj()");
+    for (int i = 0; i < 30; i++) {
+      CHECK_EQ(102, script->Run()->Int32Value());
     }
   }
 }
@@ -996,44 +992,31 @@
 template<typename Constructor, typename Accessor>
 static void TestFunctionTemplateAccessor(Constructor constructor,
                                          Accessor accessor) {
-  for (int i = 0; i < 2; i++) {
-    bool is_profiling = (i > 0);
-    LocalContext env;
-    v8::HandleScope scope(env->GetIsolate());
+  LocalContext env;
+  v8::HandleScope scope(env->GetIsolate());

-    v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
-    if (is_profiling) {
-      v8::Local<v8::String> profile_name = v8::String::New("my_profile1");
-      cpu_profiler->StartCpuProfiling(profile_name);
-    }
-
-    Local<v8::FunctionTemplate> fun_templ =
-        v8::FunctionTemplate::New(constructor);
-    fun_templ->SetClassName(v8_str("funky"));
-    fun_templ->InstanceTemplate()->SetAccessor(v8_str("m"), accessor);
-    Local<Function> fun = fun_templ->GetFunction();
-    env->Global()->Set(v8_str("obj"), fun);
-    Local<Value> result = v8_compile("(new obj()).toString()")->Run();
-    CHECK_EQ(v8_str("[object funky]"), result);
-    CompileRun("var obj_instance = new obj();");
-    Local<Script> script;
-    script = v8_compile("obj_instance.x");
-    for (int i = 0; i < 30; i++) {
-      CHECK_EQ(1, script->Run()->Int32Value());
-    }
-    script = v8_compile("obj_instance.m");
-    for (int i = 0; i < 30; i++) {
-      CHECK_EQ(239, script->Run()->Int32Value());
-    }
-
-    if (is_profiling) {
-      cpu_profiler->DeleteAllCpuProfiles();
-    }
+  Local<v8::FunctionTemplate> fun_templ =
+      v8::FunctionTemplate::New(constructor);
+  fun_templ->SetClassName(v8_str("funky"));
+  fun_templ->InstanceTemplate()->SetAccessor(v8_str("m"), accessor);
+  Local<Function> fun = fun_templ->GetFunction();
+  env->Global()->Set(v8_str("obj"), fun);
+  Local<Value> result = v8_compile("(new obj()).toString()")->Run();
+  CHECK_EQ(v8_str("[object funky]"), result);
+  CompileRun("var obj_instance = new obj();");
+  Local<Script> script;
+  script = v8_compile("obj_instance.x");
+  for (int i = 0; i < 30; i++) {
+    CHECK_EQ(1, script->Run()->Int32Value());
+  }
+  script = v8_compile("obj_instance.m");
+  for (int i = 0; i < 30; i++) {
+    CHECK_EQ(239, script->Run()->Int32Value());
   }
 }


-THREADED_TEST(FunctionTemplate) {
+THREADED_PROFILED_TEST(FunctionTemplate) {
   TestFunctionTemplateInitializer(handle_call, handle_call_2);
TestFunctionTemplateInitializer(handle_call_indirect, handle_call_indirect_2);
   TestFunctionTemplateInitializer(handle_callback, handle_callback_2);
@@ -1067,39 +1050,26 @@

 template<typename Callback>
 static void TestSimpleCallback(Callback callback) {
-  for (int i = 0; i < 2; i++) {
-    bool is_profiling = i;
-    LocalContext env;
-    v8::HandleScope scope(env->GetIsolate());
-
-    v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
-    if (is_profiling) {
-      v8::Local<v8::String> profile_name = v8::String::New("my_profile1");
-      cpu_profiler->StartCpuProfiling(profile_name);
-    }
-
- v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New();
-    object_template->Set("callback", v8::FunctionTemplate::New(callback));
-    v8::Local<v8::Object> object = object_template->NewInstance();
-    (*env)->Global()->Set(v8_str("callback_object"), object);
-    v8::Handle<v8::Script> script;
-    script = v8_compile("callback_object.callback(17)");
-    for (int i = 0; i < 30; i++) {
-      CHECK_EQ(51424, script->Run()->Int32Value());
-    }
-    script = v8_compile("callback_object.callback(17, 24)");
-    for (int i = 0; i < 30; i++) {
-      CHECK_EQ(51425, script->Run()->Int32Value());
-    }
+  LocalContext env;
+  v8::HandleScope scope(env->GetIsolate());

-    if (is_profiling) {
-      cpu_profiler->DeleteAllCpuProfiles();
-    }
+ v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New();
+  object_template->Set("callback", v8::FunctionTemplate::New(callback));
+  v8::Local<v8::Object> object = object_template->NewInstance();
+  (*env)->Global()->Set(v8_str("callback_object"), object);
+  v8::Handle<v8::Script> script;
+  script = v8_compile("callback_object.callback(17)");
+  for (int i = 0; i < 30; i++) {
+    CHECK_EQ(51424, script->Run()->Int32Value());
+  }
+  script = v8_compile("callback_object.callback(17, 24)");
+  for (int i = 0; i < 30; i++) {
+    CHECK_EQ(51425, script->Run()->Int32Value());
   }
 }


-THREADED_TEST(SimpleCallback) {
+THREADED_PROFILED_TEST(SimpleCallback) {
   TestSimpleCallback(SimpleDirectCallback);
   TestSimpleCallback(SimpleIndirectCallback);
   TestSimpleCallback(SimpleCallback);

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to