1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdio.h>
6 #include <threads.h>
7 
8 #include <zircon/syscalls.h>
9 #include <zircon/time.h>
10 #include <zircon/types.h>
11 
12 #define NUM_THREADS 1000
13 
thread_func(void * arg)14 static int thread_func(void* arg) {
15   return 0;
16 }
17 
thread_create(thrd_t * thread)18 static void thread_create(thrd_t* thread) {
19   int ret = thrd_create_with_name(thread, thread_func, NULL, "stress");
20   if (ret != thrd_success) {
21     printf("Failed to create thread: %d", ret);
22   }
23 }
24 
thread_join(thrd_t thread)25 static void thread_join(thrd_t thread) {
26   int ret = thrd_join(thread, NULL);
27   if (ret != thrd_success) {
28     printf("Failed to join thread: %d", ret);
29   }
30 }
31 
main(int argc,char ** argv)32 int main(int argc, char** argv) {
33     printf("Running thread stress test...\n");
34     thrd_t thread[NUM_THREADS];
35     while (true) {
36         zx_time_t start = zx_clock_get_monotonic();
37         for (int i = 0; i != NUM_THREADS; ++i) {
38             thread_create(&thread[i]);
39         }
40         zx_time_t create = zx_clock_get_monotonic();
41         for (int i = 0; i != NUM_THREADS; ++i) {
42             thread_join(thread[i]);
43         }
44         zx_time_t join = zx_clock_get_monotonic();
45         printf(
46             "%d threads in %.2fs (create %.2fs, join %.2fs)\n",
47             NUM_THREADS,
48             zx_time_sub_time(join, start) / 1e9,
49             zx_time_sub_time(create, start) / 1e9,
50             zx_time_sub_time(join, create) / 1e9);
51     }
52     return 0;
53 }
54