Chromium Code Reviews| Index: runtime/vm/dart_api_impl_test.cc |
| =================================================================== |
| --- runtime/vm/dart_api_impl_test.cc (revision 6173) |
| +++ runtime/vm/dart_api_impl_test.cc (working copy) |
| @@ -4740,6 +4740,86 @@ |
| Isolate::SetInterruptCallback(saved); |
| } |
| + |
| +// The specific api functions called here are a bit arbitrary. We are |
| +// trying to get a sense of the overhead for using the dart api. |
| +void UseDartApi(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + int count = Dart_GetNativeArgumentCount(args); |
| + EXPECT_EQ(2, count); |
| + |
| + // Get the receiver. |
| + Dart_Handle recv = Dart_GetNativeArgument(args, 0); |
| + EXPECT(!Dart_IsError(recv)); |
| + |
| + // Get param. |
| + Dart_Handle param = Dart_GetNativeArgument(args, 1); |
| + EXPECT(!Dart_IsError(param)); |
| + EXPECT(Dart_IsInteger(param)); |
| + bool fits = false; |
| + Dart_Handle result = Dart_IntegerFitsIntoInt64(param, &fits); |
| + EXPECT(!Dart_IsError(result) && fits); |
| + int64_t value1; |
| + result = Dart_IntegerToInt64(param, &value1); |
| + EXPECT(!Dart_IsError(result)); |
| + |
| + // Get receiver.field. |
| + Dart_Handle field_name = Dart_NewString("field"); |
|
Anton Muhin
2012/04/04 18:12:36
this might be a separate issue. and DOM bindings
turnidge
2012/04/04 19:40:22
Changed the benchmark to do native field access, t
|
| + Dart_Handle field_value = Dart_GetField(recv, field_name); |
| + EXPECT(!Dart_IsError(field_value)); |
| + EXPECT(Dart_IsInteger(field_value)); |
| + fits = false; |
| + result = Dart_IntegerFitsIntoInt64(field_value, &fits); |
| + EXPECT(!Dart_IsError(result) && fits); |
| + int64_t value2; |
| + result = Dart_IntegerToInt64(field_value, &value2); |
| + EXPECT(!Dart_IsError(result)); |
| + EXPECT_EQ(7, value2); |
| + |
| + // Return param + receiver.field. |
| + Dart_SetReturnValue(args, Dart_NewInteger(value1 + value2)); |
|
Anton Muhin
2012/04/04 18:12:36
just in case: you may want to assert you always fi
turnidge
2012/04/04 19:40:22
Added some range checks for param, which should ke
|
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +static Dart_NativeFunction bm_uda_lookup(Dart_Handle name, int argument_count) { |
| + return reinterpret_cast<Dart_NativeFunction>(&UseDartApi); |
| +} |
| + |
| + |
| +TEST_CASE(Benchmark_UseDartApi) { |
| + const char* kScriptChars = |
| + "class Class {\n" |
| + " int field = 7;\n" |
| + " int method(int param) native 'Name_Does_Not_Matter';\n" |
| + "}\n" |
| + "\n" |
| + "double benchmark(int count) {\n" |
| + " Class c = new Class();\n" |
| + " Stopwatch sw = new Stopwatch.start();\n" |
| + " for (int i = 0; i < count; i++) {\n" |
| + " c.method(i);\n" |
| + " }\n" |
| + " sw.stop();\n" |
| + " return sw.elapsedInUs() / count;\n" |
| + "}\n"; |
| + |
| + Dart_Handle lib = TestCase::LoadTestScript( |
| + kScriptChars, |
| + reinterpret_cast<Dart_NativeEntryResolver>(bm_uda_lookup)); |
| + Dart_Handle args[1]; |
| + args[0] = Dart_NewInteger(10000); |
| + Dart_Handle result = Dart_Invoke(lib, |
| + Dart_NewString("benchmark"), |
| + 1, |
| + args); |
| + EXPECT_VALID(result); |
| + EXPECT(Dart_IsDouble(result)); |
| + double out; |
| + result = Dart_DoubleValue(result, &out); |
| + fprintf(stderr, "Benchmark_UseDartApi: %f us per iteration\n", out); |
| +} |
| + |
| #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| } // namespace dart |