1 16 17 18 package org.apache.xmlrpc; 19 20 import java.net.URL ; 21 import java.util.Vector ; 22 23 public class AsyncBenchmark 24 implements Runnable 25 { 26 27 XmlRpcClient client; 28 static String url; 29 static int clients = 16; 30 static int loops = 100; 31 32 int calls = 0; 33 34 int gCalls = 0, gErrors = 0; 35 long start; 36 37 38 public AsyncBenchmark () throws Exception 39 { 40 client = new XmlRpcClientLite (url); 41 42 Vector args = new Vector (); 43 args.addElement (new Integer (123)); 47 client.execute ("math.abs", args); 48 49 start = System.currentTimeMillis (); 50 51 for (int i = 0; i < clients; i++) 52 new Thread (this).start (); 53 } 54 55 public void run () 56 { 57 int calls = 0; 58 long start = System.currentTimeMillis (); 59 60 for (int i = 0; i < loops; i++) 61 { 62 63 Vector args = new Vector (); 64 Integer n = new Integer ( 65 Math.round ((int)(Math.random () * -1000))); 66 args.addElement (n); 67 client.executeAsync ("math.abs", args, new Callback (n)); 68 calls += 1; 69 } 70 int millis = (int)(System.currentTimeMillis () - start); 71 System.err.println ("Benchmark thread finished: "+calls + " calls in "+ 72 millis + " milliseconds."); 73 } 74 75 public static void main (String args[]) throws Exception 76 { 77 if (args.length > 0 && args.length < 3) 78 { 79 url = args[0]; 80 XmlRpc.setKeepAlive (true); 81 if (args.length == 2) 82 XmlRpc.setDriver (args[1]); 83 new AsyncBenchmark (); 84 } 85 else 86 { 87 System.err.println ("Usage: java org.apache.xmlrpc.Benchmark URL [SAXDriver]"); 88 } 89 } 90 91 class Callback implements AsyncCallback 92 { 93 94 95 int n; 96 97 public Callback (Integer n) 98 { 99 this.n = Math.abs (n.intValue()); 100 } 101 102 public synchronized void handleResult (Object result, URL url, 103 String method) 104 { 105 if (n == ((Integer ) result).intValue ()) 106 gCalls += 1; 107 else 108 gErrors += 1; 109 if (gCalls + gErrors >= clients * loops) 110 printStats (); 111 } 112 113 public synchronized void handleError (Exception exception, 114 URL url, String method) 115 { 116 System.err.println (exception); 117 exception.printStackTrace (); 118 gErrors += 1; 119 if (gCalls + gErrors >= clients * loops) 120 printStats (); 121 } 122 123 public void printStats () 124 { 125 System.err.println (""); 126 System.err.println (gCalls + " calls, "+gErrors + " errors in "+ 127 (System.currentTimeMillis() - start) + " millis"); 128 System.err.println ( (1000 * (gCalls + gErrors) / 129 (System.currentTimeMillis() - start)) + " calls per second"); 130 } 131 132 } 133 134 135 } 136 | Popular Tags |