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