blob: aad74b7212bf22ad1b6d4a4d0539cb9a3deb897a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 *
26 */
27
28package bench.serial;
29
30import bench.Benchmark;
31import java.io.ObjectInputStream;
32import java.io.ObjectOutputStream;
33import java.util.Random;
34
35/**
36 * Benchmark for testing speed of string reads/writes.
37 */
38public class Strings implements Benchmark {
39
40 /**
41 * Write and read strings to/from a stream. The benchmark is run in
42 * batches: each "batch" consists of a fixed number of read/write cycles,
43 * and the stream is flushed (and underlying stream buffer cleared) in
44 * between each batch.
45 * Arguments: <string length> <# batches> <# cycles per batch>
46 */
47 public long run(String[] args) throws Exception {
48 int slen = Integer.parseInt(args[0]);
49 int nbatches = Integer.parseInt(args[1]);
50 int ncycles = Integer.parseInt(args[2]);
51 String[] strs = genStrings(slen, ncycles);
52 StreamBuffer sbuf = new StreamBuffer();
53 ObjectOutputStream oout =
54 new ObjectOutputStream(sbuf.getOutputStream());
55 ObjectInputStream oin =
56 new ObjectInputStream(sbuf.getInputStream());
57
58 doReps(oout, oin, sbuf, strs, 1, ncycles); // warmup
59
60 long start = System.currentTimeMillis();
61 doReps(oout, oin, sbuf, strs, nbatches, ncycles);
62 return System.currentTimeMillis() - start;
63 }
64
65 /**
66 * Generate nstrings random strings, each of length len.
67 */
68 String[] genStrings(int len, int nstrings) {
69 String[] strs = new String[nstrings];
70 char[] ca = new char[len];
71 Random rand = new Random(System.currentTimeMillis());
72 for (int i = 0; i < nstrings; i++) {
73 for (int j = 0; j < len; j++) {
74 ca[j] = (char) rand.nextInt();
75 }
76 strs[i] = new String(ca);
77 }
78 return strs;
79 }
80
81 /**
82 * Run benchmark for given number of batches, with given number of cycles
83 * for each batch.
84 */
85 void doReps(ObjectOutputStream oout, ObjectInputStream oin,
86 StreamBuffer sbuf, String[] strs, int nbatches, int ncycles)
87 throws Exception
88 {
89 for (int i = 0; i < nbatches; i++) {
90 sbuf.reset();
91 oout.reset();
92 for (int j = 0; j < ncycles; j++) {
93 oout.writeObject(strs[j]);
94 }
95 oout.flush();
96 for (int j = 0; j < ncycles; j++) {
97 oin.readObject();
98 }
99 }
100 }
101}
102