| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/assert.h" |
| 6 |
| 5 #include "vm/benchmark_test.h" | 7 #include "vm/benchmark_test.h" |
| 6 #include "vm/dart_api_impl.h" | 8 #include "vm/dart_api_impl.h" |
| 9 #include "vm/unit_test.h" |
| 7 | 10 |
| 8 namespace dart { | 11 namespace dart { |
| 9 | 12 |
| 10 Benchmark* Benchmark::first_ = NULL; | 13 Benchmark* Benchmark::first_ = NULL; |
| 11 Benchmark* Benchmark::tail_ = NULL; | 14 Benchmark* Benchmark::tail_ = NULL; |
| 12 | 15 |
| 13 void Benchmark::RunAll() { | 16 void Benchmark::RunAll() { |
| 14 Benchmark* benchmark = first_; | 17 Benchmark* benchmark = first_; |
| 15 while (benchmark != NULL) { | 18 while (benchmark != NULL) { |
| 16 benchmark->RunBenchmark(); | 19 benchmark->RunBenchmark(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 Dart_Handle result = Dart_CreateSnapshot(&buffer, &size); | 51 Dart_Handle result = Dart_CreateSnapshot(&buffer, &size); |
| 49 EXPECT(!Dart_IsError(result)); | 52 EXPECT(!Dart_IsError(result)); |
| 50 Timer timer(true, "Core Isolate startup benchmark"); | 53 Timer timer(true, "Core Isolate startup benchmark"); |
| 51 timer.Start(); | 54 timer.Start(); |
| 52 for (int i = 0; i < kNumIterations; i++) { | 55 for (int i = 0; i < kNumIterations; i++) { |
| 53 Dart_Isolate new_isolate = Dart_CreateIsolate(NULL, buffer, NULL, &err); | 56 Dart_Isolate new_isolate = Dart_CreateIsolate(NULL, buffer, NULL, &err); |
| 54 EXPECT(new_isolate != NULL); | 57 EXPECT(new_isolate != NULL); |
| 55 Dart_ShutdownIsolate(); | 58 Dart_ShutdownIsolate(); |
| 56 } | 59 } |
| 57 timer.Stop(); | 60 timer.Stop(); |
| 61 int64_t elapsed_time = timer.TotalElapsedTime(); |
| 62 benchmark->set_score(elapsed_time / kNumIterations); |
| 58 Dart_EnterIsolate(test_isolate); | 63 Dart_EnterIsolate(test_isolate); |
| 59 Dart_ExitScope(); | 64 Dart_ExitScope(); |
| 60 Dart_ShutdownIsolate(); | 65 Dart_ShutdownIsolate(); |
| 61 Dart_EnterIsolate(base_isolate); | 66 Dart_EnterIsolate(base_isolate); |
| 67 } |
| 68 |
| 69 |
| 70 void InitNativeFields(Dart_NativeArguments args) { |
| 71 Dart_EnterScope(); |
| 72 int count = Dart_GetNativeArgumentCount(args); |
| 73 EXPECT_EQ(1, count); |
| 74 |
| 75 Dart_Handle recv = Dart_GetNativeArgument(args, 0); |
| 76 EXPECT(!Dart_IsError(recv)); |
| 77 Dart_Handle result = Dart_SetNativeInstanceField(recv, 0, 7); |
| 78 EXPECT(!Dart_IsError(result)); |
| 79 |
| 80 Dart_ExitScope(); |
| 81 } |
| 82 |
| 83 |
| 84 // The specific api functions called here are a bit arbitrary. We are |
| 85 // trying to get a sense of the overhead for using the dart api. |
| 86 void UseDartApi(Dart_NativeArguments args) { |
| 87 Dart_EnterScope(); |
| 88 int count = Dart_GetNativeArgumentCount(args); |
| 89 EXPECT_EQ(3, count); |
| 90 |
| 91 // Get the receiver. |
| 92 Dart_Handle recv = Dart_GetNativeArgument(args, 0); |
| 93 EXPECT(!Dart_IsError(recv)); |
| 94 |
| 95 // Get param1. |
| 96 Dart_Handle param1 = Dart_GetNativeArgument(args, 1); |
| 97 EXPECT(!Dart_IsError(param1)); |
| 98 EXPECT(Dart_IsInteger(param1)); |
| 99 bool fits = false; |
| 100 Dart_Handle result = Dart_IntegerFitsIntoInt64(param1, &fits); |
| 101 EXPECT(!Dart_IsError(result) && fits); |
| 102 int64_t value1; |
| 103 result = Dart_IntegerToInt64(param1, &value1); |
| 104 EXPECT(!Dart_IsError(result)); |
| 105 EXPECT_LE(0, value1); |
| 106 EXPECT_LE(value1, 1000000); |
| 107 |
| 108 // Get native field from receiver. |
| 109 intptr_t value2; |
| 110 result = Dart_GetNativeInstanceField(recv, 0, &value2); |
| 111 EXPECT(!Dart_IsError(result)); |
| 112 EXPECT_EQ(7, value2); |
| 113 |
| 114 // Return param + receiver.field. |
| 115 Dart_SetReturnValue(args, Dart_NewInteger(value1 * value2)); |
| 116 Dart_ExitScope(); |
| 117 } |
| 118 |
| 119 |
| 120 static Dart_NativeFunction bm_uda_lookup(Dart_Handle name, int argument_count) { |
| 121 const char* cstr = NULL; |
| 122 Dart_Handle result = Dart_StringToCString(name, &cstr); |
| 123 EXPECT(!Dart_IsError(result)); |
| 124 if (strcmp(cstr, "init") == 0) { |
| 125 return InitNativeFields; |
| 126 } else { |
| 127 return UseDartApi; |
| 128 } |
| 129 } |
| 130 |
| 131 |
| 132 BENCHMARK(UseDartApi) { |
| 133 const int kNumIterations = 100000; |
| 134 const char* kScriptChars = |
| 135 "class Class extends NativeFieldsWrapper{\n" |
| 136 " int init() native 'init';\n" |
| 137 " int method(int param1, int param2) native 'method';\n" |
| 138 "}\n" |
| 139 "\n" |
| 140 "void benchmark(int count) {\n" |
| 141 " Class c = new Class();\n" |
| 142 " c.init();\n" |
| 143 " for (int i = 0; i < count; i++) {\n" |
| 144 " c.method(i,7);\n" |
| 145 " }\n" |
| 146 "}\n"; |
| 147 |
| 148 Dart_Handle lib = TestCase::LoadTestScript( |
| 149 kScriptChars, |
| 150 reinterpret_cast<Dart_NativeEntryResolver>(bm_uda_lookup)); |
| 151 |
| 152 // Create a native wrapper class with native fields. |
| 153 Dart_Handle result = Dart_CreateNativeWrapperClass( |
| 154 lib, |
| 155 Dart_NewString("NativeFieldsWrapper"), |
| 156 1); |
| 157 EXPECT(!Dart_IsError(result)); |
| 158 |
| 159 Timer timer(true, "UseDartApi benchmark"); |
| 160 timer.Start(); |
| 161 Dart_Handle args[1]; |
| 162 args[0] = Dart_NewInteger(kNumIterations); |
| 163 Dart_Invoke(lib, |
| 164 Dart_NewString("benchmark"), |
| 165 1, |
| 166 args); |
| 167 timer.Stop(); |
| 62 int64_t elapsed_time = timer.TotalElapsedTime(); | 168 int64_t elapsed_time = timer.TotalElapsedTime(); |
| 63 benchmark->set_score(elapsed_time / kNumIterations); | 169 benchmark->set_score(elapsed_time); |
| 64 } | 170 } |
| 65 | 171 |
| 66 } // namespace dart | 172 } // namespace dart |
| OLD | NEW |