33 |
33 |
34 /** |
34 /** |
35 * Benchmark for testing speed of long array reads/writes. |
35 * Benchmark for testing speed of long array reads/writes. |
36 */ |
36 */ |
37 public class LongArrays implements Benchmark { |
37 public class LongArrays implements Benchmark { |
38 |
38 |
39 /** |
39 /** |
40 * Write and read long arrays to/from a stream. The benchmark is run in |
40 * Write and read long arrays to/from a stream. The benchmark is run in |
41 * batches, with each batch consisting of a fixed number of read/write |
41 * batches, with each batch consisting of a fixed number of read/write |
42 * cycles. The ObjectOutputStream is reset after each batch of cycles has |
42 * cycles. The ObjectOutputStream is reset after each batch of cycles has |
43 * completed. |
43 * completed. |
44 * Arguments: <array size> <# batches> <# cycles per batch> |
44 * Arguments: <array size> <# batches> <# cycles per batch> |
45 */ |
45 */ |
46 public long run(String[] args) throws Exception { |
46 public long run(String[] args) throws Exception { |
47 int size = Integer.parseInt(args[0]); |
47 int size = Integer.parseInt(args[0]); |
48 int nbatches = Integer.parseInt(args[1]); |
48 int nbatches = Integer.parseInt(args[1]); |
49 int ncycles = Integer.parseInt(args[2]); |
49 int ncycles = Integer.parseInt(args[2]); |
50 long[][] arrays = new long[ncycles][size]; |
50 long[][] arrays = new long[ncycles][size]; |
51 StreamBuffer sbuf = new StreamBuffer(); |
51 StreamBuffer sbuf = new StreamBuffer(); |
52 ObjectOutputStream oout = |
52 ObjectOutputStream oout = |
53 new ObjectOutputStream(sbuf.getOutputStream()); |
53 new ObjectOutputStream(sbuf.getOutputStream()); |
54 ObjectInputStream oin = |
54 ObjectInputStream oin = |
55 new ObjectInputStream(sbuf.getInputStream()); |
55 new ObjectInputStream(sbuf.getInputStream()); |
56 |
56 |
57 doReps(oout, oin, sbuf, arrays, 1); // warmup |
57 doReps(oout, oin, sbuf, arrays, 1); // warmup |
58 |
58 |
59 long start = System.currentTimeMillis(); |
59 long start = System.currentTimeMillis(); |
60 doReps(oout, oin, sbuf, arrays, nbatches); |
60 doReps(oout, oin, sbuf, arrays, nbatches); |
61 return System.currentTimeMillis() - start; |
61 return System.currentTimeMillis() - start; |
62 } |
62 } |
63 |
63 |
64 /** |
64 /** |
65 * Run benchmark for given number of batches, with given number of cycles |
65 * Run benchmark for given number of batches, with given number of cycles |
66 * for each batch. |
66 * for each batch. |
67 */ |
67 */ |
68 void doReps(ObjectOutputStream oout, ObjectInputStream oin, |
68 void doReps(ObjectOutputStream oout, ObjectInputStream oin, |
69 StreamBuffer sbuf, long[][] arrays, int nbatches) |
69 StreamBuffer sbuf, long[][] arrays, int nbatches) |
70 throws Exception |
70 throws Exception |
71 { |
71 { |
72 int ncycles = arrays.length; |
72 int ncycles = arrays.length; |
73 for (int i = 0; i < nbatches; i++) { |
73 for (int i = 0; i < nbatches; i++) { |
74 sbuf.reset(); |
74 sbuf.reset(); |
75 oout.reset(); |
75 oout.reset(); |
76 for (int j = 0; j < ncycles; j++) { |
76 for (int j = 0; j < ncycles; j++) { |
77 oout.writeObject(arrays[j]); |
77 oout.writeObject(arrays[j]); |
78 } |
78 } |
79 oout.flush(); |
79 oout.flush(); |
80 for (int j = 0; j < ncycles; j++) { |
80 for (int j = 0; j < ncycles; j++) { |
81 oin.readObject(); |
81 oin.readObject(); |
82 } |
82 } |
83 } |
83 } |
84 } |
84 } |
85 } |
85 } |
86 |
|
87 |
|