summaryrefslogtreecommitdiff
path: root/src/mongo/client/examples/mongoperf.cpp
blob: 1681e8a911333a82076833b4ab3ea87dcb0fef6a (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
/*    Copyright 2009 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.
 */

/*
   How to build and run:

   scons mongoperf
   ./mongoperf -h
*/


// note: mongoperf is an internal mongodb utility
// so we define the following macro
#define MONGO_EXPOSE_MACROS 1

#include "mongo/platform/basic.h"

#include <iostream>

#include <boost/filesystem/operations.hpp>

#include "mongo/db/jsobj.h"
#include "mongo/db/json.h"
#include "mongo/db/storage/mmap_v1/logfile.h"
#include "mongo/db/storage/mmap_v1/mmap.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/stdx/thread.h"
#include "mongo/util/allocator.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/processinfo.h"
#include "mongo/util/time_support.h"
#include "mongo/util/timer.h"

using namespace std;
using namespace mongo;
using namespace mongoutils::str;

int dummy;
unsigned recSizeKB;
LogFile* lf = 0;
MemoryMappedFile* mmfFile;
char* mmf = 0;
bo options;
unsigned long long len;  // file len
const unsigned PG = 4096;
unsigned nThreadsRunning = 0;

// as this is incremented A LOT, at some point this becomes a bottleneck if very high ops/second (in cache) things are happening.
AtomicUInt32 iops;

SimpleMutex m;

int syncDelaySecs = 0;

void syncThread() {
    while (1) {
        mongo::Timer t;
        mmfFile->flush(true);
        cout << "                                                     mmf sync took " << t.millis()
             << "ms" << endl;
        sleepsecs(syncDelaySecs);
    }
}

void stripTrailing(std::string& s, const char* chars) {
    std::string::iterator to = s.begin();
    for (std::string::iterator i = s.begin(); i != s.end(); i++) {
        // During each iteration if i finds a non-"chars" character it writes it to the
        // position of t. So the part of the string left from the "to" iterator is already
        // "cleared" string.
        if (!contains(chars, *i)) {
            if (i != to)
                s.replace(to, to + 1, 1, *i);
            to++;
        }
    }
    s.erase(to, s.end());
}

char* round(char* x) {
    size_t f = (size_t)x;
    char* p = (char*)((f + PG - 1) / PG * PG);
    return p;
}

struct Aligned {
    char x[8192];
    char* addr() {
        return round(x);
    }
};

unsigned long long rrand() {
    // RAND_MAX is very small on windows
    return (static_cast<unsigned long long>(rand()) << 15) ^ rand();
}

void workerThread() {
    bool r = options["r"].trueValue();
    bool w = options["w"].trueValue();
    cout << "read:" << r << " write:" << w << endl;
    long long su = options["sleepMicros"].numberLong();
    Aligned a;
    while (1) {
        unsigned long long rofs = (rrand() * PG) % len;
        unsigned long long wofs = (rrand() * PG) % len;
        const unsigned P = PG / 1024;
        if (mmf) {
            if (r) {
                for (unsigned p = P; p <= recSizeKB; p += P) {
                    if (rofs < len)
                        dummy += mmf[rofs];
                    rofs += PG;
                }
                iops.fetchAndAdd(1);
            }
            if (w) {
                for (unsigned p = P; p <= recSizeKB; p += P) {
                    if (wofs < len)
                        mmf[wofs] = 3;
                    wofs += PG;
                }
                iops.fetchAndAdd(1);
            }
        } else {
            if (r) {
                lf->readAt(rofs, a.addr(), recSizeKB * 1024);
                iops.fetchAndAdd(1);
            }
            if (w) {
                lf->writeAt(wofs, a.addr(), recSizeKB * 1024);
                iops.fetchAndAdd(1);
            }
        }
        long long micros = su / nThreadsRunning;
        if (micros) {
            sleepmicros(micros);
        }
    }
}

void go() {
    verify(options["r"].trueValue() || options["w"].trueValue());

    recSizeKB = options["recSizeKB"].numberInt();
    if (recSizeKB == 0)
        recSizeKB = 4;
    verify(recSizeKB <= 64000 && recSizeKB > 0);

    MemoryMappedFile f;
    cout << "creating test file size:";
    len = options["fileSizeMB"].numberLong();
    if (len == 0)
        len = 1;
    cout << len << "MB ..." << endl;

    if (0 && len > 2000 && !options["mmf"].trueValue()) {
        // todo make tests use 64 bit offsets in their i/o -- i.e. adjust LogFile::writeAt and such
        cout << "\nsizes > 2GB not yet supported with mmf:false" << endl;
        return;
    }
    len *= 1024 * 1024;
    const char* fname = "./mongoperf__testfile__tmp";
    try {
        boost::filesystem::remove(fname);
    } catch (...) {
        cout << "error deleting file " << fname << endl;
        return;
    }
    lf = new LogFile(fname, true);
    const unsigned sz = 1024 * 1024 *
        32;  // needs to be big as we are using synchronousAppend.  if we used a regular MongoFile it wouldn't have to be
    char* buf = (char*)mongoMalloc(sz + 4096);
    const char* p = round(buf);
    for (unsigned long long i = 0; i < len; i += sz) {
        lf->synchronousAppend(p, sz);
        if (i % (1024ULL * 1024 * 1024) == 0 && i) {
            cout << i / (1024ULL * 1024 * 1024) << "GB..." << endl;
        }
    }
    BSONObj& o = options;

    if (o["mmf"].trueValue()) {
        delete lf;
        lf = 0;
        mmfFile = new MemoryMappedFile();
        mmf = (char*)mmfFile->map(fname);
        verify(mmf);

        syncDelaySecs = options["syncDelay"].numberInt();
        if (syncDelaySecs) {
            stdx::thread t(syncThread);
        }
    }

    cout << "testing..." << endl;

    cout << "options:" << o.toString() << endl;
    unsigned wthr = 1;
    if (!o["nThreads"].eoo()) {
        wthr = (unsigned)o["nThreads"].Int();
    }
    cout << "wthr " << wthr << endl;

    if (wthr < 1) {
        cout << "bad threads field value" << endl;
        return;
    }
    unsigned i = 0;
    unsigned d = 1;
    unsigned& nthr = nThreadsRunning;
    while (1) {
        if (i++ % 8 == 0) {
            if (nthr < wthr) {
                while (nthr < wthr && nthr < d) {
                    nthr++;
                    stdx::thread w(workerThread);
                }
                cout << "new thread, total running : " << nthr << endl;
                d *= 2;
            }
        }
        sleepsecs(1);
        unsigned long long w = iops.loadRelaxed();
        iops.store(0);
        w /= 1;  // 1 secs
        cout << w << " ops/sec ";
        if (mmf == 0)
            // only writing 4 bytes with mmf so we don't say this
            cout << (w * PG / 1024 / 1024) << " MB/sec";
        cout << endl;
    }
}

int main(int argc, char* argv[]) {
    try {
        cout << "mongoperf" << endl;

        if (argc > 1) {
            cout <<

                "\n"
                "usage:\n"
                "\n"
                "  mongoperf < myjsonconfigfile\n"
                "\n"
                "  {\n"
                "    nThreads:<n>,     // number of threads (default 1)\n"
                "    fileSizeMB:<n>,   // test file size (default 1MB)\n"
                "    sleepMicros:<n>,  // pause for sleepMicros/nThreads between each operation "
                "(default 0)\n"
                "    mmf:<bool>,       // if true do i/o's via memory mapped files (default "
                "false)\n"
                "    r:<bool>,         // do reads (default false)\n"
                "    w:<bool>,         // do writes (default false)\n"
                "    recSizeKB:<n>,    // size of each write (default 4KB)\n"
                "    syncDelay:<n>     // secs between fsyncs, like --syncdelay in mongod. "
                "(default 0/never)\n"
                "  }\n"
                "\n"
                "mongoperf is a performance testing tool. the initial tests are of disk subsystem "
                "performance; \n"
                "  tests of mongos and mongod will be added later.\n"
                "most fields are optional.\n"
                "non-mmf io is direct io (no caching). use a large file size to test making the "
                "heads\n"
                "  move significantly and to avoid i/o coalescing\n"
                "mmf io uses caching (the file system cache).\n"
                "\n"

                 << endl;
            return EXIT_SUCCESS;
        }

        cout << "use -h for help" << endl;

        char input[1024];
        memset(input, 0, sizeof(input));
        cin.read(input, 1000);
        if (*input == 0) {
            cout << "error no options found on stdin for mongoperf" << endl;
            return EXIT_FAILURE;
        }

        string s = input;
        stripTrailing(s, " \n\r\0x1a");
        try {
            options = fromjson(s);
        } catch (...) {
            cout << "couldn't parse json options. input was:\n|" << s << "|" << endl;
            return EXIT_FAILURE;
        }
        cout << "parsed options:\n" << options.toString() << endl;
        ProcessInfo::initializeSystemInfo();

        go();
    } catch (DBException& e) {
        cout << "caught DBException " << e.toString() << endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}