| 1 | /*
 | 
|---|
| 2 |  * Copyright (c) 2018 Jiri Svoboda
 | 
|---|
| 3 |  * All rights reserved.
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  * Redistribution and use in source and binary forms, with or without
 | 
|---|
| 6 |  * modification, are permitted provided that the following conditions
 | 
|---|
| 7 |  * are met:
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  * - Redistributions of source code must retain the above copyright
 | 
|---|
| 10 |  *   notice, this list of conditions and the following disclaimer.
 | 
|---|
| 11 |  * - Redistributions in binary form must reproduce the above copyright
 | 
|---|
| 12 |  *   notice, this list of conditions and the following disclaimer in the
 | 
|---|
| 13 |  *   documentation and/or other materials provided with the distribution.
 | 
|---|
| 14 |  * - The name of the author may not be used to endorse or promote products
 | 
|---|
| 15 |  *   derived from this software without specific prior written permission.
 | 
|---|
| 16 |  *
 | 
|---|
| 17 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
|---|
| 18 |  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
|---|
| 19 |  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
|---|
| 20 |  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
|---|
| 21 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
|---|
| 22 |  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
|---|
| 23 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
|---|
| 24 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
|---|
| 25 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
|---|
| 26 |  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /** @addtogroup perf
 | 
|---|
| 30 |  * @{
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | /**
 | 
|---|
| 33 |  * @file
 | 
|---|
| 34 |  */
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include <stdio.h>
 | 
|---|
| 37 | #include <stddef.h>
 | 
|---|
| 38 | #include <stdlib.h>
 | 
|---|
| 39 | #include <str.h>
 | 
|---|
| 40 | #include "perf.h"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | benchmark_t benchmarks[] = {
 | 
|---|
| 43 | #include "ipc/ns_ping.def"
 | 
|---|
| 44 | #include "ipc/ping_pong.def"
 | 
|---|
| 45 | #include "malloc/malloc1.def"
 | 
|---|
| 46 | #include "malloc/malloc2.def"
 | 
|---|
| 47 |         { NULL, NULL, NULL }
 | 
|---|
| 48 | };
 | 
|---|
| 49 | 
 | 
|---|
| 50 | static bool run_benchmark(benchmark_t *bench)
 | 
|---|
| 51 | {
 | 
|---|
| 52 |         /* Execute the benchmarl */
 | 
|---|
| 53 |         const char *ret = bench->entry();
 | 
|---|
| 54 | 
 | 
|---|
| 55 |         if (ret == NULL) {
 | 
|---|
| 56 |                 printf("\nBenchmark completed\n");
 | 
|---|
| 57 |                 return true;
 | 
|---|
| 58 |         }
 | 
|---|
| 59 | 
 | 
|---|
| 60 |         printf("\n%s\n", ret);
 | 
|---|
| 61 |         return false;
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | static int run_benchmarks(void)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |         benchmark_t *bench;
 | 
|---|
| 67 |         unsigned int i = 0;
 | 
|---|
| 68 |         unsigned int n = 0;
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         char *failed_names = NULL;
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         printf("\n*** Running all benchmarks ***\n\n");
 | 
|---|
| 73 | 
 | 
|---|
| 74 |         for (bench = benchmarks; bench->name != NULL; bench++) {
 | 
|---|
| 75 |                 printf("%s (%s)\n", bench->name, bench->desc);
 | 
|---|
| 76 |                 if (run_benchmark(bench)) {
 | 
|---|
| 77 |                         i++;
 | 
|---|
| 78 |                         continue;
 | 
|---|
| 79 |                 }
 | 
|---|
| 80 | 
 | 
|---|
| 81 |                 if (!failed_names) {
 | 
|---|
| 82 |                         failed_names = str_dup(bench->name);
 | 
|---|
| 83 |                 } else {
 | 
|---|
| 84 |                         char *f = NULL;
 | 
|---|
| 85 |                         asprintf(&f, "%s, %s", failed_names, bench->name);
 | 
|---|
| 86 |                         if (!f) {
 | 
|---|
| 87 |                                 printf("Out of memory.\n");
 | 
|---|
| 88 |                                 abort();
 | 
|---|
| 89 |                         }
 | 
|---|
| 90 |                         free(failed_names);
 | 
|---|
| 91 |                         failed_names = f;
 | 
|---|
| 92 |                 }
 | 
|---|
| 93 |                 n++;
 | 
|---|
| 94 |         }
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         printf("\nCompleted, %u benchmarks run, %u succeeded.\n", i + n, i);
 | 
|---|
| 97 |         if (failed_names)
 | 
|---|
| 98 |                 printf("Failed benchmarks: %s\n", failed_names);
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         return n;
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | static void list_benchmarks(void)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |         size_t len = 0;
 | 
|---|
| 106 |         benchmark_t *bench;
 | 
|---|
| 107 |         for (bench = benchmarks; bench->name != NULL; bench++) {
 | 
|---|
| 108 |                 if (str_length(bench->name) > len)
 | 
|---|
| 109 |                         len = str_length(bench->name);
 | 
|---|
| 110 |         }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         unsigned int _len = (unsigned int) len;
 | 
|---|
| 113 |         if ((_len != len) || (((int) _len) < 0)) {
 | 
|---|
| 114 |                 printf("Command length overflow\n");
 | 
|---|
| 115 |                 return;
 | 
|---|
| 116 |         }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         for (bench = benchmarks; bench->name != NULL; bench++)
 | 
|---|
| 119 |                 printf("%-*s %s\n", _len, bench->name, bench->desc);
 | 
|---|
| 120 | 
 | 
|---|
| 121 |         printf("%-*s Run all benchmarks\n", _len, "*");
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | int main(int argc, char *argv[])
 | 
|---|
| 125 | {
 | 
|---|
| 126 |         if (argc < 2) {
 | 
|---|
| 127 |                 printf("Usage:\n\n");
 | 
|---|
| 128 |                 printf("%s <benchmark>\n\n", argv[0]);
 | 
|---|
| 129 |                 list_benchmarks();
 | 
|---|
| 130 |                 return 0;
 | 
|---|
| 131 |         }
 | 
|---|
| 132 | 
 | 
|---|
| 133 |         if (str_cmp(argv[1], "*") == 0) {
 | 
|---|
| 134 |                 return run_benchmarks();
 | 
|---|
| 135 |         }
 | 
|---|
| 136 | 
 | 
|---|
| 137 |         benchmark_t *bench;
 | 
|---|
| 138 |         for (bench = benchmarks; bench->name != NULL; bench++) {
 | 
|---|
| 139 |                 if (str_cmp(argv[1], bench->name) == 0) {
 | 
|---|
| 140 |                         return (run_benchmark(bench) ? 0 : -1);
 | 
|---|
| 141 |                 }
 | 
|---|
| 142 |         }
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         printf("Unknown benchmark \"%s\"\n", argv[1]);
 | 
|---|
| 145 |         return -2;
 | 
|---|
| 146 | }
 | 
|---|
| 147 | 
 | 
|---|
| 148 | /** @}
 | 
|---|
| 149 |  */
 | 
|---|