0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/dbtests/mmaptests.cpp

216 lines
6.3 KiB
C++
Raw Normal View History

2010-09-27 18:35:22 +02:00
// @file mmaptests.cpp
2010-09-14 21:38:46 +02:00
/**
* 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/>.
*/
#include "pch.h"
2010-12-16 21:11:30 +01:00
#include "../db/mongommf.h"
2010-12-22 21:12:26 +01:00
#include "../util/timer.h"
2010-09-14 21:38:46 +02:00
#include "dbtests.h"
namespace MMapTests {
2010-12-16 21:11:30 +01:00
class LeakTest {
const string fn;
const int optOld;
2010-12-16 21:11:30 +01:00
public:
2011-01-04 06:40:41 +01:00
LeakTest() :
fn( (path(dbpath) / "testfile.map").string() ), optOld(cmdLine.durOptions)
{
cmdLine.durOptions = 0; // DurParanoid doesn't make sense with this test
}
2011-01-04 06:40:41 +01:00
~LeakTest() {
cmdLine.durOptions = optOld;
2011-01-04 06:40:41 +01:00
try { boost::filesystem::remove(fn); }
catch(...) { }
2010-12-16 21:11:30 +01:00
}
void run() {
2011-01-04 06:40:41 +01:00
try { boost::filesystem::remove(fn); }
catch(...) { }
2010-12-16 21:11:30 +01:00
writelock lk;
{
MongoMMF f;
unsigned long long len = 256 * 1024 * 1024;
assert( f.create(fn, len, /*sequential*/false) );
2011-01-08 19:22:45 +01:00
{
2010-12-22 21:12:26 +01:00
char *p = (char *) f.getView();
assert(p);
// write something to the private view as a test
strcpy(p, "hello");
}
2011-01-04 06:40:41 +01:00
if( cmdLine.dur ) {
2010-12-16 21:11:30 +01:00
char *w = (char *) f.view_write();
strcpy(w + 6, "world");
}
2010-12-30 21:34:30 +01:00
MongoFileFinder ff;
ASSERT( ff.findByPath(fn) );
ASSERT( ff.findByPath("asdf") == 0 );
}
{
MongoFileFinder ff;
ASSERT( ff.findByPath(fn) == 0 );
2010-12-16 21:11:30 +01:00
}
2010-12-22 21:12:26 +01:00
int N = 10000;
#if !defined(_WIN32) && !defined(__linux__)
// seems this test is slow on OS X.
N = 100;
#endif
2010-12-16 21:11:30 +01:00
// we make a lot here -- if we were leaking, presumably it would fail doing this many.
2010-12-22 21:12:26 +01:00
Timer t;
for( int i = 0; i < N; i++ ) {
2010-12-16 21:11:30 +01:00
MongoMMF f;
assert( f.open(fn, i%4==1) );
2011-01-08 19:22:45 +01:00
{
2010-12-22 21:12:26 +01:00
char *p = (char *) f.getView();
assert(p);
strcpy(p, "zzz");
}
2011-01-04 06:40:41 +01:00
if( cmdLine.dur ) {
2010-12-16 21:11:30 +01:00
char *w = (char *) f.view_write();
if( i % 2 == 0 )
++(*w);
assert( w[6] == 'w' );
}
}
2011-01-04 06:40:41 +01:00
if( t.millis() > 10000 ) {
2010-12-22 21:12:26 +01:00
log() << "warning: MMap LeakTest is unusually slow N:" << N << ' ' << t.millis() << "ms" << endl;
}
2010-12-16 21:11:30 +01:00
}
};
2011-01-04 06:40:41 +01:00
2010-12-16 21:11:30 +01:00
class All : public Suite {
public:
2011-01-04 06:40:41 +01:00
All() : Suite( "mmap" ) {}
void setupTests() {
2010-12-16 21:11:30 +01:00
add< LeakTest >();
}
} myall;
2010-09-14 21:38:46 +02:00
#if 0
class CopyOnWriteSpeedTest {
public:
void run() {
string fn = "/tmp/testfile.map";
boost::filesystem::remove(fn);
MemoryMappedFile f;
char *p = (char *) f.create(fn, 1024 * 1024 * 1024, true);
assert(p);
strcpy(p, "hello");
{
void *x = f.testGetCopyOnWriteView();
Timer tt;
for( int i = 11; i < 1000000000; i++ )
p[i] = 'z';
cout << "fill 1GB time: " << tt.millis() << "ms" << endl;
f.testCloseCopyOnWriteView(x);
}
/* test a lot of view/unviews */
{
Timer t;
char *q;
for( int i = 0; i < 1000; i++ ) {
q = (char *) f.testGetCopyOnWriteView();
assert( q );
if( i == 999 ) {
strcpy(q+2, "there");
}
f.testCloseCopyOnWriteView(q);
}
cout << "view unview: " << t.millis() << "ms" << endl;
}
2011-01-04 06:40:41 +01:00
f.flush(true);
2010-09-14 21:38:46 +02:00
/* plain old mmaped writes */
2011-01-04 06:40:41 +01:00
{
2010-09-14 21:38:46 +02:00
Timer t;
for( int i = 0; i < 10; i++ ) {
memset(p+100, 'c', 200 * 1024 * 1024);
}
cout << "traditional writes: " << t.millis() << "ms" << endl;
}
2011-01-04 06:40:41 +01:00
f.flush(true);
2010-09-14 21:38:46 +02:00
/* test doing some writes */
2011-01-04 06:40:41 +01:00
{
2010-09-14 21:38:46 +02:00
Timer t;
char *q = (char *) f.testGetCopyOnWriteView();
for( int i = 0; i < 10; i++ ) {
assert( q );
memset(q+100, 'c', 200 * 1024 * 1024);
}
f.testCloseCopyOnWriteView(q);
cout << "inc style some writes: " << t.millis() << "ms" << endl;
}
/* test doing some writes */
2011-01-04 06:40:41 +01:00
{
2010-09-14 21:38:46 +02:00
Timer t;
for( int i = 0; i < 10; i++ ) {
char *q = (char *) f.testGetCopyOnWriteView();
assert( q );
memset(q+100, 'c', 200 * 1024 * 1024);
f.testCloseCopyOnWriteView(q);
}
cout << "some writes: " << t.millis() << "ms" << endl;
}
/* more granular */
2011-01-04 06:40:41 +01:00
{
2010-09-14 21:38:46 +02:00
Timer t;
for( int i = 0; i < 100; i++ ) {
char *q = (char *) f.testGetCopyOnWriteView();
assert( q );
memset(q+100, 'c', 20 * 1024 * 1024);
f.testCloseCopyOnWriteView(q);
}
cout << "more granular some writes: " << t.millis() << "ms" << endl;
2011-01-04 06:40:41 +01:00
}
2010-09-14 21:38:46 +02:00
2011-01-04 06:40:41 +01:00
p[10] = 0;
cout << p << endl;
2010-09-14 21:38:46 +02:00
}
};
2011-01-04 06:40:41 +01:00
2010-09-14 21:38:46 +02:00
class All : public Suite {
public:
2011-01-04 06:40:41 +01:00
All() : Suite( "mmap" ) {}
void setupTests() {
2010-09-14 21:38:46 +02:00
add< CopyOnWriteSpeedTest >();
}
} myall;
#endif
}