Changeset 60029df in mainline for uspace/app/perf/perf.c


Ignore:
Timestamp:
2019-01-03T09:49:54Z (5 years ago)
Author:
Vojtech Horky <vojtech.horky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a362c16
Parents:
043d464f
Message:

perf: add CSV output

Use -o filename to store benchmark results (performance data) in a
simple CSV file for further processing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/perf/perf.c

    r043d464f r60029df  
    3636
    3737#include <assert.h>
     38#include <getopt.h>
    3839#include <math.h>
    3940#include <stdio.h>
     
    4344#include <time.h>
    4445#include <errno.h>
     46#include <str_error.h>
    4547#include <perf.h>
    4648#include <types/casting.h>
     49#include "benchlist.h"
     50#include "csv.h"
    4751#include "perf.h"
    48 #include "benchlist.h"
    4952
    5053#define MIN_DURATION_SECS 10
     
    5558    benchmark_t *bench, uint64_t workload_size)
    5659{
     60        csv_report_add_entry(stopwatch, run_index, bench, workload_size);
     61
    5762        usec_t duration_usec = NSEC2USEC(stopwatch_get_nanos(stopwatch));
    5863
     
    282287
    283288        for (size_t i = 0; i < benchmark_count; i++)
    284                 printf("%-*s %s\n", (int) len, benchmarks[i]->name, benchmarks[i]->desc);
    285 
    286         printf("%-*s Run all benchmarks\n", (int) len, "*");
     289                printf("  %-*s %s\n", (int) len, benchmarks[i]->name, benchmarks[i]->desc);
     290
     291        printf("  %-*s Run all benchmarks\n", (int) len, "*");
     292}
     293
     294static void print_usage(const char *progname)
     295{
     296        printf("Usage: %s [options] <benchmark>\n", progname);
     297        printf("-h, --help                 "
     298            "Print this help and exit\n");
     299        printf("-o, --output filename.csv  "
     300            "Store machine-readable data in filename.csv\n");
     301        printf("<benchmark> is one of the following:\n");
     302        list_benchmarks();
    287303}
    288304
    289305int main(int argc, char *argv[])
    290306{
    291         if (argc < 2) {
    292                 printf("Usage:\n\n");
    293                 printf("%s <benchmark>\n\n", argv[0]);
    294                 list_benchmarks();
    295                 return 0;
    296         }
    297 
    298         if (str_cmp(argv[1], "*") == 0) {
    299                 return run_benchmarks();
    300         }
    301 
    302         for (size_t i = 0; i < benchmark_count; i++) {
    303                 if (str_cmp(argv[1], benchmarks[i]->name) == 0) {
    304                         return (run_benchmark(benchmarks[i]) ? 0 : -1);
    305                 }
    306         }
    307 
    308         printf("Unknown benchmark \"%s\"\n", argv[1]);
    309         return -2;
     307        const char *short_options = "ho:";
     308        struct option long_options[] = {
     309                { "help", optional_argument, NULL, 'h' },
     310                { "output", required_argument, NULL, 'o' },
     311                { 0, 0, NULL, 0 }
     312        };
     313
     314        char *csv_output_filename = NULL;
     315
     316        int opt = 0;
     317        while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) > 0) {
     318                switch (opt) {
     319                case 'h':
     320                        print_usage(*argv);
     321                        return 0;
     322                case 'o':
     323                        csv_output_filename = optarg;
     324                        break;
     325                case -1:
     326                default:
     327                        break;
     328                }
     329        }
     330
     331        if (optind + 1 != argc) {
     332                print_usage(*argv);
     333                fprintf(stderr, "Error: specify one benchmark to run or * for all.\n");
     334                return -3;
     335        }
     336
     337        const char *benchmark = argv[optind];
     338
     339        if (csv_output_filename != NULL) {
     340                errno_t rc = csv_report_open(csv_output_filename);
     341                if (rc != EOK) {
     342                        fprintf(stderr, "Failed to open CSV report '%s': %s\n",
     343                            csv_output_filename, str_error(rc));
     344                        return -4;
     345                }
     346        }
     347
     348        int exit_code = 0;
     349
     350        if (str_cmp(benchmark, "*") == 0) {
     351                exit_code = run_benchmarks();
     352        } else {
     353                bool benchmark_exists = false;
     354                for (size_t i = 0; i < benchmark_count; i++) {
     355                        if (str_cmp(benchmark, benchmarks[i]->name) == 0) {
     356                                benchmark_exists = true;
     357                                exit_code = run_benchmark(benchmarks[i]) ? 0 : -1;
     358                                break;
     359                        }
     360                }
     361                if (!benchmark_exists) {
     362                        printf("Unknown benchmark \"%s\"\n", benchmark);
     363                        exit_code = -2;
     364                }
     365        }
     366
     367        csv_report_close();
     368
     369        return exit_code;
    310370}
    311371
Note: See TracChangeset for help on using the changeset viewer.