1 16 17 18 package org.apache.xmlrpc; 19 20 import java.io.IOException ; 21 import java.util.Date ; 22 import java.util.Vector ; 23 24 public class Benchmark 25 implements Runnable 26 { 27 28 XmlRpcClient client; 29 static String url; 30 static int clients = 16; 31 static int loops = 100; 32 33 long start; 34 35 int gCalls = 0, gErrors = 0; 36 37 Date date; 38 39 public Benchmark () throws Exception 40 { 41 client = new XmlRpcClientLite (url); 42 43 Vector args = new Vector (); 44 args.addElement (new Integer (123)); 48 client.execute ("math.abs", args); 49 date = new Date (); 50 date = new Date ((date.getTime() / 1000) * 1000); 51 52 start = System.currentTimeMillis (); 53 int nclients = clients; 54 55 for (int i = 0; i < nclients; i++) 56 new Thread (this).start (); 57 } 58 59 public void run () 60 { 61 int errors = 0; 62 int calls = 0; 63 try 64 { 65 int val = (int)(-100 * Math.random ()); 66 Vector args = new Vector (); 67 68 args.addElement (new Integer (val)); 72 75 for (int i = 0; i < loops; i++) 76 { 77 78 Integer ret = (Integer ) client.execute ("math.abs", args); 80 85 if (ret.intValue () != Math.abs (val)) 87 { 88 errors += 1; 93 } 94 calls += 1; 95 } 96 } 97 catch (IOException x) 98 { 99 System.err.println ("Exception in client: "+x); 100 x.printStackTrace (); 101 } 102 catch (XmlRpcException x) 103 { 104 System.err.println ("Server reported error: "+x); 105 } 106 catch (Exception other) 107 { 108 System.err.println ("Exception in Benchmark client: "+other); 109 } 110 int millis = (int)(System.currentTimeMillis () - start); 111 checkout (calls, errors, millis); 112 } 113 114 private synchronized void checkout (int calls, int errors, int millis) 115 { 116 clients--; 117 gCalls += calls; 118 gErrors += errors; 119 System.err.println ("Benchmark thread finished: "+calls + " calls, "+ 120 errors + " errors in "+millis + " milliseconds."); 121 if (clients == 0) 122 { 123 System.err.println (""); 124 System.err.println ("Benchmark result: "+ 125 (1000 * gCalls / millis) + " calls per second."); 126 } 127 } 128 129 public static void main (String args[]) throws Exception 130 { 131 if (args.length > 0 && args.length < 3) 132 { 133 url = args[0]; 134 XmlRpc.setKeepAlive (true); 135 if (args.length == 2) 136 XmlRpc.setDriver (args[1]); 137 new Benchmark (); 138 } 139 else 140 { 141 System.err.println ("Usage: java org.apache.xmlrpc.Benchmark URL [SAXDriver]"); 142 } 143 } 144 145 } 146 | Popular Tags |