summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/perftests.cpp
blob: 6c4ac4751e3d540776987348166fee744b6fedcc (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
 *    Copyright (C) 2008 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects
 *    for all of the code used other than as permitted herein. If you modify
 *    file(s) with this exception, you may extend this exception to your
 *    version of the file(s), but you are not obligated to do so. If you do not
 *    wish to do so, delete this exception statement from your version. If you
 *    delete this exception statement from all source files in the program,
 *    then also delete it in the license file.
 */

/** @file perftests.cpp.cpp : unit tests relating to performance

          The idea herein is tests that run fast and can be part of the normal CI suite.  So no
          tests herein that take a long time to run.  Obviously we need those too, but they will be
          separate.

          These tests use DBDirectClient; they are a bit white-boxish.
*/

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kDefault

#include "mongo/platform/basic.h"

#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <iomanip>
#include <iostream>
#include <mutex>

#include "mongo/config.h"
#include "mongo/db/bson/dotted_path_support.h"
#include "mongo/db/client.h"
#include "mongo/db/db.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/lasterror.h"
#include "mongo/db/storage/mmap_v1/dur_stats.h"
#include "mongo/db/storage/mmap_v1/mmap.h"
#include "mongo/db/storage/storage_options.h"
#include "mongo/dbtests/dbtests.h"
#include "mongo/dbtests/framework_options.h"
#include "mongo/stdx/condition_variable.h"
#include "mongo/stdx/thread.h"
#include "mongo/util/log.h"
#include "mongo/util/timer.h"
#include "mongo/util/version.h"

namespace PerfTests {

using std::cout;
using std::endl;
using std::fixed;
using std::left;
using std::min;
using std::right;
using std::setprecision;
using std::setw;
using std::string;
using std::vector;

namespace dps = ::mongo::dotted_path_support;

const bool profiling = false;

class ClientBase {
public:
    ClientBase() : _client(&_txn) {
        mongo::LastError::get(_txn.getClient()).reset();
    }
    virtual ~ClientBase() {
        mongo::LastError::get(_txn.getClient()).reset();
    }

protected:
    void insert(const char* ns, BSONObj o) {
        _client.insert(ns, o);
    }
    void update(const char* ns, BSONObj q, BSONObj o, bool upsert = 0) {
        _client.update(ns, Query(q), o, upsert);
    }
    bool error() {
        return !_client.getPrevError().getField("err").isNull();
    }

    DBClientBase* client() {
        return &_client;
    }
    OperationContext* txn() {
        return &_txn;
    }

private:
    const ServiceContext::UniqueOperationContext _txnPtr = cc().makeOperationContext();
    OperationContext& _txn = *_txnPtr;
    DBDirectClient _client;
};

static std::shared_ptr<DBClientConnection> conn;
static string _perfhostname;

class B : public ClientBase {
    string _ns;

protected:
    const char* ns() {
        return _ns.c_str();
    }

    // anything you want to do before being timed
    virtual void prep() {}

    // anything you want to do before threaded test
    virtual void prepThreaded() {}

    virtual void timed() = 0;

    // optional 2nd test phase to be timed separately. You must provide it with a unique
    // name in order for it to run by overloading 'name2'.
    virtual void timed2(DBClientBase*) {}

    // return name of second test.
    virtual string name2() {
        return name();
    }

    virtual void post() {}

    virtual string name() = 0;

    // how long to run test.  0 is a sentinel which means just run the timed() method once and time
    // it.
    virtual int howLongMillis() {
        return profiling ? 30000 : 5000;
    }

    /* override if your test output doesn't need that */
    virtual bool showDurStats() {
        return true;
    }

public:
    virtual unsigned batchSize() {
        return 50;
    }

    void say(unsigned long long n, long long us, string s) {
        unsigned long long rps = (n * 1000 * 1000) / (us > 0 ? us : 1);
        cout << "stats " << setw(42) << left << s << ' ' << right << setw(9) << rps << ' ' << right
             << setw(5) << us / 1000 << "ms ";
        if (showDurStats()) {
            cout << dur::stats.curr()->_asCSV();
        }
        cout << endl;

        if (conn && !conn->isFailed()) {
            const char* ns = "perf.pstats";
            if (frameworkGlobalParams.perfHist) {
                static bool needver = true;
                try {
                    // try to report rps from last time */
                    Query q;
                    {
                        BSONObjBuilder b;
                        b.append("host", _perfhostname);
                        b.append("test", s);
                        b.append("dur", storageGlobalParams.dur);
                        DEV {
                            b.append("info.DEBUG", true);
                        }
                        else b.appendNull("info.DEBUG");
                        if (sizeof(int*) == 4)
                            b.append("info.bits", 32);
                        else
                            b.appendNull("info.bits");
                        q = Query(b.obj()).sort("when", -1);
                    }
                    BSONObj fields = BSON("rps" << 1 << "info" << 1);
                    vector<BSONObj> v;
                    conn->findN(v, ns, q, frameworkGlobalParams.perfHist, 0, &fields);
                    for (vector<BSONObj>::iterator i = v.begin(); i != v.end(); i++) {
                        BSONObj o = *i;
                        double lastrps = o["rps"].Number();
                        if (0 && lastrps) {
                            cout << "stats " << setw(42) << right << "new/old:" << ' ' << setw(9);
                            cout << fixed << setprecision(2) << rps / lastrps;
                            if (needver) {
                                cout << "         "
                                     << dps::extractElementAtPath(o, "info.git").toString();
                            }
                            cout << '\n';
                        }
                    }
                } catch (...) {
                }
                cout.flush();
                needver = false;
            }
            {
                bob b;
                b.append("host", _perfhostname);
                b.appendTimeT("when", time(0));
                b.append("test", s);
                b.append("rps", (int)rps);
                b.append("millis", us / 1000);
                b.appendBool("dur", storageGlobalParams.dur);
                if (showDurStats() && storageGlobalParams.dur) {
                    b.append("durStats", dur::stats.asObj());
                }

                {
                    auto&& vii = VersionInfoInterface::instance();
                    bob inf;
                    inf.append("version", vii.version());
                    if (sizeof(int*) == 4)
                        inf.append("bits", 32);
                    DEV inf.append("DEBUG", true);
#if defined(_WIN32)
                    inf.append("os", "win");
#endif
                    inf.append("git", vii.gitVersion());
#ifdef MONGO_CONFIG_SSL
                    inf.append("OpenSSL", vii.openSSLVersion());
#endif
                    inf.append("boost", BOOST_VERSION);
                    b.append("info", inf.obj());
                }
                BSONObj o = b.obj();
                // cout << "inserting " << o.toString() << endl;
                try {
                    conn->insert(ns, o);
                } catch (std::exception& e) {
                    warning() << "couldn't save perf results: " << e.what() << endl;
                }
            }
        }
    }

    /** if true runs timed2() again with several threads (8 at time of this writing).
    */
    virtual bool testThreaded() {
        return false;
    }

    int howLong() {
        int hlm = howLongMillis();
        DEV {
            // don't run very long with in debug mode - not very meaningful anyway on that build
            hlm = min(hlm, 500);
        }
        return hlm;
    }

    void run() {
        unsigned long long n = 0;

        _ns = string("perftest.") + name();
        client()->dropCollection(ns());
        prep();
        int hlm = howLong();
        mongo::Timer t;
        n = 0;
        const unsigned int Batch = batchSize();

        if (hlm == 0) {
            // means just do once
            timed();
        } else {
            do {
                unsigned int i;
                for (i = 0; i < Batch; i++)
                    timed();
                n += i;
            } while (t.micros() < (hlm * 1000));
        }

        client()->getLastError();  // block until all ops are finished

        say(n, t.micros(), name());

        post();

        string test2name = name2();
        {
            if (test2name != name()) {
                dur::stats.curr()->reset();
                mongo::Timer t;
                unsigned long long n = 0;
                while (1) {
                    unsigned int i;
                    for (i = 0; i < Batch; i++)
                        timed2(client());
                    n += i;
                    if (t.millis() > hlm)
                        break;
                }
                say(n, t.micros(), test2name);
            }
        }

        if (testThreaded()) {
            const int nThreads = 8;
            // cout << "testThreaded nThreads:" << nThreads << endl;
            mongo::Timer t;
            const unsigned long long result = launchThreads(nThreads);
            say(result / nThreads, t.micros(), test2name + "-threaded");
        }
    }

    bool stop;

    void thread(unsigned long long* counter) {
#if defined(_WIN32)
        static int z;
        srand(++z ^ (unsigned)time(0));
#endif
        Client::initThreadIfNotAlready("perftestthr");
        const ServiceContext::UniqueOperationContext txnPtr = cc().makeOperationContext();
        OperationContext& txn = *txnPtr;
        DBDirectClient c(&txn);

        const unsigned int Batch = batchSize();
        prepThreaded();
        while (1) {
            unsigned int i = 0;
            for (i = 0; i < Batch; i++)
                timed2(&c);
            *counter += i;
            if (stop)
                break;
        }
    }

    unsigned long long launchThreads(int remaining) {
        stop = false;
        if (!remaining) {
            int hlm = howLong();
            sleepmillis(hlm);
            stop = true;
            return 0;
        }
        unsigned long long counter = 0;
        stdx::thread athread(stdx::bind(&B::thread, this, &counter));
        unsigned long long child = launchThreads(remaining - 1);
        athread.join();
        unsigned long long accum = child + counter;
        return accum;
    }
};

SimpleMutex m;
boost::mutex mboost;              // NOLINT
boost::timed_mutex mboost_timed;  // NOLINT
std::mutex mstd;                  // NOLINT
std::timed_mutex mstd_timed;      // NOLINT
SpinLock s;
stdx::condition_variable c;

class boostmutexspeed : public B {
public:
    string name() {
        return "boost::mutex";
    }
    virtual int howLongMillis() {
        return 500;
    }
    virtual bool showDurStats() {
        return false;
    }
    void timed() {
        boost::lock_guard<boost::mutex> lk(mboost);  // NOLINT
    }
};
class boosttimed_mutexspeed : public B {
public:
    string name() {
        return "boost::timed_mutex";
    }
    virtual int howLongMillis() {
        return 500;
    }
    virtual bool showDurStats() {
        return false;
    }
    void timed() {
        boost::lock_guard<boost::timed_mutex> lk(mboost_timed);  // NOLINT
    }
};
class simplemutexspeed : public B {
public:
    string name() {
        return "simplemutex";
    }
    virtual int howLongMillis() {
        return 500;
    }
    virtual bool showDurStats() {
        return false;
    }
    void timed() {
        stdx::lock_guard<SimpleMutex> lk(m);
    }
};

class stdmutexspeed : public B {
public:
    string name() {
        return "std::mutex";
    }
    virtual int howLongMillis() {
        return 500;
    }
    virtual bool showDurStats() {
        return false;
    }
    void timed() {
        std::lock_guard<std::mutex> lk(mstd);  // NOLINT
    }
};
class stdtimed_mutexspeed : public B {
public:
    string name() {
        return "std::timed_mutex";
    }
    virtual int howLongMillis() {
        return 500;
    }
    virtual bool showDurStats() {
        return false;
    }
    void timed() {
        std::lock_guard<std::timed_mutex> lk(mstd_timed);  // NOLINT
    }
};


class All : public Suite {
public:
    All() : Suite("perf") {}

    void setupTests() {
        cout << "stats test                                       rps------  time-- "
             << dur::stats.curr()->_CSVHeader() << endl;
        add<simplemutexspeed>();
        add<boostmutexspeed>();
        add<boosttimed_mutexspeed>();
        add<stdmutexspeed>();
        add<stdtimed_mutexspeed>();
    }
} myall;
}