KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 public class AsyncBenchmark
24     implements Runnable JavaDoc
25 {
26
27     XmlRpcClient client;
28     static String JavaDoc 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 JavaDoc
39     {
40         client = new XmlRpcClientLite (url);
41
42         Vector JavaDoc args = new Vector JavaDoc ();
43         // Some JITs (Symantec, IBM) have problems with several Threads
44
// starting all at the same time.
45
// This initial XML-RPC call seems to pacify them.
46
args.addElement (new Integer JavaDoc (123));
47         client.execute ("math.abs", args);
48
49         start = System.currentTimeMillis ();
50
51         for (int i = 0; i < clients; i++)
52             new Thread JavaDoc (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 JavaDoc args = new Vector JavaDoc ();
64             Integer JavaDoc n = new Integer JavaDoc (
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 JavaDoc args[]) throws Exception JavaDoc
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 JavaDoc n)
98         {
99             this.n = Math.abs (n.intValue());
100         }
101
102         public synchronized void handleResult (Object JavaDoc result, URL JavaDoc url,
103                 String JavaDoc method)
104         {
105             if (n == ((Integer JavaDoc) 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 JavaDoc exception,
114                 URL JavaDoc url, String JavaDoc 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