~pbeaman/akiban-persistit/accelerate-pruning

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
/**
 * Copyright © 2005-2012 Akiban Technologies, Inc.  All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3 (only) of the
 * License.
 *
 * 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/>.
 * 
 * This program may also be available under different license terms. For more
 * information, see www.akiban.com or contact licensing@akiban.com.
 */

package com.persistit.stress;

import com.persistit.Key;
import com.persistit.Transaction;
import com.persistit.TransactionRunnable;
import com.persistit.Value;
import com.persistit.exception.PersistitException;
import com.persistit.test.TestResult;
import com.persistit.util.ArgParser;

public class Stress1txn extends StressBase {

    private final static String SHORT_DESCRIPTION = "Simple transactional write/read/delete/traverse loops";

    private final static String LONG_DESCRIPTION = "   Simple stress test that perform <repeat> iterations of the following: \r\n"
            + "    - insert <count> sequentially ascending keys \r\n"
            + "    - read and verify <count> sequentially ascending key/value pairs\r\n"
            + "    - traverse and count all keys using next() \r\n"
            + "    - delete <count> sequentially ascending keys \r\n"
            + "   Optional <splay> value allows variations in key sequence: \r\n"
            + "   Same as Stress1 except uses Transactions\r\n";

    private final static String[] ARGS_TEMPLATE = { "op|String:wrtd|Operations to perform",
            "repeat|int:1:0:1000000000|Repetitions", "count|int:10000:0:1000000000|Number of nodes to populate",
            "size|int:30:1:5000000|Size of each data value", "splay|int:1:1:1000|Splay", };

    int _size;
    int _splay;
    String _opflags;

    @Override
    public String shortDescription() {
        return SHORT_DESCRIPTION;
    }

    @Override
    public String longDescription() {
        return LONG_DESCRIPTION;
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        _ap = new ArgParser("com.persistit.Stress1", _args, ARGS_TEMPLATE);
        _splay = _ap.getIntValue("splay");
        _opflags = _ap.getStringValue("op");
        _size = _ap.getIntValue("size");
        _repeatTotal = _ap.getIntValue("repeat");
        _total = _ap.getIntValue("count");
        _dotGranularity = 10000;

        try {
            // Exchange with Thread-private Tree
            _ex = getPersistit().getExchange("persistit", _rootName + _threadIndex, true);
        } catch (final Exception ex) {
            handleThrowable(ex);
        }
    }

    @Override
    public void executeTest() throws Exception {
        final Value value1 = _ex.getValue();
        final Value value2 = new Value(getPersistit());
        final Transaction txn = getPersistit().getTransaction();

        setPhase("@");
        try {
            txn.begin();
            _ex.clear().remove(Key.GTEQ);
            txn.commit();
        } catch (final Exception e) {
            handleThrowable(e);
        } finally {
            txn.end();
        }
        verboseln();
        for (_repeat = 0; (_repeat < _repeatTotal) && !isStopped(); _repeat++) {
            verboseln();
            verboseln("Starting cycle " + (_repeat + 1) + " of " + _repeatTotal);

            if (_opflags.indexOf('w') >= 0) {
                setPhase("w");
                try {
                    txn.begin();
                    for (_count = 0; (_count < _total) && !isStopped(); _count++) {
                        dot();
                        final int keyInteger = keyInteger(_count);
                        _ex.clear().append(keyInteger);
                        setupTestValue(_ex, _count, _size);
                        _ex.store();
                    }
                    txn.commit();
                } catch (final Exception e) {
                    handleThrowable(e);
                    break;
                } finally {
                    txn.end();
                }
            }

            if (_opflags.indexOf('r') >= 0) {
                setPhase("r");
                for (_count = 0; (_count < _total) && !isStopped(); _count++) {
                    dot();
                    final int keyInteger = keyInteger(_count);
                    _ex.clear().append(keyInteger);
                    setupTestValue(_ex, _count, _size);
                    try {
                        txn.run(new TransactionRunnable() {
                            @Override
                            public void runTransaction() throws PersistitException {

                                // fetch to a different Value object so we can
                                // compare
                                // with the original.
                                _ex.fetch(value2);
                                compareValues(value1, value2);
                            }
                        });
                    } catch (final Exception e) {
                        handleThrowable(e);
                    }
                }
            }

            if (_opflags.indexOf('t') >= 0) {
                setPhase("t");
                try {
                    txn.begin();
                    _ex.clear().append(Integer.MIN_VALUE);
                    for (_count = 0; (_count < (_total * 10)) && !isStopped(); _count++) {
                        dot();
                        if (!_ex.next()) {
                            break;
                        }
                    }
                    if (_count != _total) {
                        _result = new TestResult(false, "Traverse count=" + _count + " out of " + _total
                                + " repetition=" + _repeat + " in thread=" + _threadIndex);
                        println(_result);
                        forceStop();
                        break;
                    }
                    txn.commit();
                } catch (final Exception e) {
                    handleThrowable(e);
                } finally {
                    txn.end();
                }
            }

            if (_opflags.indexOf('d') >= 0) {
                setPhase("d");
                try {
                    txn.begin();
                    for (_count = 0; (_count < _total) && !isStopped(); _count++) {
                        dot();
                        final int keyInteger = keyInteger(_count);
                        _ex.clear().append(keyInteger);
                        _ex.remove();
                    }
                    txn.commit();
                } catch (final Exception e) {
                    handleThrowable(e);
                } finally {
                    txn.end();
                }
            }

            if ((_opflags.indexOf('h') > 0) && !isStopped()) {
                setPhase("h");
                try {
                    Thread.sleep(1000);
                } catch (final Exception e) {
                }
            }

        }
        verboseln();
        verbose("done");
    }

    int keyInteger(final int counter) {
        int keyInteger = (counter * _splay) % _total;
        if (keyInteger < 0) {
            keyInteger += _total;
        }
        return keyInteger;
    }

    public static void main(final String[] args) {
        final Stress1txn test = new Stress1txn();
        test.runStandalone(args);
    }
}