aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/array/sort_benchmark.cpp
blob: ba95c6633323437d28cf998eb22ea2bb7b99ccdb (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/rusage.h>
#include <vespa/vespalib/util/array.hpp>
#include <csignal>
#include <algorithm>

#include <vespa/log/log.h>
LOG_SETUP("sort_benchmark");

using namespace vespalib;

class Test : public TestApp
{
public:
private:
    template<typename T>
    vespalib::Array<T> create(size_t count);
    template<typename T>
    void sortDirect(size_t count);
    template<typename T>
    void sortInDirect(size_t count);
    int Main() override;
};

template<size_t N>
class TT
{
public:
    TT(uint64_t v) : _v(v) { }
    bool operator < (const TT & rhs) const { return _v < rhs._v; }
private:
    uint64_t _v;
    uint8_t  _payLoad[N - sizeof(uint64_t)];
};

template <typename T>
class I
{
public:
    I(const T * p) : _p(p) { }
    bool operator < (const I & rhs) const { return *_p < *rhs._p; }
private:
    const T * _p;
};

template<typename T>
vespalib::Array<T>
Test::create(size_t count)
{
    vespalib::Array<T> v;
    v.reserve(count);
    srand(0);
    for (size_t i(0); i < count; i++) {
        v.push_back(rand());
    }
    return v;
}

template<typename T>
void Test::sortDirect(size_t count)
{
    vespalib::Array<T> v(create<T>(count));
    LOG(info, "Running sortDirect with %ld count and payload of %ld", v.size(), sizeof(T));
    for (size_t j=0; j < 10; j++) {
        vespalib::Array<T> t(v);
        std::sort(t.begin(), t.end());
    }
}

template<typename T>
void Test::sortInDirect(size_t count)
{
    vespalib::Array<T> k(create<T>(count));
    LOG(info, "Running sortInDirect with %ld count and payload of %ld", k.size(), sizeof(T));
    vespalib::Array< I<T> > v;
    v.reserve(k.size());
    for (size_t i(0), m(k.size()); i < m; i++) {
        v.push_back(&k[i]);
    }
    for (size_t j=0; j < 10; j++) {
        vespalib::Array< I<T> > t(v);
        std::sort(t.begin(), t.end());
    }
}

int
Test::Main()
{
    std::string type("sortdirect");
    size_t count = 1000000;
    size_t payLoad = 0;
    if (_argc > 1) {
        type = _argv[1];
    }
    if (_argc > 2) {
        count = strtol(_argv[2], NULL, 0);
    }
    if (_argc > 3) {
        payLoad = strtol(_argv[3], NULL, 0);
    }
    TEST_INIT("sort_benchmark");
    steady_time start(steady_clock::now());
    if (payLoad < 8) {
        using T = TT<8>;
        if (type == "sortdirect") {
            sortDirect<T>(count);
        } else if (type == "sortindirect") {
            sortInDirect<T>(count);
        } else {
            LOG(warning, "type '%s' is unknown", type.c_str());
        }
    } else if (payLoad < 16) {
        using T = TT<16>;
        if (type == "sortdirect") {
            sortDirect<T>(count);
        } else if (type == "sortindirect") {
            sortInDirect<T>(count);
        } else {
            LOG(warning, "type '%s' is unknown", type.c_str());
        }
    } else if (payLoad < 32) {
        using T = TT<32>;
        if (type == "sortdirect") {
            sortDirect<T>(count);
        } else if (type == "sortindirect") {
            sortInDirect<T>(count);
        } else {
            LOG(warning, "type '%s' is unknown", type.c_str());
        }
    } else if (payLoad < 64) {
        using T = TT<64>;
        if (type == "sortdirect") {
            sortDirect<T>(count);
        } else if (type == "sortindirect") {
            sortInDirect<T>(count);
        } else {
            LOG(warning, "type '%s' is unknown", type.c_str());
        }
    } else if (payLoad < 128) {
        using T = TT<128>;
        if (type == "sortdirect") {
            sortDirect<T>(count);
        } else if (type == "sortindirect") {
            sortInDirect<T>(count);
        } else {
            LOG(warning, "type '%s' is unknown", type.c_str());
        }
    } else if (payLoad < 256) {
        using T = TT<256>;
        if (type == "sortdirect") {
            sortDirect<T>(count);
        } else if (type == "sortindirect") {
            sortInDirect<T>(count);
        } else {
            LOG(warning, "type '%s' is unknown", type.c_str());
        }
    } else if (payLoad < 512) {
        using T = TT<512>;
        if (type == "sortdirect") {
            sortDirect<T>(count);
        } else if (type == "sortindirect") {
            sortInDirect<T>(count);
        } else {
            LOG(warning, "type '%s' is unknown", type.c_str());
        }
    } else {
        using T = TT<1024>;
        LOG(info, "Payload %ld is too big to make any sense. Using %ld.", payLoad, sizeof(T));
        if (type == "sortdirect") {
            sortDirect<T>(count);
        } else if (type == "sortindirect") {
            sortInDirect<T>(count);
        } else {
            LOG(warning, "type '%s' is unknown", type.c_str());
        }
    }
    LOG(info, "rusage = {\n%s\n}", vespalib::RUsage::createSelf(start).toString().c_str());
    ASSERT_EQUAL(0, kill(0, SIGPROF));
    TEST_DONE();
}

TEST_APPHOOK(Test);