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