summaryrefslogtreecommitdiff
path: root/java/demo/Freeze/bench/Client.java
blob: 7872ec17e256f38e5fa11b7cc8834b8110dc3d36 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

class TestApp extends Freeze.Application
{
    TestApp(String dbEnvName)
    {
	super(dbEnvName);
    }

    void
    IntIntMapTest(Freeze.DBEnvironment dbEnv, boolean fast)
    {
	Freeze.DB db;
	if (fast)
	{
	    db = dbEnv.openDB("IntIntMap.fast", true);
	}
	else
	{
	    db = dbEnv.openDB("IntIntMap", true);
	}

	IntIntMap m = new IntIntMap(db);

	//
	// Populate the database.
	//
	_watch.start();
	if (fast)
	{
	    for (int i = 0; i < _repetitions; ++i)
	    {
		m.fastPut(new Integer(i), new Integer(i));
	    }
	}
	else
	{
	    for (int i = 0; i < _repetitions; ++i)
	    {
		m.put(new Integer(i), new Integer(i));
	    }
	}

	double total = _watch.stop();
        double perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " " + ((fast) ? "fast " : "") + "writes: " + total + "ms");
        System.out.println("\ttime per write: " + perRecord + "ms");

	//
	// Read each record.
	//
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    Integer n = (Integer)m.get(new Integer(i));
	    test(n.intValue() == i);
	}
        total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " reads: " + total + "ms");
        System.out.println("\ttime per read: " + perRecord + "ms");

	//
	// Remove each record.
	//
	_watch.start();
	if (fast)
	{
	    for (int i = 0; i < _repetitions; ++i)
	    {
		test(m.fastRemove(new Integer(i)));
	    }
	}
	else
	{
	    for (int i = 0; i < _repetitions; ++i)
	    {
		Integer n = (Integer)m.remove(new Integer(i));
		test(n.intValue() == i);
	    }
	}
	total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " " + ((fast) ? "fast " : "") + "removes: " + total + "ms");
        System.out.println("\ttime per remove: " + perRecord + "ms");

	db.close();
    }

    interface Generator
    {
	int next();
	String toString();
    }

    static class RandomGenerator implements Generator
    {
	RandomGenerator(int seed, int max)
	{
	    _r = new java.util.Random(0);
	    _max = max;
	}

	public int
	next()
	{
	    return _r.nextInt(_max);
	}

	public String
	toString()
	{
	    return "random(" + _max + ")";
	}

	private java.util.Random _r;
	private int _max;
    };

    static class SequentialGenerator implements Generator
    {
	SequentialGenerator(int min, int max)
	{
	    _min = min;
	    _max = max;
	    _current = _min;
	}

	public int
	next()
	{
	    int n = _current;
	    ++_current;
	    if (_current > _max)
	    {
		_current = _min;
	    }
	    return n;
	}

	public String
	toString()
	{
	    return new Integer((_max - _min)+1).toString();
	}

	private int _min;
	private int _max;
	private int _current;
    }

    void
    GeneratedRead(IntIntMap m, int reads, Generator gen)
    {
	_watch.start();
	for (int i = 0; i < reads; ++i)
	{
	    int key = gen.next();
	    Integer n = (Integer)m.get(new Integer(key));
	    test(n.intValue() == key);
	}
        double total = _watch.stop();
        double perRecord = total / reads;

        System.out.println("\ttime for " + reads + " reads of " + gen + " records: " +
			   total + "ms");
	System.out.println("\ttime per read: " + perRecord + "ms");
    }

    void
    IntIntMapReadTest(Freeze.DBEnvironment dbEnv)
    {
	Freeze.DB db = dbEnv.openDB("IntIntMap", true);

	IntIntMap m = new IntIntMap(db);

	//
	// Populate the database.
	//
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    m.fastPut(new Integer(i), new Integer(i));
	}

	double total = _watch.stop();
        double perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " writes: " + total + "ms");
        System.out.println("\ttime per write: " + perRecord + "ms");

	//
	// Do some read tests.
	//
	GeneratedRead(m, _repetitions, new SequentialGenerator(1000, 1000));
	GeneratedRead(m, _repetitions, new SequentialGenerator(2000, 2009));
	GeneratedRead(m, _repetitions, new SequentialGenerator(3000, 3099));
	GeneratedRead(m, _repetitions, new SequentialGenerator(4000, 4999));

	//
	// Do a random read test.
	//
	GeneratedRead(m, _repetitions, new RandomGenerator(0, 10000));

	//
	// Remove each record.
	//
