| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
| 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.md file. | |
| 4 | |
| 5 #include <pthread.h> | |
| 6 #include <sys/time.h> | |
| 7 | |
| 8 #include <cstdio> | |
| 9 | |
| 10 #include "include/service_api.h" | |
| 11 #include "include/dartino_api.h" | |
| 12 | |
| 13 #include "src/shared/assert.h" | |
| 14 #include "src/shared/platform.h" | |
| 15 | |
| 16 #include "src/vm/thread_pool.h" | |
| 17 | |
| 18 #include "cc/performance_service.h" | |
| 19 | |
| 20 static pthread_mutex_t mutex; | |
| 21 static pthread_cond_t cond; | |
| 22 static int status = 0; | |
| 23 | |
| 24 static const int kDone = 1; | |
| 25 static const int kCallCount = 10000; | |
| 26 | |
| 27 static dartino::Monitor* echo_monitor = dartino::Platform::CreateMonitor(); | |
| 28 static bool echo_async_done = false; | |
| 29 | |
| 30 static uint64_t GetMicroseconds() { | |
| 31 struct timeval tv; | |
| 32 if (gettimeofday(&tv, NULL) < 0) return -1; | |
| 33 uint64_t result = tv.tv_sec * 1000000LL; | |
| 34 result += tv.tv_usec; | |
| 35 return result; | |
| 36 } | |
| 37 | |
| 38 static void EchoCallback(int result, void* data) { | |
| 39 if (result < kCallCount) { | |
| 40 PerformanceService::echoAsync(result + 1, EchoCallback, NULL); | |
| 41 } else { | |
| 42 echo_monitor->Lock(); | |
| 43 echo_async_done = true; | |
| 44 echo_monitor->Notify(); | |
| 45 echo_monitor->Unlock(); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 static void RunEchoTests() { | |
| 50 uint64_t start = GetMicroseconds(); | |
| 51 for (int i = 0; i < kCallCount; i++) { | |
| 52 int result = PerformanceService::echo(i); | |
| 53 ASSERT(result == i); | |
| 54 } | |
| 55 uint64_t end = GetMicroseconds(); | |
| 56 int sync_us = static_cast<int>(end - start); | |
| 57 printf("Sync call took %.2f us.\n", | |
| 58 static_cast<double>(sync_us) / kCallCount); | |
| 59 printf(" - %.2f calls/s\n", (1000000.0 / sync_us) * kCallCount); | |
| 60 | |
| 61 start = GetMicroseconds(); | |
| 62 PerformanceService::echoAsync(0, EchoCallback, NULL); | |
| 63 echo_monitor->Lock(); | |
| 64 while (!echo_async_done) echo_monitor->Wait(); | |
| 65 echo_monitor->Unlock(); | |
| 66 end = GetMicroseconds(); | |
| 67 int async_us = static_cast<int>(end - start); | |
| 68 printf("Async call took %.2f us.\n", | |
| 69 static_cast<double>(async_us) / kCallCount); | |
| 70 printf(" - %.2f calls/s\n", (1000000.0 / async_us) * kCallCount); | |
| 71 } | |
| 72 | |
| 73 static int CountTreeNodes(TreeNode node) { | |
| 74 int sum = 1; | |
| 75 List<TreeNode> children = node.getChildren(); | |
| 76 for (int i = 0; i < children.length(); i++) { | |
| 77 sum += CountTreeNodes(children[i]); | |
| 78 } | |
| 79 return sum; | |
| 80 } | |
| 81 | |
| 82 static void BuildTree(int n, TreeNodeBuilder node) { | |
| 83 if (n > 1) { | |
| 84 List<TreeNodeBuilder> children = node.initChildren(2); | |
| 85 BuildTree(n - 1, children[0]); | |
| 86 BuildTree(n - 1, children[1]); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 static void RunTreeTests() { | |
| 91 const int kTreeDepth = 7; | |
| 92 | |
| 93 uint64_t start = GetMicroseconds(); | |
| 94 MessageBuilder builder(8192); | |
| 95 TreeNodeBuilder built = builder.initRoot<TreeNodeBuilder>(); | |
| 96 BuildTree(kTreeDepth, built); | |
| 97 uint64_t end = GetMicroseconds(); | |
| 98 printf("Building (C++) took %d us.\n", static_cast<int>(end - start)); | |
| 99 | |
| 100 start = GetMicroseconds(); | |
| 101 PerformanceService::countTreeNodes(built); | |
| 102 end = GetMicroseconds(); | |
| 103 printf("Counting (Dart) took %d us.\n", static_cast<int>(end - start)); | |
| 104 | |
| 105 start = GetMicroseconds(); | |
| 106 TreeNode generated = PerformanceService::buildTree(kTreeDepth); | |
| 107 end = GetMicroseconds(); | |
| 108 printf("Building (Dart) took %d us.\n", static_cast<int>(end - start)); | |
| 109 | |
| 110 start = GetMicroseconds(); | |
| 111 CountTreeNodes(generated); | |
| 112 end = GetMicroseconds(); | |
| 113 printf("Counting (C++) took %d us.\n", static_cast<int>(end - start)); | |
| 114 | |
| 115 generated.Delete(); | |
| 116 } | |
| 117 | |
| 118 void EchoInThread(void* data) { | |
| 119 intptr_t value = reinterpret_cast<intptr_t>(data); | |
| 120 for (int i = 0; i < 64; i++) { | |
| 121 int result = PerformanceService::echo(value + i); | |
| 122 ASSERT(result == value + i); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 static void RunThreadTests() { | |
| 127 const int kThreadCount = 32; | |
| 128 dartino::ThreadPool thread_pool(kThreadCount); | |
| 129 thread_pool.Start(); | |
| 130 for (int i = 0; i < kThreadCount; i++) { | |
| 131 while (!thread_pool.TryStartThread(EchoInThread, | |
| 132 reinterpret_cast<void*>(i))) { | |
| 133 } | |
| 134 } | |
| 135 thread_pool.JoinAll(); | |
| 136 } | |
| 137 | |
| 138 static void ChangeStatusAndNotify(int new_status) { | |
| 139 pthread_mutex_lock(&mutex); | |
| 140 status = new_status; | |
| 141 pthread_cond_signal(&cond); | |
| 142 pthread_mutex_unlock(&mutex); | |
| 143 } | |
| 144 | |
| 145 static void WaitForStatus(int expected) { | |
| 146 pthread_mutex_lock(&mutex); | |
| 147 while (expected != status) pthread_cond_wait(&cond, &mutex); | |
| 148 pthread_mutex_unlock(&mutex); | |
| 149 } | |
| 150 | |
| 151 static void* DartThreadEntry(void* arg) { | |
| 152 const char* path = static_cast<char*>(arg); | |
| 153 DartinoSetup(); | |
| 154 DartinoProgram program = DartinoLoadSnapshotFromFile(path); | |
| 155 if (DartinoRunMain(program, 0, NULL) != 0) { | |
| 156 FATAL1("Failed to run snapshot: %s\n", path); | |
| 157 } | |
| 158 DartinoDeleteProgram(program); | |
| 159 DartinoTearDown(); | |
| 160 ChangeStatusAndNotify(kDone); | |
| 161 return NULL; | |
| 162 } | |
| 163 | |
| 164 static void RunSnapshotInNewThread(char* path) { | |
| 165 pthread_t thread; | |
| 166 int result = pthread_create(&thread, NULL, DartThreadEntry, path); | |
| 167 if (result != 0) { | |
| 168 perror("Failed to start thread"); | |
| 169 exit(1); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 static void SetupPerformanceTest(int argc, char** argv) { | |
| 174 pthread_mutex_init(&mutex, NULL); | |
| 175 pthread_cond_init(&cond, NULL); | |
| 176 ServiceApiSetup(); | |
| 177 RunSnapshotInNewThread(argv[1]); | |
| 178 PerformanceService::setup(); | |
| 179 } | |
| 180 | |
| 181 static void TearDownPerformanceTest() { | |
| 182 PerformanceService::tearDown(); | |
| 183 WaitForStatus(kDone); | |
| 184 ServiceApiTearDown(); | |
| 185 } | |
| 186 | |
| 187 int main(int argc, char** argv) { | |
| 188 if (argc < 2) { | |
| 189 printf("Usage: %s <snapshot>\n", argv[0]); | |
| 190 return 1; | |
| 191 } | |
| 192 SetupPerformanceTest(argc, argv); | |
| 193 RunEchoTests(); | |
| 194 RunTreeTests(); | |
| 195 RunThreadTests(); | |
| 196 TearDownPerformanceTest(); | |
| 197 return 0; | |
| 198 } | |
| OLD | NEW |