aboutsummaryrefslogtreecommitdiffstats
path: root/vbench/src/apps/vbench/vbench.cpp
blob: c73f38adaf27d6349bcd9e72247b67e76f444ede (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/vespalib/util/programoptions.h>
#include <vespa/vespalib/util/thread.h>
#include <vespa/vespalib/util/runnable_pair.h>
#include <vbench/vbench/vbench.h>
#include <iostream>

using namespace vbench;

VESPA_THREAD_STACK_TAG(vbench_thread);

using SIG = vespalib::SignalHandler;

struct NotifyDone : public vespalib::Runnable {
    vespalib::Gate &done;
    NotifyDone(vespalib::Gate &d) : done(d) {}
    void run() override {
        done.countDown();
    }
};

void setupSignals() {
    SIG::PIPE.ignore();
    SIG::INT.hook();
    SIG::TERM.hook();
}

int run(const std::string &cfg_name) {
    vespalib::MappedFileInput cfg_file(cfg_name);
    if (!cfg_file.valid()) {
        fprintf(stderr, "could not load config file: %s\n", cfg_name.c_str());
        return 1;
    }
    vespalib::Slime cfg;
    vespalib::Memory mapped_cfg(cfg_file.get().data, cfg_file.get().size);
    if (!vespalib::slime::JsonFormat::decode(mapped_cfg, cfg)) {
        fprintf(stderr, "unable to parse config file: %s\n",
                cfg.toString().c_str());
        return 1;
    }
    setupSignals();
    vespalib::Gate done;
    VBench vbench(cfg);
    NotifyDone notify(done);
    vespalib::RunnablePair runBoth(vbench, notify);
    auto thread = vespalib::thread::start(runBoth, vbench_thread);
    while (!SIG::INT.check() && !SIG::TERM.check() && !done.await(1s)) {}
    if (!done.await(vespalib::duration::zero())) {
        vbench.abort();
        done.await();
    }
    thread.join();
    if (vbench.tainted()) {
        fprintf(stderr, "vbench failed: %s\n",
                vbench.tainted().reason().c_str());
        return 1;
    }
    return 0;
}

int usage(const char *prog) {
    fprintf(stderr, "vbench -- vespa benchmarking tool\n\n");
    fprintf(stderr, "usage: %s run <config-file>\n", prog);
    fprintf(stderr, "  run benchmarking as described in the config file.\n\n");
    return 1;
}

int main(int argc, char **argv) {
    if (argc > 1) {
        std::string mode = argv[1];
        if (mode == "run" && argc == 3) {
            return run(argv[2]);
        }
    }
    return usage(argv[0]);
}