/*
 *      For this test I don't want to remove the records because I
 *      want to examine the cache stats for the database.
 *
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    test(m.fastRemove(new Integer(i)));
	}
	total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " removes: " + total + "ms");
        System.out.println("\ttime per remove: " + perRecord + "ms");
*/

	db.close();
    }

    void
    Struct1Struct2MapTest(Freeze.DBEnvironment dbEnv)
    {
	Freeze.DB db = dbEnv.openDB("Struct1Struct2", true);

	Struct1Struct2Map m = new Struct1Struct2Map(db);

	//
	// Populate the database.
	//
	Struct1 s1 = new Struct1();
	Struct2 s2 = new Struct2();
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;
	    s2.s = new Integer(i).toString();
	    m.fastPut(s1, s2);
	}

	double total = _watch.stop();
        double perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " writes: " + total + "ms");
        System.out.println("\ttime per write: " + perRecord + "ms");

	//
	// Read each record.
	//
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;
	    Struct2 ns2 = (Struct2)m.get(s1);
	    test(ns2.s.equals(new Integer(i).toString()));
	}
        total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " reads: " + total + "ms");
        System.out.println("\ttime per read: " + perRecord + "ms");

	//
	// Remove each record.
	//
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;
	    test(m.fastRemove(s1));
	}
	total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " removes: " + total + "ms");
        System.out.println("\ttime per remove: " + perRecord + "ms");

	db.close();
    }

    void
    Struct1Class1MapTest(Freeze.DBEnvironment dbEnv)
    {
	Freeze.DB db = dbEnv.openDB("Struct1Class1", true);

	Struct1Class1Map m = new Struct1Class1Map(db);

	//
	// Populate the database.
	//
	Struct1 s1 = new Struct1();
	Class1 c1 = new Class1();
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;
	    c1.s = new Integer(i).toString();
	    m.fastPut(s1, c1);
	}

	double total = _watch.stop();
        double perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " writes: " + total + "ms");
        System.out.println("\ttime per write: " + perRecord + "ms");

	//
	// Read each record.
	//
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;
	    Class1 nc1 = (Class1)m.get(s1);
	    test(nc1.s.equals(new Integer(i).toString()));
	}
        total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " reads: " + total + "ms");
        System.out.println("\ttime per read: " + perRecord + "ms");

	//
	// Remove each record.
	//
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;
	    test(m.fastRemove(s1));
	}
	total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " removes: " + total + "ms");
        System.out.println("\ttime per remove: " + perRecord + "ms");

	db.close();
    }

    void
    Struct1ObjectMapTest(Freeze.DBEnvironment dbEnv)
    {
	Freeze.DB db = dbEnv.openDB("Struct1Object", true);

	Struct1ObjectMap m = new Struct1ObjectMap(db);

	//
	// Populate the database.
	//
	Struct1 s1 = new Struct1();
	Class1 c1 = new Class1();
	Class2 c2 = new Class2();
	c2.rec = c2;
	c2.obj = c1;
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;
	    Object o;
	    if ((i % 2) == 0)
	    {
		o = c2;
	    }
	    else
	    {
		o = c1;
	    }
	    c1.s = new Integer(i).toString();

	    m.fastPut(s1, o);
	}

	double total = _watch.stop();
        double perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " writes: " + total + "ms");
        System.out.println("\ttime per write: " + perRecord + "ms");

	//
	// Read each record.
	//
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;

	    Object o = m.get(s1);

	    Class1 nc1;
	    if ((i % 2) == 0)
	    {
		Class2 nc2 = (Class2)o;
		test(nc2.rec == nc2);
		nc1 = (Class1)nc2.obj;
	    }
	    else
	    {
		nc1 = (Class1)o;
	    }

	    test(nc1.s.equals(new Integer(i).toString()));
	}
        total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " reads: " + total + "ms");
        System.out.println("\ttime per read: " + perRecord + "ms");

	//
	// Remove each record.
	//
	_watch.start();
	for (int i = 0; i < _repetitions; ++i)
	{
	    s1.l = i;
	    test(m.fastRemove(s1));
	}
	total = _watch.stop();
        perRecord = total / _repetitions;

        System.out.println("\ttime for " + _repetitions + " removes: " + total + "ms");
        System.out.println("\ttime per remove: " + perRecord + "ms");

	db.close();
    }
    
    public int
    runFreeze(String[] args, Freeze.DBEnvironment dbEnv)
    {
	System.out.println("IntIntMap (Collections API)");
	IntIntMapTest(dbEnv, false);

	System.out.println("IntIntMap (Fast API)");
	IntIntMapTest(dbEnv, true);

	System.out.println("Struct1Struct2Map");
	Struct1Struct2MapTest(dbEnv);

	System.out.println("Struct1Class1Map");
	Struct1Class1MapTest(dbEnv);

	MyFactory factory = new MyFactory();
	factory.install(dbEnv.getCommunicator());

	System.out.println("Struct1ObjectMap");
	Struct1ObjectMapTest(dbEnv);

	System.out.println("IntIntMap (read test)");
	IntIntMapReadTest(dbEnv);

	return 0;
    }

    private static void
    test(boolean b)
    {
        if (!b)
        {
            throw new RuntimeException();
        }
    }

    static private class MyFactory implements Ice.ObjectFactory
    {
	public Ice.Object
	create(String type)
	{
	    if (type.equals("::Class1"))
	    {
		return new Class1();
	    }
	    else if (type.equals("::Class2"))
	    {
		return new Class2();
	    }
	    throw new RuntimeException();
	}
	
	public void
	destroy()
	{
	}

	public void
	install(Ice.Communicator communicator)
	{
	    communicator.addObjectFactory(this, "::Class1");
	    communicator.addObjectFactory(this, "::Class2");
	}
    }

    private int _repetitions = 10000;
    private StopWatch _watch = new StopWatch();
}

public class Client
{
    static public void
    main(String[] args)
    {
	TestApp app = new TestApp("db");
	app.main("test.Freeze.bench.Client", args, "config");
    }
}