001: // $Id: QueueSpeedTest.java,v 1.7 2005/05/30 16:15:12 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import org.jgroups.util.LinkedListQueue;
006: import org.jgroups.util.Queue;
007:
008: /**
009: * Measures the speed of inserting and removing 1 million elements into/from a Queue and a LinkedQueue.
010: *
011: * @author Bela Ban
012: */
013: public class QueueSpeedTest {
014: int num_elements = 1000000;
015: final int NUM = 10;
016:
017: public QueueSpeedTest(int num_elements) {
018: this .num_elements = num_elements;
019: }
020:
021: public void start() throws Exception {
022: double q1 = 0, q2 = 0, diff;
023:
024: System.out.println("warming up cache");
025: measureQueue();
026: measureLinkedListQueue();
027:
028: System.out.println("running insertions " + NUM
029: + " times (will take average)");
030: for (int i = 0; i < NUM; i++) {
031: System.out.println("Round #" + (i + 1));
032: q1 += measureQueue();
033: q2 += measureLinkedListQueue();
034: }
035:
036: q1 = q1 / NUM;
037: q2 = q2 / NUM;
038:
039: System.out.println("Time to insert and remove " + num_elements
040: + " into Queue: " + q1 + " ms");
041: System.out.println("Time to insert and remove " + num_elements
042: + " into LinkedListQueue: " + q2 + " ms");
043:
044: diff = (long) (q2 - q1);
045: System.out.println("diff is " + Math.abs(diff) + "; "
046: + (q2 < q1 ? "LinkedListQueue" : "Queue")
047: + " is faster");
048: }
049:
050: long measureQueue() throws Exception {
051: Queue q = new Queue();
052: long start, stop;
053:
054: start = System.currentTimeMillis();
055: for (int i = 0; i < num_elements; i++) {
056: if (i % 2 == 0)
057: q.add(new Integer(i));
058: else
059: q.addAtHead(new Integer(i));
060: }
061:
062: while (q.size() > 0)
063: q.remove();
064:
065: stop = System.currentTimeMillis();
066: return stop - start;
067: }
068:
069: long measureLinkedListQueue() throws Exception {
070: LinkedListQueue q = new LinkedListQueue();
071: long start, stop;
072:
073: start = System.currentTimeMillis();
074: for (int i = 0; i < num_elements; i++) {
075: if (i % 2 == 0)
076: q.add(new Integer(i));
077: else
078: q.addAtHead(new Integer(i));
079: }
080:
081: while (q.size() > 0)
082: q.remove();
083:
084: stop = System.currentTimeMillis();
085: return stop - start;
086: }
087:
088: public static void main(String[] args) {
089: int num_elements = 1000000;
090:
091: for (int i = 0; i < args.length; i++) {
092: if ("-num_elements".equals(args[i])) {
093: num_elements = Integer.parseInt(args[++i]);
094: continue;
095: }
096: help();
097: return;
098: }
099:
100: try {
101: new QueueSpeedTest(num_elements).start();
102: } catch (Exception ex) {
103: System.err.println(ex);
104: }
105: }
106:
107: static void help() {
108: System.out
109: .println("QueueSpeedTest [-help] [-num_elements <num>]");
110: }
111: }
|