~akiban-technologies/akiban-persistit/3.1.7-update

« back to all changes in this revision

Viewing changes to examples/SimpleBench/SimpleBench.java

  • Committer: Padraig O'Sullivan
  • Date: 2011-02-01 16:48:36 UTC
  • mfrom: (125.1.12 learning)
  • Revision ID: osullivan.padraig@gmail.com-20110201164836-mtdbk9d0rgcxpwye
Merge Padraig.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Akiban Technologies Inc.
 
3
 * This program is free software: you can redistribute it and/or modify
 
4
 * it under the terms of the GNU Affero General Public License, version 3,
 
5
 * as published by the Free Software Foundation.
 
6
 *
 
7
 * This program is distributed in the hope that it will be useful,
 
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
10
 * GNU Affero General Public License for more details.
 
11
 *
 
12
 * You should have received a copy of the GNU Affero General Public License
 
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 
14
 */
 
15
 
 
16
import java.util.Properties;
 
17
import java.util.Random;
 
18
 
 
19
import com.persistit.Exchange;
 
20
import com.persistit.Key;
 
21
import com.persistit.Value;
 
22
import com.persistit.Persistit;
 
23
 
 
24
/**
 
25
 * <p>
 
26
 * A simple demonstration of Persistit&trade;'s insert and retrieval
 
27
 * and performance.  This class performs the following operations:
 
28
 * <ol>
 
29
 * <li> Initializes {@link com.persistit.Persistit} using stock properties file.
 
30
 * </li>
 
31
 * <li> Creates an {@link com.persistit.Exchange} with which to store and fetch
 
32
 *      data to and from a tree.
 
33
 * </li>
 
34
 * <li> Removes any data formerly stored in the tree.
 
35
 * </li>
 
36
 * <li> Inserts numerous String values, indexed by
 
37
 *      small integers.
 
38
 * </li>
 
39
 * <li> Traverses all keys in forward sequential order.
 
40
 * </li>
 
41
 * <li> Traverses all keys in reverse sequential order.
 
42
 * </li>
 
43
 * <li> Modifies records by random key value
 
44
 * </li>
 
45
 * <li> Lengthens all records
 
46
 * </liL
 
47
 * <li> Fetches records by random key value.
 
48
 * </li>
 
49
 * </ol>
 
50
 * By default, <tt>SimpleBench</tt> performs this cycle ten times, inserting 
 
51
 * 500,000 records.  You can specify different iteration and cycle counts on the
 
52
 * command line.
 
53
 * </p>
 
54
 * <p>
 
55
 * Note that if you run this program using a Sun HotSpot&trade; 
 
56
 * Server JVM, you will observe a substantial performance improvement over
 
57
 * multiple cycles as the optimizing compiler fully analyzes and optimizes
 
58
 * heavily-used code paths.
 
59
 * </p> 
 
60
 *
 
61
 * @version 1.0
 
62
 */
 
63
public class SimpleBench
 
