1 23 24 package org.objectweb.fractal.julia.perf; 25 26 import org.objectweb.fractal.api.Component; 27 import org.objectweb.fractal.api.NoSuchInterfaceException; 28 import org.objectweb.fractal.api.factory.Factory; 29 import org.objectweb.fractal.api.factory.GenericFactory; 30 import org.objectweb.fractal.api.type.ComponentType; 31 import org.objectweb.fractal.api.type.InterfaceType; 32 import org.objectweb.fractal.api.type.TypeFactory; 33 34 import org.objectweb.fractal.julia.perf.components.I; 35 import org.objectweb.fractal.julia.perf.components.C; 36 37 import org.objectweb.fractal.util.Fractal; 38 39 48 49 public class TestPerf { 50 51 57 58 public static void main (final String [] args) throws Exception { 59 60 Component boot = Fractal.getBootstrapComponent(); 61 62 TypeFactory tf = Fractal.getTypeFactory(boot); 63 ComponentType type = tf.createFcType(new InterfaceType[] { 64 tf.createFcItfType( 65 "s", 66 I.class.getName(), 67 false, 68 false, 69 false) 70 }); 71 GenericFactory cf = Fractal.getGenericFactory(boot); 72 Component tmpl = cf.newFcInstance( 73 type, "primitiveTemplate", new Object [] { 74 "primitive", C.class.getName() 75 }); 76 77 Component comp = Fractal.getFactory(tmpl).newFcInstance(); 78 try { 79 Fractal.getLifeCycleController(comp).startFc(); 80 } catch (NoSuchInterfaceException _) { 81 } 82 I i = (I)comp.getFcInterface("s"); 83 84 System.out.println("Julia performances, " + args[0]); 85 computeObjectCallDuration(); 86 computeComponentCallDuration(i); 87 computeEmptyObjectSize(); 88 computeEmptyComponentSize(Fractal.getFactory(tmpl)); 89 computeComponentTypeSize((ComponentType)comp.getFcType()); 90 computeComponentCreationDuration(); 91 computeTemplateCreationDuration(); 92 computeTemplateInstantiationDuration(Fractal.getFactory(tmpl)); 93 System.out.println(); 94 } 95 96 100 103 104 private final static int NB_CALLS = 10000000; 105 106 109 110 private final static int LOOPS = 7; 111 112 115 116 private static double objectCallMean; 117 118 121 122 private static void computeObjectCallDuration () { 123 double min = Long.MAX_VALUE; 125 double max = Long.MIN_VALUE; 126 double mean = 0; 127 I itf = new C(); 128 129 for (int j = 0; j < LOOPS; ++j) { 131 int i = NB_CALLS; 132 double dt = System.currentTimeMillis(); 133 while (i > 0) { 134 i = itf.m(i); 135 } 136 dt = 1000 * (System.currentTimeMillis() - dt) / NB_CALLS; 137 if (j > 1) { 139 min = Math.min(dt, min); 141 max = Math.max(dt, max); 142 mean += dt; 143 } 144 } 145 mean = mean / (LOOPS - 2); 147 double delta = 1000 * Math.max(max - mean, mean - min); 149 System.out.print("method call = "); 151 System.out.println(format(mean) + " µs (+/- " + format(delta) + " ns)"); 152 objectCallMean = mean; 155 } 156 157 162 163 private static void computeComponentCallDuration (I itf) { 164 double min = Long.MAX_VALUE; 166 double max = Long.MIN_VALUE; 167 double mean = 0; 168 169 for (int j = 0; j < LOOPS; ++j) { 171 int i = NB_CALLS; 172 double dt = System.currentTimeMillis(); 173 while (i > 0) { 174 i = itf.m(i); 175 } 176 dt = 1000 * (System.currentTimeMillis() - dt) / NB_CALLS; 177 if (j > 1) { 179 min = Math.min(dt, min); 181 max = Math.max(dt, max); 182 mean += dt; 183 } 184 } 185 mean = mean / (LOOPS - 2); 187 double delta = 1000 * Math.max(max - mean, mean - min); 189 double overhead = mean - objectCallMean; 191 System.out.print("component call = "); 193 System.out.print(format(mean) + " µs (+/- " + format(delta) + " ns)"); 194 System.out.print(" => +" + format(overhead) + " µs"); 195 System.out.println(" (" + format(overhead / objectCallMean) + " calls)"); 196 } 197 198 203 204 private static void computeComponentCreationDuration () throws Exception { 205 Component boot = Fractal.getBootstrapComponent(); 206 double min = Long.MAX_VALUE; 208 double max = Long.MIN_VALUE; 209 double mean = 0; 210 for (int j = 0; j < LOOPS; ++j) { 212 int i = 0; 213 double dt = System.currentTimeMillis(); 214 while (i < NB_INSTANCES_2) { 215 TypeFactory tf = Fractal.getTypeFactory(boot); 216 GenericFactory cf = Fractal.getGenericFactory(boot); 217 ComponentType type = tf.createFcType(new InterfaceType[] { 218 tf.createFcItfType( 219 "s", 220 I.class.getName(), 221 false, 222 false, 223 false) 224 }); 225 cf.newFcInstance(type, "primitive", C.class.getName()); 226 ++i; 227 } 228 dt = (System.currentTimeMillis() - dt) / NB_INSTANCES_2; 229 if (j > 1) { 231 min = Math.min(dt, min); 233 max = Math.max(dt, max); 234 mean += dt; 235 } 236 } 237 mean = mean / (LOOPS - 2); 239 double delta = 1000 * Math.max(max - mean, mean - min); 241 System.out.print("component creation = "); 243 System.out.println(format(mean) + " ms (+/- " + format(delta) + " µs)"); 244 } 245 246 251 252 private static void computeTemplateCreationDuration () throws Exception { 253 Component boot = Fractal.getBootstrapComponent(); 254 double min = Long.MAX_VALUE; 256 double max = Long.MIN_VALUE; 257 double mean = 0; 258 for (int j = 0; j < LOOPS; ++j) { 260 int i = 0; 261 double dt = System.currentTimeMillis(); 262 while (i < NB_INSTANCES_2) { 263 TypeFactory tf = Fractal.getTypeFactory(boot); 264 ComponentType type = tf.createFcType(new InterfaceType[] { 265 tf.createFcItfType( 266 "s", 267 I.class.getName(), 268 false, 269 false, 270 false) 271 }); 272 GenericFactory cf = Fractal.getGenericFactory(boot); 273 cf.newFcInstance( 274 type, "primitiveTemplate", new Object [] { 275 "primitive", C.class.getName() 276 }); 277 ++i; 278 } 279 dt = (System.currentTimeMillis() - dt) / NB_INSTANCES_2; 280 if (j > 1) { 282 min = Math.min(dt, min); 284 max = Math.max(dt, max); 285 mean += dt; 286 } 287 } 288 mean = mean / (LOOPS - 2); 290 double delta = 1000 * Math.max(max - mean, mean - min); 292 System.out.print("template creation = "); 294 System.out.println(format(mean) + " ms (+/- " + format(delta) + " µs)"); 295 } 296 297 303 304 private static void computeTemplateInstantiationDuration (Factory tmpl) 305 throws Exception 306 { 307 double min = Long.MAX_VALUE; 309 double max = Long.MIN_VALUE; 310 double mean = 0; 311 for (int j = 0; j < LOOPS; ++j) { 313 int i = 0; 314 double dt = System.currentTimeMillis(); 315 while (i < NB_INSTANCES_2) { 316 tmpl.newFcInstance(); 317 ++i; 318 } 319 dt = (System.currentTimeMillis() - dt) / NB_INSTANCES_2; 320 if (j > 1) { 322 min = Math.min(dt, min); 324 max = Math.max(dt, max); 325 mean += dt; 326 } 327 } 328 mean = mean / (LOOPS - 2); 330 double delta = 1000 * Math.max(max - mean, mean - min); 332 System.out.print("template instantiation = "); 334 System.out.println(format(mean) + " ms (+/- " + format(delta) + " µs)"); 335 } 336 337 341 344 345 private final static int NB_INSTANCES_1 = 100; 346 347 350 351 private final static int NB_INSTANCES_2 = 1000; 352 353 356 357 private static long objectSize; 358 359 362 363 private static void computeEmptyObjectSize () { 364 long oldSize; 366 long newSize = estimateEmptyObjectSize(); 367 do { 369 oldSize = newSize; 371 newSize = estimateEmptyObjectSize(); 372 } while (newSize != oldSize); 375 System.out.print("empty object = "); 377 System.out.println(newSize + " bytes (" + (newSize / 4) + " words)"); 378 objectSize = newSize; 381 } 382 383 388 389 private static long estimateEmptyObjectSize () { 390 long min = 0; 391 while (true) { 392 min = freeMem(min); 393 Object [] instances = new Object [NB_INSTANCES_1]; 394 long start = freeMem(0); 395 for (int i = 0; i < instances.length; ++i) { 396 instances[i] = new C(); 397 } 398 long freeMem = freeMem(0); 399 for (int i = 0; i < instances.length; ++i) { 400 instances[i] = null; 401 } 402 long end = freeMem(freeMem); 403 408 if (Math.abs(end - start) < NB_INSTANCES_1) { 409 return (end - freeMem) / NB_INSTANCES_1; 410 } 411 } 412 } 413 414 420 421 private static void computeEmptyComponentSize (Factory tmpl) 422 throws Exception 423 { 424 long oldSize; 426 long newSize = estimateEmptyComponentSize(tmpl); 427 do { 429 oldSize = newSize; 431 newSize = estimateEmptyComponentSize(tmpl); 432 } while (newSize != oldSize); 435 System.out.print("empty component = "); 437 System.out.print(newSize + " bytes (" + (newSize / 4) + " words)"); 438 System.out.println(" => + " + (newSize - objectSize) / 4 + " words"); 439 } 440 441 448 449 private static long estimateEmptyComponentSize (Factory tmpl) 450 throws Exception 451 { 452 long min = 0; 453 while (true) { 454 min = freeMem(min); 455 Object [] instances = new Object [NB_INSTANCES_1]; 456 long start = freeMem(0); 457 for (int i = 0; i < instances.length; ++i) { 458 instances[i] = tmpl.newFcInstance(); 459 } 460 long freeMem = freeMem(0); 461 for (int i = 0; i < instances.length; ++i) { 462 instances[i] = null; 463 } 464 long end = freeMem(freeMem); 465 470 if (Math.abs(end - start) < NB_INSTANCES_1) { 471 return (end - freeMem) / NB_INSTANCES_1; 472 } 473 } 474 } 475 476 482 483 private static void computeComponentTypeSize (ComponentType type) 484 throws Exception 485 { 486 long oldSize; 488 long newSize = estimateComponentTypeSize(type); 489 do { 491 oldSize = newSize; 493 newSize = estimateComponentTypeSize(type); 494 } while (newSize != oldSize); 497 int n = type.getFcInterfaceTypes().length; 499 System.out.print("component type (" + n + " interfaces) = "); 500 System.out.println(newSize + " bytes (" + (newSize / 4) + " words)"); 501 } 502 503 510 511 private static long estimateComponentTypeSize (ComponentType type) 512 throws Exception 513 { 514 TypeFactory tf = Fractal.getTypeFactory(Fractal.getBootstrapComponent()); 515 long min = 0; 516 while (true) { 517 min = freeMem(min); 518 Object [] instances = new Object [NB_INSTANCES_1]; 519 long start = freeMem(0); 520 for (int i = 0; i < instances.length; ++i) { 521 instances[i] = clone(tf, type); 522 } 523 long freeMem = freeMem(0); 524 for (int i = 0; i < instances.length; ++i) { 525 instances[i] = null; 526 } 527 long end = freeMem(freeMem); 528 533 if (Math.abs(end - start) < NB_INSTANCES_1) { 534 return (end - freeMem) / NB_INSTANCES_1; 535 } 536 } 537 } 538 539 545 546 private static long freeMem (long min) { 547 Runtime rt = Runtime.getRuntime(); 548 rt.gc(); 550 long oldFreeMem; 551 long newFreeMem = rt.freeMemory(); 552 while (true) { 553 rt.gc(); 555 oldFreeMem = newFreeMem; 556 newFreeMem = rt.freeMemory(); 557 if (newFreeMem == oldFreeMem && newFreeMem >= min) { 559 return newFreeMem; 560 } 561 } 562 } 563 564 568 574 575 private static String format (final double d) { 576 return Float.toString((float)d); 577 } 578 579 587 588 private static ComponentType clone ( 589 final TypeFactory tf, 590 final ComponentType type) throws Exception 591 { 592 InterfaceType[] itfTypes = type.getFcInterfaceTypes(); 593 InterfaceType[] cloneItfTypes = new InterfaceType[itfTypes.length]; 594 for (int i = 0; i < itfTypes.length; ++i) { 595 InterfaceType itfType = itfTypes[i]; 596 cloneItfTypes[i] = tf.createFcItfType( 597 new String (itfType.getFcItfName().toCharArray()), 598 new String (itfType.getFcItfSignature().toCharArray()), 599 itfType.isFcClientItf(), 600 itfType.isFcOptionalItf(), 601 itfType.isFcCollectionItf()); 602 } 603 return tf.createFcType(cloneItfTypes); 604 } 605 } 606 | Popular Tags |