~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to db/flushtest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "stdafx.h"
 
2
#include <stdio.h>
 
3
#include "../util/goodies.h"
 
4
#include <fcntl.h>
 
5
 
 
6
namespace mongo {
 
7
 
 
8
#if defined(F_FULLFSYNC)
 
9
    void fullsync(int f) {
 
10
        fcntl( f, F_FULLFSYNC );
 
11
    }
 
12
#else
 
13
    void fullsync(int f) {
 
14
        fdatasync(f);
 
15
    }
 
16
#endif
 
17
 
 
18
    int main(int argc, char* argv[], char *envp[] ) {
 
19
        cout << "hello" << endl;
 
20
 
 
21
        FILE *f = fopen("/data/db/temptest", "a");
 
22
 
 
23
        if ( f == 0 ) {
 
24
            cout << "can't open file\n";
 
25
            return 1;
 
26
        }
 
27
 
 
28
        {
 
29
            Timer t;
 
30
            for ( int i = 0; i < 50000; i++ )
 
31
                fwrite("abc", 3, 1, f);
 
32
            cout << "small writes: " << t.millis() << "ms" << endl;
 
33
        }
 
34
 
 
35
        {
 
36
            Timer t;
 
37
            for ( int i = 0; i < 10000; i++ ) {
 
38
                fwrite("abc", 3, 1, f);
 
39
                fflush(f);
 
40
                fsync( fileno( f ) );
 
41
            }
 
42
            int ms = t.millis();
 
43
            cout << "flush: " << ms << "ms, " << ms / 10000.0 << "ms/request" << endl;
 
44
        }
 
45
 
 
46
        {
 
47
            Timer t;
 
48
            for ( int i = 0; i < 500; i++ ) {
 
49
                fwrite("abc", 3, 1, f);
 
50
                fflush(f);
 
51
                fsync( fileno( f ) );
 
52
                sleepmillis(2);
 
53
            }
 
54
            int ms = t.millis() - 500 * 2;
 
55
            cout << "flush with sleeps: " << ms << "ms, " << ms / 500.0 << "ms/request" << endl;
 
56
        }
 
57
 
 
58
        char buf[8192];
 
59
        for ( int pass = 0; pass < 2; pass++ ) {
 
60
            cout << "pass " << pass << endl;
 
61
            {
 
62
                Timer t;
 
63
                int n = 500;
 
64
                for ( int i = 0; i < n; i++ ) {
 
65
                    if ( pass == 0 )
 
66
                        fwrite("abc", 3, 1, f);
 
67
                    else
 
68
                        fwrite(buf, 8192, 1, f);
 
69
                    buf[0]++;
 
70
                    fflush(f);
 
71
                    fullsync(fileno(f));
 
72
                }
 
73
                int ms = t.millis();
 
74
                cout << "fullsync: " << ms << "ms, " << ms / ((double) n) << "ms/request" << endl;
 
75
            }
 
76
 
 
77
            {
 
78
                Timer t;
 
79
                for ( int i = 0; i < 500; i++ ) {
 
80
                    if ( pass == 0 )
 
81
                        fwrite("abc", 3, 1, f);
 
82
                    else
 
83
                        fwrite(buf, 8192, 1, f);
 
84
                    buf[0]++;
 
85
                    fflush(f);
 
86
                    fullsync(fileno(f));
 
87
                    sleepmillis(2);
 
88
                }
 
89
                int ms = t.millis() - 2 * 500;
 
90
                cout << "fullsync with sleeps: " << ms << "ms, " << ms / 500.0 << "ms/request" << endl;
 
91
            }
 
92
        }
 
93
 
 
94
        // without growing
 
95
        {
 
96
            fclose(f);
 
97
            /* try from beginning of the file, where we aren't appending and changing the file length,
 
98
               to see if this is faster as the directory entry then doesn't have to be flushed (if noatime in effect).
 
99
            */
 
100
            f = fopen("/data/db/temptest", "r+");
 
101
            Timer t;
 
102
            int n = 500;
 
103
            for ( int i = 0; i < n; i++ ) {
 
104
                fwrite("xyz", 3, 1, f);
 
105
                fflush(f);
 
106
                fullsync(fileno(f));
 
107
            }
 
108
            int ms = t.millis();
 
109
            cout << "fullsync without growing: " << ms << "ms, " << ms / ((double) n) << "ms/request" << endl;
 
110
        }
 
111
 
 
112
        // without growing, with delay
 
113
        {
 
114
            fclose(f);
 
115
            /* try from beginning of the file, where we aren't appending and changing the file length,
 
116
               to see if this is faster as the directory entry then doesn't have to be flushed (if noatime in effect).
 
117
            */
 
118
            f = fopen("/data/db/temptest", "r+");
 
119
            Timer t;
 
120
            int n = 500;
 
121
            for ( int i = 0; i < n; i++ ) {
 
122
                fwrite("xyz", 3, 1, f);
 
123
                fflush(f);
 
124
                fullsync(fileno(f));
 
125
                sleepmillis(2);
 
126
            }
 
127
            int ms = t.millis() - 2 * 500;
 
128
            cout << "fullsync without growing with sleeps: " << ms << "ms, " << ms / ((double) n) << "ms/request" << endl;
 
129
        }
 
130
 
 
131
        return 0;
 
132
    }
 
133
 
 
134
} // namespace mongo