summaryrefslogtreecommitdiffstats
path: root/vbench/src/tests/app_vbench/app_vbench_test.cpp
blob: 366fe4ce871f36d6f7736960e2578fd7eaed367a (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>
#include <vbench/test/all.h>
#include <vespa/vespalib/util/slaveproc.h>

using namespace vbench;
using vespalib::SlaveProc;

using InputReader = vespalib::InputReader;
using OutputWriter = vespalib::OutputWriter;

bool endsWith(const Memory &mem, const string &str) {
    return (mem.size < str.size()) ? false
        : (strncmp(mem.data + mem.size - str.size(), str.data(), str.size()) == 0);
}

void readUntil(Input &input, SimpleBuffer &buffer, const string &end) {
    InputReader in(input);
    while (!endsWith(buffer.get(), end)) {
        char c = in.read();
        if (in.failed()) {
            return;
        }
        buffer.reserve(1).data[0] = c;
        buffer.commit(1);
    }
}

TEST("vbench usage") {
    std::string out;
    EXPECT_FALSE(SlaveProc::run("../../apps/vbench/vbench_app", out));
    fprintf(stderr, "%s\n", out.c_str());
}

TEST_MT_F("run vbench", 2, ServerSocket()) {
    if (thread_id == 0) {
        for (;;) {
            Stream::UP stream = f1.accept();
            if (stream.get() == 0) {
                break;
            }
            SimpleBuffer ignore;
            readUntil(*stream, ignore, "\r\n\r\n");
            OutputWriter out(*stream, 256);
            out.write("HTTP/1.1 200\r\n");
            out.write("content-length: 4\r\n");
            out.write("X-Yahoo-Vespa-NumHits 10\r\n");
            out.write("X-Yahoo-Vespa-TotalHitCount 100\r\n");
            out.write("\r\n");
            out.write("data");
        }
    } else {
        std::string out;
        EXPECT_TRUE(SlaveProc::run(strfmt("sed -i 's/_LOCAL_PORT_/%d/' vbench.cfg", f1.port()).c_str()));
        EXPECT_TRUE(SlaveProc::run("../../apps/vbench/vbench_app run vbench.cfg 2> vbench.out", out));
        fprintf(stderr, "%s\n", out.c_str());
        f1.close();
    }
}

TEST_MAIN_WITH_PROCESS_PROXY() { TEST_RUN_ALL(); }