1 23 24 29 package com.sun.enterprise.management.base; 30 31 import java.util.Set ; 32 import java.util.HashSet ; 33 import java.util.Collections ; 34 35 import java.io.IOException ; 36 import java.io.File ; 37 import java.io.FileOutputStream ; 38 39 import javax.management.ObjectName ; 40 import javax.management.AttributeList ; 41 import javax.management.Notification ; 42 import javax.management.MBeanServerConnection ; 43 import javax.management.NotCompliantMBeanException ; 44 45 import com.sun.appserv.management.base.UploadDownloadMgr; 46 47 import com.sun.enterprise.management.AMXTestBase; 48 import com.sun.enterprise.management.Capabilities; 49 50 import com.sun.appserv.management.util.misc.ExceptionUtil; 51 52 import com.sun.enterprise.management.PropertyKeys; 53 54 55 62 public final class UploadDownloadMgrTest extends AMXTestBase 63 { 64 public 65 UploadDownloadMgrTest( ) 66 throws IOException 67 { 68 } 69 70 public static Capabilities 71 getCapabilities() 72 { 73 return getOfflineCapableCapabilities( true ); 74 } 75 76 public UploadDownloadMgr 77 getUploadDownloadMgr() 78 { 79 return( getDomainRoot().getUploadDownloadMgr() ); 80 } 81 82 83 public Object 84 upload( final String name, final int totalSize ) 85 throws IOException 86 { 87 final UploadDownloadMgr mgr = getUploadDownloadMgr(); 88 90 final int chunkSize = 32 * 1024; 91 92 final long start = now(); 93 94 final Object uploadID = mgr.initiateUpload( name, totalSize ); 95 int remaining = totalSize; 96 boolean done = false; 97 while ( remaining != 0 ) 98 { 99 final int actual = remaining < chunkSize ? remaining : chunkSize; 100 101 final byte[] bytes = new byte[ actual ]; 102 done = mgr.uploadBytes( uploadID, bytes ); 103 remaining -= actual; 104 } 106 assert( done ); 107 108 printElapsed( "UploadDownloadMgr.upload: " + totalSize + " bytes", start ); 109 return( uploadID ); 110 } 111 112 113 private File 114 createTempFile( final long totalSize ) 115 throws IOException 116 { 117 final long start = now(); 118 119 final File temp = File.createTempFile( "UploadDownloadMgrTest", "junk" ); 120 121 temp.deleteOnExit(); 122 123 final FileOutputStream os = new FileOutputStream ( temp ); 124 125 try 126 { 127 long remaining = totalSize; 128 129 final byte[] junk = new byte[ 1024 * 1024 ]; 130 131 while ( remaining != 0 ) 132 { 133 final long actual = remaining < junk.length ? remaining : junk.length; 134 135 os.write( junk, 0, (int)actual ); 136 remaining -= actual; 137 } 138 os.close(); 139 } 140 catch( IOException e ) 141 { 142 os.close(); 143 temp.delete(); 144 throw e; 145 } 146 147 assert( temp.length() == totalSize ); 148 149 printElapsed( "UploadDownloadMgr.createTempFile: " + 150 totalSize + " bytes", start ); 151 return( temp ); 152 } 153 154 public File 155 testDownloadFile( 156 final int testSize, 157 final int chunkSize ) 158 throws IOException 159 { 160 final UploadDownloadMgr mgr = 161 getDomainRoot().getUploadDownloadMgr(); 162 163 final File testFile = createTempFile( testSize ); 164 165 final long start = now(); 166 final Object id = mgr.initiateDownload( testFile, true ); 167 168 final int maxChunkSize = mgr.getMaxDownloadChunkSize(); 170 final int actualChunkSize = chunkSize < maxChunkSize ? 171 chunkSize : maxChunkSize; 172 173 final long length = mgr.getDownloadLength( id ); 174 long doneSoFar = 0; 175 while ( doneSoFar < length ) 176 { 177 final byte[] bytes = mgr.downloadBytes( id, actualChunkSize ); 178 doneSoFar += bytes.length; 179 } 180 181 printElapsed( "UploadDownloadMgr.testDownloadFile: " + 182 testSize + " bytes" + " chunksize = " + actualChunkSize, start ); 183 return( testFile ); 184 } 185 186 private final int K = 1024; 187 private final int MEGABYTE = K * K; 188 189 public void 190 testDownloadFileBufferSameSizeSmallerThanDownload() 191 throws IOException 192 { 193 final int size = 256 * K; 194 195 testDownloadFile( size, size -1 ); 196 } 197 198 public void 199 testDownloadFileBufferSameSizeAsDownload() 200 throws IOException 201 { 202 final int size = 256 * K; 203 testDownloadFile( size, size ); 204 } 205 206 public void 207 testDownloadFileBufferLargerThanDownload() 208 throws IOException 209 { 210 final int size = 256 * K; 211 testDownloadFile( size, size + 1 ); 212 } 213 214 public void 215 testDownloadSmallFile() 216 throws IOException 217 { 218 final int size = 50 * K; 219 testDownloadFile( size, size + 1 ); 220 testDownloadFile( size, size ); 221 } 222 223 224 public void 225 testDownloadTinyFile() 226 throws IOException 227 { 228 final int size = 1; 229 testDownloadFile( size, size + 1 ); 230 testDownloadFile( size, size ); 231 } 232 233 public void 234 testDownloadBigFile() 235 throws IOException 236 { 237 final long start = now(); 238 Integer def = new Integer (PropertyKeys.DEFAULT_UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB); 239 final int kb = 240 getEnvInteger( PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB, def).intValue(); 241 assert( kb >= 1 ) : 242 "Test size must be positive, value for " + 243 PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_BIG_FILE_KB + 244 ": " + kb; 245 246 testDownloadFile( kb * K, MEGABYTE ); 247 248 printElapsed( "UploadDownloadMgrTest.testDownloadBigFile: " + kb + "kb", start); 249 } 250 251 252 private final class UploadDownloadTestThread extends Thread 253 { 254 Throwable mThrowable; 255 boolean mDone; 256 final int mLength; 257 long mElapsed; 258 259 public 260 UploadDownloadTestThread( final int length ) 261 { 262 mThrowable = null; 263 mDone = false; 264 mLength = length; 265 mElapsed = 0; 266 } 267 268 public void 269 run() 270 { 271 mDone = false; 272 try 273 { 274 final long start = System.currentTimeMillis(); 275 276 final File f = testDownloadFile( mLength, 1 * K ); 277 upload( f.toString(), mLength ); 278 279 mElapsed = System.currentTimeMillis() - start; 280 } 281 catch( Throwable t ) 282 { 283 mThrowable = t; 284 } 285 mDone = true; 286 } 287 288 long 289 getLength() 290 { 291 return mLength; 292 } 293 294 long 295 getElapsed() 296 { 297 return mElapsed; 298 } 299 300 Throwable 301 getThrowable() 302 { 303 return( mThrowable ); 304 } 305 306 public boolean 307 done() 308 { 309 return( mDone ); 310 } 311 } 312 313 316 public void 317 testHeavilyThreaded() 318 throws IOException 319 { 320 Integer def = new Integer (PropertyKeys.DEFAULT_UPLOAD_DOWNLOAD_MGR_TEST_THREADS); 321 322 int numThreads = getEnvInteger( PropertyKeys.UPLOAD_DOWNLOAD_MGR_TEST_THREADS, def).intValue(); 323 if ( numThreads <= 0 ) 324 { 325 numThreads = 1; 326 } 327 328 printVerbose( "UploadDownloadMgrTest.testHeavilyThreaded: using " + numThreads + " threads." ); 329 330 final UploadDownloadTestThread[] threads = 331 new UploadDownloadTestThread[ numThreads ]; 332 333 for( int i = 0; i < numThreads; ++i ) 335 { 336 threads[ i ] = new UploadDownloadTestThread( i * K + 1 ); 337 threads[ i ].start(); 338 } 339 340 boolean done = false; 342 while ( true ) 343 { 344 int numDone = 0; 345 for( int i = 0; i < numThreads; ++i ) 346 { 347 if ( threads[ i ].done() ) 348 { 349 ++numDone; 350 } 351 } 352 353 if ( numDone == numThreads ) 354 break; 355 356 printVerbose( "UploadDownloadMgrTest.testHeavilyThreaded: waiting for " + 357 (numThreads - numDone) + " of " + numThreads + " threads "); 358 mySleep( 1000 ); 359 } 360 361 for( int i = 0; i < numThreads; ++i ) 363 { 364 assert( threads[ i ].done() ); 365 assert( threads[ i ].getThrowable() == null ) : 366 ExceptionUtil.getStackTrace( threads[ i ].getThrowable() ); 367 } 368 369 } 370 371 public void 372 testUploadFile1() 373 throws IOException 374 { 375 final Object id = upload( "./deploy.temp1." + now(), 1024 * K ); 376 } 377 378 public void 379 testUploadFile2() 380 throws IOException 381 { 382 final Object id = upload( "./deploy.temp2." + now(), 1 + 100 * K ); 383 } 384 385 public void 386 testUploadFile3() 387 throws IOException 388 { 389 final Object id = upload( "./deploy.temp3." + now(), 1 ); 390 } 391 392 public void 393 testUploadFile4() 394 throws IOException 395 { 396 final Object id = upload( "./deploy.temp4." + now(), K + 1 ); 397 } 398 399 public void 400 testUploadFile5() 401 throws IOException 402 { 403 final Object id = upload( null, 1 + 2048 * K ); 404 } 405 } 406 407 408 | Popular Tags |