1 24 25 package org.objectweb.cjdbc.scenario.standalone.socket; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.io.ObjectOutputStream ; 30 import java.net.Socket ; 31 import java.net.UnknownHostException ; 32 import java.util.Enumeration ; 33 import java.util.ResourceBundle ; 34 35 import org.objectweb.cjdbc.common.stream.encoding.HexaEncoding; 36 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility; 37 38 43 public class UTFClient implements Runnable 44 { 45 String hexa; 46 int serverPort = 7603; 47 boolean useWriteObject = false; 48 private int loop = 1; 49 String imageFile = "/image/cinema.pdf"; 50 String serverName = "localhost"; 51 private boolean useBufferedStream = false; 52 private boolean useGC = false; 53 private boolean useSameObject; 54 private boolean useReset; 55 56 59 public String getServerName() 60 { 61 return serverName; 62 } 63 64 67 public void setServerName(String serverName) 68 { 69 this.serverName = serverName; 70 } 71 72 75 public int getLoop() 76 { 77 return loop; 78 } 79 80 83 public void setLoop(int loop) 84 { 85 this.loop = loop; 86 } 87 88 91 public int getServerPort() 92 { 93 return serverPort; 94 } 95 96 99 public void setServerPort(int serverPort) 100 { 101 this.serverPort = serverPort; 102 } 103 104 107 public boolean getUseWriteObject() 108 { 109 return useWriteObject; 110 } 111 112 115 public void setUseWriteObject(boolean useWriteObject) 116 { 117 this.useWriteObject = useWriteObject; 118 } 119 120 123 public String getImageFile() 124 { 125 return imageFile; 126 } 127 128 131 public void setImageFile(String imageFile) 132 { 133 this.imageFile = imageFile; 134 } 135 136 139 public void run() 140 { 141 try 142 { 143 144 File image = new File (getClass().getResource(imageFile).getFile()); 145 hexa = HexaEncoding.data2hex(ScenarioUtility.readBinary(image)); 146 byte[] bytes = hexa.getBytes(); 147 Socket socket = new Socket (serverName, serverPort); 148 ObjectOutputStream out = new ObjectOutputStream (socket.getOutputStream()); 149 150 out.writeBoolean(useBufferedStream); 151 if (useBufferedStream) 152 out = new ObjectOutputStream (socket.getOutputStream()); 153 154 out.writeBoolean(useWriteObject); 155 out.writeBoolean(useGC); 156 out.writeBoolean(false); 157 out.writeInt(loop); 158 out.flush(); 159 160 if (useWriteObject) 161 { 162 for (int i = 0; i < loop; i++) 163 { 164 System.out.println(i); 165 if (!useSameObject) 166 out.writeObject(HexaEncoding.data2hex(ScenarioUtility 167 .readBinary(image))); 168 else 169 out.writeObject(hexa); 170 out.flush(); 171 if (useGC) 172 System.gc(); 173 if (useReset) 174 out.reset(); 175 } 176 } 177 else 178 { 179 for (int i = 0; i < loop; i++) 180 { 181 System.out.println(i); 182 out.writeInt(bytes.length); 183 out.write(bytes); 184 out.flush(); 185 } 186 } 187 188 socket.close(); 189 } 190 catch (UnknownHostException e) 191 { 192 e.printStackTrace(); 194 } 195 catch (IOException e) 196 { 197 e.printStackTrace(); 199 } 200 201 } 202 203 206 public String getHexa() 207 { 208 return hexa; 209 } 210 211 214 public boolean getUseBufferedStream() 215 { 216 return useBufferedStream; 217 } 218 219 222 public void setUseBufferedStream(boolean useBufferedStream) 223 { 224 this.useBufferedStream = useBufferedStream; 225 } 226 227 233 public static void main(String [] args) throws Exception 234 { 235 236 ResourceBundle rb = ResourceBundle.getBundle("socketTest"); 238 Enumeration enu = rb.getKeys(); 239 String key; 240 while (enu.hasMoreElements()) 241 { 242 key = (String ) enu.nextElement(); 243 System.out.println("Using test property <" + key + "> with value <" 244 + rb.getObject(key) + ">"); 245 } 246 int serverPort = Integer.parseInt(rb.getString("serverPort")); 247 String imageFile = rb.getString("imageFile"); 248 int numberOfLoop = Integer.parseInt(rb.getString("numberOfLoop")); 249 String serverName = rb.getString("serverName"); 250 boolean useBufferedStream = new Boolean (rb.getString("useBufferedStream")) 251 .booleanValue(); 252 boolean useWriteObject = new Boolean (rb.getString("useWriteObject")) 253 .booleanValue(); 254 boolean useSameObject = new Boolean (rb.getString("useSameObject")) 255 .booleanValue(); 256 boolean useGC = new Boolean (rb.getString("useGC")).booleanValue(); 257 boolean useReset = new Boolean (rb.getString("useReset")).booleanValue(); 258 259 UTFClient cutf = new UTFClient(); 261 cutf.setServerPort(serverPort); 262 cutf.setLoop(numberOfLoop); 263 cutf.setImageFile(imageFile); 264 cutf.setServerName(serverName); 265 cutf.setUseBufferedStream(useBufferedStream); 266 cutf.setUseWriteObject(useWriteObject); 267 cutf.setUseGC(useGC); 268 cutf.setUseSameObject(useSameObject); 269 cutf.setUseReset(useReset); 270 271 long started = System.currentTimeMillis(); 273 long freeStart = Runtime.getRuntime().totalMemory() 274 - Runtime.getRuntime().freeMemory(); 275 Thread client = new Thread (cutf); 276 client.start(); 277 278 client.join(); 280 long end = System.currentTimeMillis(); 281 long freeEnd = Runtime.getRuntime().totalMemory() 282 - Runtime.getRuntime().freeMemory(); 283 long usedMemory = (freeEnd - freeStart); 284 long last = end - started; 285 long time = last / 1000; 286 float average1 = ((float) numberOfLoop / (float) time); 287 float average2 = ((float) time / (float) numberOfLoop); 288 289 System.out.println("The test lasted " + time + " s. (" + last 290 + " ms.) for an average of " + average1 291 + " loop(s) per second (or 1 loop takes :" + average2 + "seconds"); 292 System.out.println("Used memory was:" + usedMemory / 1024 / 1024 + " Mb, " 293 + usedMemory + " bytes."); 294 } 295 296 299 void setUseReset(boolean useReset) 300 { 301 this.useReset = useReset; 302 } 303 304 307 void setUseSameObject(boolean useSameObject) 308 { 309 this.useSameObject = useSameObject; 310 } 311 312 315 void setUseGC(boolean useGC) 316 { 317 this.useGC = useGC; 318 319 } 320 } | Popular Tags |