1 package org.sapia.ubik.net.mplex; 2 3 import org.sapia.ubik.net.SocketConnection; 4 5 import java.io.ByteArrayOutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.OutputStream ; 9 10 import java.net.HttpURLConnection ; 11 import java.net.URL ; 12 13 import java.util.Random ; 14 15 16 24 public class MultiplexedClient { 25 public static final long START_TIME = System.currentTimeMillis(); 26 public static final int SLEEP_FACTOR = 200; 27 private static int _totalThreadsHttp; 28 private static long _totalTimeHttp; 29 private static long _minTimeHttp; 30 private static long _maxTimeHttp; 31 private static long _iterationHttp; 32 private static int _totalThreadsObject; 33 private static long _totalTimeObject; 34 private static long _minTimeObject; 35 private static long _maxTimeObject; 36 private static long _iterationObject; 37 private static final byte TYPE_HTTP = 1; 38 private static final byte TYPE_OBJECT = 2; 39 private static boolean statsLogged = false; 40 41 44 public MultiplexedClient() { 45 super(); 46 } 47 48 public static synchronized void addStat(long delta, byte type) { 49 if (type == TYPE_HTTP) { 50 _totalTimeHttp += delta; 51 _iterationHttp++; 52 _minTimeHttp = ((_minTimeHttp == 0) ? delta 53 : Math.min(_minTimeHttp, delta)); 54 _maxTimeHttp = Math.max(_maxTimeHttp, delta); 55 } else if (type == TYPE_OBJECT) { 56 _totalTimeObject += delta; 57 _iterationObject++; 58 _minTimeObject = ((_minTimeObject == 0) ? delta 59 : Math.min(_minTimeObject, delta)); 60 _maxTimeObject = Math.max(_maxTimeObject, delta); 61 } 62 } 63 64 public static synchronized void logStat() { 65 StringBuffer aBuffer = new StringBuffer (); 66 aBuffer.append(System.currentTimeMillis() - START_TIME) 67 .append("\tHTTP\tthread\t").append(_totalThreadsHttp) 68 .append("\tcount\t").append(_iterationHttp).append("\tavg time\t") 69 .append(_totalTimeHttp / _iterationHttp).append("\tmin time\t") 70 .append(_minTimeHttp).append("\tmax time\t").append(_maxTimeHttp) 71 .append("\ttot time\t").append(_totalTimeHttp) 72 .append("\n\tOBJECT\tthread\t").append(_totalThreadsObject) 73 .append("\tcount\t").append(_iterationObject).append("\tavg time\t") 74 .append(_totalTimeObject / _iterationObject).append("\tmin time\t") 75 .append(_minTimeObject).append("\tmax time\t").append(_maxTimeObject) 76 .append("\ttot time\t").append(_totalTimeObject).append("\n"); 77 78 System.out.println(aBuffer.toString()); 79 } 80 81 public static void log(Object log) { 82 log((log == null) ? "null" : log.toString()); 83 } 84 85 public static synchronized void log(String log) { 86 StringBuffer aBuffer = new StringBuffer (); 87 aBuffer.append(System.currentTimeMillis() - START_TIME).append(" [") 88 .append(Thread.currentThread().getName()).append("] ").append(log); 89 90 if ((_iterationHttp > 0) && (_iterationObject > 0) && 92 (((_iterationHttp + _iterationObject) % 100) == 0)) { 93 if (!statsLogged) { 94 statsLogged = true; 95 logStat(); 96 } 97 } else { 98 statsLogged = false; 99 } 100 } 101 102 public static void main(String [] args) { 103 for (int i = 1; i <= 25; i++) { 104 Thread objectClient = new Thread (new ObjectClient(), "object-client-" + 105 i); 106 objectClient.start(); 107 _totalThreadsObject++; 108 109 Thread httpClient = new Thread (new HttpClient(), "http-client-" + i); 110 httpClient.start(); 111 _totalThreadsHttp++; 112 113 try { 114 Thread.sleep(2000); 115 } catch (InterruptedException e) { 116 } 117 } 118 } 119 120 124 public static class ObjectClient implements Runnable { 125 private Random _random = new Random (); 126 private SocketConnection _conn; 127 128 private SocketConnection getConnection() throws IOException { 129 if (_conn == null) { 130 log("Getting new connection to port 7777..."); 131 _conn = new SocketConnection(new java.net.Socket ("localhost", 7777)); 132 log("Connected to " + _conn.getServerAddress()); 133 } 134 135 return _conn; 136 } 137 138 public void run() { 139 try { 140 while (true) { 141 try { 142 log("Sending..."); 143 144 long start = System.currentTimeMillis(); 145 Object request = "This is foo!!!"; 146 getConnection().send(request); 147 148 log("Receiving..."); 149 150 Object response = getConnection().receive(); 151 addStat(System.currentTimeMillis() - start, TYPE_OBJECT); 152 log(response); 153 154 Thread.sleep(_random.nextInt(50) * SLEEP_FACTOR); 156 } catch (IOException ioe) { 157 ioe.printStackTrace(); 158 159 if (_conn == null) { 160 break; 161 } 162 163 log("Resetting connection..."); 164 _conn.close(); 165 _conn = null; 166 } 167 } 168 } catch (Throwable t) { 169 t.printStackTrace(); 170 } 171 } 172 } 173 174 178 public static class HttpClient implements Runnable { 179 private Random _random = new Random (); 180 181 private HttpURLConnection getConnection() throws IOException { 182 log("Getting new HTTP connection to port 7777..."); 183 184 URL anURL = new URL ("http://localhost:7777"); 185 HttpURLConnection _conn = (HttpURLConnection ) anURL.openConnection(); 186 _conn.setRequestMethod("POST"); 187 _conn.setDoInput(true); 188 _conn.setDoOutput(true); 189 _conn.setUseCaches(false); 190 log("Connected to localhost:7777"); 191 192 return _conn; 193 } 194 195 public void run() { 196 try { 197 while (true) { 198 HttpURLConnection conn = null; 199 200 try { 201 long start = System.currentTimeMillis(); 202 conn = getConnection(); 203 204 log("Sending HTTP..."); 205 206 OutputStream os = conn.getOutputStream(); 207 os.write("This is HTTP foo!!!".getBytes()); 208 os.flush(); 209 210 log("Receiving HTTP..."); 211 212 InputStream is = conn.getInputStream(); 213 ByteArrayOutputStream response = new ByteArrayOutputStream (); 214 215 boolean isDone = false; 216 byte[] data = new byte[1024]; 217 218 while (!isDone) { 219 int length = is.read(data); 220 221 if (length >= 0) { 222 response.write(data, 0, length); 223 } 224 225 isDone = is.available() == 0; 226 } 227 228 addStat(System.currentTimeMillis() - start, TYPE_HTTP); 229 log(response.toString("UTF-8")); 230 231 Thread.sleep(_random.nextInt(50) * SLEEP_FACTOR); 233 } catch (IOException ioe) { 234 ioe.printStackTrace(); 235 } finally { 236 if (conn == null) { 237 break; 238 } 239 240 conn.disconnect(); 241 } 242 } 243 } catch (Throwable t) { 244 t.printStackTrace(); 245 } 246 } 247 } 248 } 249 | Popular Tags |