KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > Benchmark


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xmlrpc;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 public class Benchmark
25     implements Runnable JavaDoc
26 {
27
28     XmlRpcClient client;
29     static String JavaDoc 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 JavaDoc date;
38
39     public Benchmark () throws Exception JavaDoc
40     {
41         client = new XmlRpcClientLite (url);
42
43         Vector JavaDoc args = new Vector JavaDoc ();
44         // Some JITs (Symantec, IBM) have problems with several Threads
45
// starting all at the same time.
46
// This initial XML-RPC call seems to pacify them.
47
args.addElement (new Integer JavaDoc (123));
48         client.execute ("math.abs", args);
49         date = new Date JavaDoc ();
50         date = new Date JavaDoc ((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 JavaDoc (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 JavaDoc args = new Vector JavaDoc ();
67
68             // ECHO STRING
69
// args.addElement (Integer.toString (val));
70
// ABS INT
71
args.addElement (new Integer JavaDoc (val));
72             // ECHO DATE
73
// args.addElement (date);
74

75             for (int i = 0; i < loops; i++)
76             {
77
78                 // ABS INT
79
Integer JavaDoc ret = (Integer JavaDoc) client.execute ("math.abs", args);
80                 // ECHO
81
// Vector v = (Vector) client.execute ("echo", args);
82
// ECHO DATE
83
// Date d = (Date) v.elementAt (0);
84

85                 // ABS INT
86
if (ret.intValue () != Math.abs (val))
87                 {
88                     // ECHO DATE
89
// if (date.getTime() != d.getTime()) {
90
// ECHO STRING
91
// if (!Integer.toString(val).equals (v.elementAt (0))) {
92
errors += 1;
93                 }
94                 calls += 1;
95             }
96         }
97         catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc args[]) throws Exception JavaDoc
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