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

Side by Side Diff: runtime/vm/dart_api_impl_test.cc

Issue 9979016: Add a unit test Benchmark_UseDartApi to measure dart api overhead. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/dart_api_impl.cc ('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
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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "platform/utils.h" 7 #include "platform/utils.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/dart_api_state.h" 10 #include "vm/dart_api_state.h"
(...skipping 4722 matching lines...) Expand 10 before | Expand all | Expand 10 after
4733 } 4733 }
4734 } 4734 }
4735 4735
4736 // We should have received the expected number of interrupts. 4736 // We should have received the expected number of interrupts.
4737 EXPECT_EQ(kInterruptCount, interrupt_count); 4737 EXPECT_EQ(kInterruptCount, interrupt_count);
4738 4738
4739 // Give the spawned thread enough time to properly exit. 4739 // Give the spawned thread enough time to properly exit.
4740 Isolate::SetInterruptCallback(saved); 4740 Isolate::SetInterruptCallback(saved);
4741 } 4741 }
4742 4742
4743
4744 void InitNativeFields(Dart_NativeArguments args) {
4745 Dart_EnterScope();
4746 int count = Dart_GetNativeArgumentCount(args);
4747 EXPECT_EQ(1, count);
4748
4749 Dart_Handle recv = Dart_GetNativeArgument(args, 0);
4750 EXPECT(!Dart_IsError(recv));
4751 Dart_Handle result = Dart_SetNativeInstanceField(recv, 0, 7);
4752 EXPECT(!Dart_IsError(result));
4753
4754 Dart_ExitScope();
4755 }
4756
4757
4758 // The specific api functions called here are a bit arbitrary. We are
4759 // trying to get a sense of the overhead for using the dart api.
4760 void UseDartApi(Dart_NativeArguments args) {
4761 Dart_EnterScope();
4762 int count = Dart_GetNativeArgumentCount(args);
4763 EXPECT_EQ(3, count);
4764
4765 // Get the receiver.
4766 Dart_Handle recv = Dart_GetNativeArgument(args, 0);
4767 EXPECT(!Dart_IsError(recv));
4768
4769 // Get param1.
4770 Dart_Handle param1 = Dart_GetNativeArgument(args, 1);
4771 EXPECT(!Dart_IsError(param1));
4772 EXPECT(Dart_IsInteger(param1));
4773 bool fits = false;
4774 Dart_Handle result = Dart_IntegerFitsIntoInt64(param1, &fits);
4775 EXPECT(!Dart_IsError(result) && fits);
4776 int64_t value1;
4777 result = Dart_IntegerToInt64(param1, &value1);
4778 EXPECT(!Dart_IsError(result));
4779 EXPECT_LE(0, value1);
4780 EXPECT_LE(value1, 1000000);
4781
4782 // Get native field from receiver.
4783 intptr_t value2;
4784 result = Dart_GetNativeInstanceField(recv, 0, &value2);
4785 EXPECT(!Dart_IsError(result));
4786 EXPECT_EQ(7, value2);
4787
4788 // Return param + receiver.field.
4789 Dart_SetReturnValue(args, Dart_NewInteger(value1 * value2));
4790 Dart_ExitScope();
4791 }
4792
4793
4794 static Dart_NativeFunction bm_uda_lookup(Dart_Handle name, int argument_count) {
4795 const char* cstr = NULL;
4796 Dart_Handle result = Dart_StringToCString(name, &cstr);
4797 EXPECT(!Dart_IsError(result));
4798 if (strcmp(cstr, "init") == 0) {
4799 return InitNativeFields;
4800 } else {
4801 return UseDartApi;
4802 }
4803 }
4804
4805
4806 TEST_CASE(Benchmark_UseDartApi) {
4807 const char* kScriptChars =
4808 "class Class extends NativeFieldsWrapper{\n"
4809 " int init() native 'init';\n"
4810 " int method(int param1, int param2) native 'method';\n"
4811 "}\n"
4812 "\n"
4813 "double benchmark(int count) {\n"
4814 " Class c = new Class();\n"
4815 " c.init();\n"
4816 " Stopwatch sw = new Stopwatch.start();\n"
4817 " for (int i = 0; i < count; i++) {\n"
4818 " c.method(i,7);\n"
4819 " }\n"
4820 " sw.stop();\n"
4821 " return sw.elapsedInUs() / count;\n"
4822 "}\n";
4823
4824 Dart_Handle lib = TestCase::LoadTestScript(
4825 kScriptChars,
4826 reinterpret_cast<Dart_NativeEntryResolver>(bm_uda_lookup));
4827
4828 // Create a native wrapper class with native fields.
4829 Dart_Handle result = Dart_CreateNativeWrapperClass(
4830 lib,
4831 Dart_NewString("NativeFieldsWrapper"),
4832 1);
4833 EXPECT_VALID(result);
4834
4835 Dart_Handle args[1];
4836 args[0] = Dart_NewInteger(100000);
4837 result = Dart_Invoke(lib,
4838 Dart_NewString("benchmark"),
4839 1,
4840 args);
4841 EXPECT_VALID(result);
4842 EXPECT(Dart_IsDouble(result));
4843 double out;
4844 result = Dart_DoubleValue(result, &out);
4845 fprintf(stderr, "Benchmark_UseDartApi: %f us per iteration\n", out);
4846 }
4847
4743 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 4848 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
4744 4849
4745 } // namespace dart 4850 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698