64
{
 
65
    public static void main(String[] args)
 
66
    throws Exception
 
67
    {
 
68
        w("");
 
69
        w("SimpleBench");
 
70
        w("");
 
71
        w("This simple benchmark demonstrates Persistit's ability to");
 
72
        w("modify its in-memory data structures fast. The buffer pool has");
 
73
        w("been carefully sized to ensure that once the buffers are");
 
74
        w("\"warmed up\", no disk I/O is needed to complete the store,");
 
75
        w("traverse, and fetch operations being performed.");
 
76
        w("");
 
77
        w("Although real applications do not perform entirely in this");
 
78
        w("manner, they typically do exhibit strong locality of reference");
 
79
        w("within the keyspace, which is why disk caching is effective,");
 
80
        w("and why it is important to examine in-memory performance.");
 
81
        w("");
 
82
        w("Truly meaningful performance comparisons can best be made by");
 
83
        w("measuring actual application performance.");
 
84
        w("");
 
85
        //
 
86
        // Initializes buffer pool, loads Volume(s)
 
87
        // and sets up PrewriteJournal.
 
88
        //
 
89
        System.out.println("Initializing Persistit");
 
90
        
 
91
        // Note: you can override datapath and logpath properties as follows:
 
92
        //
 
93
        // java -Dcom.persistit.datapath=... -Dcom.persistit.logpath=... SimpleBench
 
94
        //
 
95
        Properties props = new Properties();
 
96
        props.setProperty("datapath", ".");
 
97
        props.setProperty("logpath", ".");
 
98
        props.setProperty("logfile", "${logpath}/persistit_${timestamp}.log");
 
99
        props.setProperty("buffer.count.8192", "4K");
 
100
        props.setProperty("volume.1",
 
101
            "${datapath}/SimpleBench.v01,create,pageSize:8K," +
 
102
            "initialSize:16M,extensionSize:1M,maximumSize:10G");
 
103
        props.setProperty("pwjpath", "${datapath}/SimpleBench.pwj");
 
104
        props.setProperty("pwjsize", "16M");
 
105
        props.setProperty("pwjdelete", "true");
 
106
        props.setProperty("pwjcount", "1");
 
107
        
 
108
        Persistit persistit = new Persistit();
 
109
        persistit.initialize(props);
 
110
        
 
111
        try
 
112
        {
 
113
            doBench(persistit, args);
 
114
        }
 
115
        finally
 
116
        {
 
117
            persistit.close();
 
118
        }
 
119
    }
 
120
    
 
121
    private static void w(String s)
 
122
    {
 
123
        System.out.println(s);
 
124
    }
 
125
    
 
126
    public static void doBench(Persistit persistit, String[] args)
 
127
    throws Exception
 
128
    {
 
129
        //
 
130
        // Source of random key values.  Initialized with a constant seed
 
131
        // value so that results are reproducible.
 
132
        //
 
133
        Random random = new Random(1);
 
134
        //
 
135
        // An Exchange provides all the methods you use to manipulate data.
 
136
        // This constructs a Exchange in volume "SimpleBench" and either opens
 
137
        // or creates within that volume a Tree called "BenchTree".
 
138
        //
 
139
        Exchange exchange = new Exchange(persistit, "SimpleBench", "BenchTree", true);
 
140
        long time;
 
141
        
 
142
        int iterations = 250000;
 
143
        int cycles = 10;
 
144
        
 
145
        if (args.length > 0) iterations = Integer.parseInt(args[0]);
 
146
        if (args.length > 1) cycles = Integer.parseInt(args[1]);
 
147
 
 
148
        long time0 = System.currentTimeMillis();
 
149
        for (int cycle = 1; cycle <= cycles; cycle++)
 
150
        {
 
151
            
 
152
            System.out.println();
 
153
            System.out.println();
 
154
            System.out.println("Starting cycle #" + cycle + " of " + cycles);
 
155
            System.out.println();
 
156
            //
 
157
            // Remove all records everything.
 
158
            //
 
159
            exchange.removeAll();
 
160
            //
 
161
            // Set up a stock 30-character value to store.
 
162
            //
 
163
            exchange.getValue().putString("ABCDEFGHIJABCDEFGHIJABCDEFGHIJ");
 
164
            //
 
165
            // INSERTION
 
166
            // Inserts numerous String values, indexed by  small integers.
 
167
            //
 
168
            System.out.print(
 
169
                "Inserting " + iterations + 
 
170
                " records in forward sequential key order ");
 
171
            time = System.currentTimeMillis();
 
172
            for (int index = 0; index < iterations; index++)
 
173
            {
 
174
                exchange.to(index).store();
 
175
                if ((index % 50000) == 0) System.out.print(".");
 
176
            }
 
177
            time = System.currentTimeMillis() - time;
 
178
            System.out.println(" -- took " + time + "ms");
 
179
            
 
180
            //
 
181
            // TRAVERSAL
 
182
            // Traverses all keys in forward-sequential order.
 
183
            //
 
184
            System.out.print(
 
185
                "Traversing " + iterations + 
 
186
                " keys in forward order ");
 
187
            exchange.to(Key.BEFORE);
 
188
            time = System.currentTimeMillis();
 
189
            for (int index = 0; exchange.next(); index++)
 
190
            {
 
191
                if ((index % 50000) == 0) System.out.print(".");
 
192
            }
 
193
            time = System.currentTimeMillis() - time;
 
194
            System.out.println(" -- took " + time + "ms");
 
195
            
 
196
            //
 
197
            // TRAVERSAL
 
198
            // Traverses all keys in reverse-sequential order.
 
199
            //
 
200
            System.out.print(
 
201
                "Traversing " + iterations + 
 
202
                " keys in reverse order ");
 
203
            exchange.to(Key.AFTER);
 
204
            time = System.currentTimeMillis();
 
205
            for (int index = 0; exchange.previous(); index++)
 
206
            {
 
207
                if ((index % 50000) == 0) System.out.print(".");
 
208
            }
 
209
            time = System.currentTimeMillis() - time;
 
210
            System.out.println(" -- took " + time + "ms");
 
211
            
 
212
            //
 
213
            // UPDATE
 
214
            // Updates numerous String values with equal length using random keys.
 
215
            //
 
216
            exchange.getValue().putString("abcdefghijabcdefghijabcdefghij");
 
217
            
 
218
            System.out.print(
 
219
                "Modifying " + iterations + 
 
220
                " values by random key ");
 
221
            time = System.currentTimeMillis();
 
222
            for (int index = 0; index < iterations; index++)
 
223
            {
 
224
                if ((index % 50000) == 0) System.out.print(".");
 
225
                int randomKeyInt = random.nextInt(iterations);
 
226
                exchange.to(randomKeyInt).store();
 
227
            } 
 
228
            time = System.currentTimeMillis() - time;
 
229
            System.out.println(" -- took " + time + "ms");
 
230
            
 
231
            //
 
232
            // LENGTHEN
 
233
            // Lengthens all String values in forward-seqential key order.
 
234
            //
 
235
            exchange.getValue().putString("ABCDEFGHIJABCDEFGHIJABCDEFGHIJ0123456789");
 
236
            
 
237
            System.out.print(
 
238
                "Lengthening " + iterations + 
 
239
                " values by 10 chars in forward-sequential order");
 
240
            time = System.currentTimeMillis();
 
241
            for (int index = 0; index < iterations; index++)
 
242
            {
 
243
                if ((index % 50000) == 0) System.out.print(".");
 
244
                exchange.to(index).store();
 
245
            } 
 
246
            time = System.currentTimeMillis() - time;
 
247
            System.out.println(" -- took " + time + "ms");
 
248
    
 
249
            //
 
250
            // RETRIEVE ALL VALUES IN PSEUDO-RANDOM ORDER
 
251
            //
 
252
            StringBuilder sb = new StringBuilder();
 
253
            System.out.print(
 
254
                "Retrieving " + iterations + 
 
255
                " values in random order");
 
256
            time = System.currentTimeMillis();
 
257
            for (int index = 0; index < iterations; index++)
 
258
            {
 
259
                if ((index % 50000) == 0) System.out.print(".");
 
260
                
 
261
                int randomKeyInt = random.nextInt(iterations);
 
262
                exchange.to(randomKeyInt).fetch();
 
263
                
 
264
                exchange.getValue().getString(sb).length();
 
265
                if (sb.length() != 40)
 
266
                {
 
267
                    throw new RuntimeException(
 
268
                        "Wrong value " + exchange.getValue() + 
 
269
                        " at " + randomKeyInt);
 
270
                }
 
271
            } 
 
272
            time = System.currentTimeMillis() - time;
 
273
            System.out.println(" -- took " + time + "ms");
 
274
            
 
275
            System.out.print("Flushing all pending updates to the OS");
 
276
            time = System.currentTimeMillis();
 
277
            persistit.flush();
 
278
            time = System.currentTimeMillis() - time;
 
279
            System.out.println(" -- took " + time + "ms");
 
280
 
 
281
            System.out.print("Synching all pending I/O to disk");
 
282
            time = System.currentTimeMillis();
 
283
            persistit.sync();
 
284
            time = System.currentTimeMillis() - time;
 
285
            System.out.println(" -- took " + time + "ms");
 
286
 
 
287
        }
 
288
        time0 = System.currentTimeMillis() - time0;
 
289
        System.out.println("Total time: " + time0);
 
290
        
 
291
        persistit.close();
 
292
    }
 
293
}