Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/Strings.java
41162 views
/*1* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25*/2627package bench.serial;2829import bench.Benchmark;30import java.io.ObjectInputStream;31import java.io.ObjectOutputStream;32import java.util.Random;3334/**35* Benchmark for testing speed of string reads/writes.36*/37public class Strings implements Benchmark {3839/**40* Write and read strings to/from a stream. The benchmark is run in41* batches: each "batch" consists of a fixed number of read/write cycles,42* and the stream is flushed (and underlying stream buffer cleared) in43* between each batch.44* Arguments: <string length> <# batches> <# cycles per batch>45*/46public long run(String[] args) throws Exception {47int slen = Integer.parseInt(args[0]);48int nbatches = Integer.parseInt(args[1]);49int ncycles = Integer.parseInt(args[2]);50String[] strs = genStrings(slen, ncycles);51StreamBuffer sbuf = new StreamBuffer();52ObjectOutputStream oout =53new ObjectOutputStream(sbuf.getOutputStream());54ObjectInputStream oin =55new ObjectInputStream(sbuf.getInputStream());5657doReps(oout, oin, sbuf, strs, 1, ncycles); // warmup5859long start = System.currentTimeMillis();60doReps(oout, oin, sbuf, strs, nbatches, ncycles);61return System.currentTimeMillis() - start;62}6364/**65* Generate nstrings random strings, each of length len.66*/67String[] genStrings(int len, int nstrings) {68String[] strs = new String[nstrings];69char[] ca = new char[len];70Random rand = new Random(System.currentTimeMillis());71for (int i = 0; i < nstrings; i++) {72for (int j = 0; j < len; j++) {73ca[j] = (char) rand.nextInt();74}75strs[i] = new String(ca);76}77return strs;78}7980/**81* Run benchmark for given number of batches, with given number of cycles82* for each batch.83*/84void doReps(ObjectOutputStream oout, ObjectInputStream oin,85StreamBuffer sbuf, String[] strs, int nbatches, int ncycles)86throws Exception87{88for (int i = 0; i < nbatches; i++) {89sbuf.reset();90oout.reset();91for (int j = 0; j < ncycles; j++) {92oout.writeObject(strs[j]);93}94oout.flush();95for (int j = 0; j < ncycles; j++) {96oin.readObject();97}98}99}100}101102103