| 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/ping_pong.def"
|
|---|
| 44 | #include "ipc/ns_ping.def"
|
|---|
| 45 | { NULL, NULL, NULL }
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | static bool run_benchmark(benchmark_t *bench)
|
|---|
| 49 | {
|
|---|
| 50 | /* Execute the benchmarl */
|
|---|
| 51 | const char *ret = bench->entry();
|
|---|
| 52 |
|
|---|
| 53 | if (ret == NULL) {
|
|---|
| 54 | printf("\nBenchmark completed\n");
|
|---|
| 55 | return true;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | printf("\n%s\n", ret);
|
|---|
| 59 | return false;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static int run_benchmarks(void)
|
|---|
| 63 | {
|
|---|
| 64 | benchmark_t *bench;
|
|---|
| 65 | unsigned int i = 0;
|
|---|
| 66 | unsigned int n = 0;
|
|---|
| 67 |
|
|---|
| 68 | char *failed_names = NULL;
|
|---|
| 69 |
|
|---|
| 70 | printf("\n*** Running all benchmarks ***\n\n");
|
|---|
| 71 |
|
|---|
| 72 | for (bench = benchmarks; bench->name != NULL; bench++) {
|
|---|
| 73 | printf("%s (%s)\n", bench->name, bench->desc);
|
|---|
| 74 | if (run_benchmark(bench)) {
|
|---|
| 75 | i++;
|
|---|
| 76 | continue;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | if (!failed_names) {
|
|---|
| 80 | failed_names = str_dup(bench->name);
|
|---|
| 81 | } else {
|
|---|
| 82 | char *f = NULL;
|
|---|
| 83 | asprintf(&f, "%s, %s", failed_names, bench->name);
|
|---|
| 84 | if (!f) {
|
|---|
| 85 | printf("Out of memory.\n");
|
|---|
| 86 | abort();
|
|---|
| 87 | }
|
|---|
| 88 | free(failed_names);
|
|---|
| 89 | failed_names = f;
|
|---|
| 90 | }
|
|---|
| 91 | n++;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | printf("\nCompleted, %u benchmarks run, %u succeeded.\n", i + n, i);
|
|---|
| 95 | if (failed_names)
|
|---|
| 96 | printf("Failed benchmarks: %s\n", failed_names);
|
|---|
| 97 |
|
|---|
| 98 | return n;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static void list_benchmarks(void)
|
|---|
| 102 | {
|
|---|
| 103 | size_t len = 0;
|
|---|
| 104 | benchmark_t *bench;
|
|---|
| 105 | for (bench = benchmarks; bench->name != NULL; bench++) {
|
|---|
| 106 | if (str_length(bench->name) > len)
|
|---|
| 107 | len = str_length(bench->name);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | unsigned int _len = (unsigned int) len;
|
|---|
| 111 | if ((_len != len) || (((int) _len) < 0)) {
|
|---|
| 112 | printf("Command length overflow\n");
|
|---|
| 113 | return;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | for (bench = benchmarks; bench->name != NULL; bench++)
|
|---|
| 117 | printf("%-*s %s\n", _len, bench->name, bench->desc);
|
|---|
| 118 |
|
|---|
| 119 | printf("%-*s Run all benchmarks\n", _len, "*");
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | int main(int argc, char *argv[])
|
|---|
| 123 | {
|
|---|
| 124 | if (argc < 2) {
|
|---|
| 125 | printf("Usage:\n\n");
|
|---|
| 126 | printf("%s <benchmark>\n\n", argv[0]);
|
|---|
| 127 | list_benchmarks();
|
|---|
| 128 | return 0;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | if (str_cmp(argv[1], "*") == 0) {
|
|---|
| 132 | return run_benchmarks();
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | benchmark_t *bench;
|
|---|
| 136 | for (bench = benchmarks; bench->name != NULL; bench++) {
|
|---|
| 137 | if (str_cmp(argv[1], bench->name) == 0) {
|
|---|
| 138 | return (run_benchmark(bench) ? 0 : -1);
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | printf("Unknown benchmark \"%s\"\n", argv[1]);
|
|---|
| 143 | return -2;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /** @}
|
|---|
| 147 | */
|
|---|