1 package org.hibernate.stat; 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.hibernate.cache.Cache; 9 import org.hibernate.engine.SessionFactoryImplementor; 10 import org.hibernate.util.ArrayHelper; 11 12 17 public class StatisticsImpl implements Statistics, StatisticsImplementor { 18 19 21 private static final Log log = LogFactory.getLog(StatisticsImpl.class); 22 23 private SessionFactoryImplementor sessionFactory; 24 25 private boolean isStatisticsEnabled; 26 private long startTime; 27 private long sessionOpenCount; 28 private long sessionCloseCount; 29 private long flushCount; 30 private long connectCount; 31 32 private long prepareStatementCount; 33 private long closeStatementCount; 34 35 private long entityLoadCount; 36 private long entityUpdateCount; 37 private long entityInsertCount; 38 private long entityDeleteCount; 39 private long entityFetchCount; 40 private long collectionLoadCount; 41 private long collectionUpdateCount; 42 private long collectionRemoveCount; 43 private long collectionRecreateCount; 44 private long collectionFetchCount; 45 46 private long secondLevelCacheHitCount; 47 private long secondLevelCacheMissCount; 48 private long secondLevelCachePutCount; 49 50 private long queryExecutionCount; 51 private long queryExecutionMaxTime; 52 private long queryCacheHitCount; 53 private long queryCacheMissCount; 54 private long queryCachePutCount; 55 56 private long commitedTransactionCount; 57 private long transactionCount; 58 59 60 private final Map secondLevelCacheStatistics = new HashMap (); 61 62 private final Map entityStatistics = new HashMap (); 63 64 private final Map collectionStatistics = new HashMap (); 65 66 private final Map queryStatistics = new HashMap (); 67 68 public StatisticsImpl() { 69 clear(); 70 } 71 72 public StatisticsImpl(SessionFactoryImplementor sessionFactory) { 73 clear(); 74 this.sessionFactory = sessionFactory; 75 } 76 77 80 public synchronized void clear() { 81 secondLevelCacheHitCount = 0; 82 secondLevelCacheMissCount = 0; 83 secondLevelCachePutCount = 0; 84 85 sessionCloseCount = 0; 86 sessionOpenCount = 0; 87 flushCount = 0; 88 connectCount = 0; 89 90 prepareStatementCount = 0; 91 closeStatementCount = 0; 92 93 entityDeleteCount = 0; 94 entityInsertCount = 0; 95 entityUpdateCount = 0; 96 entityLoadCount = 0; 97 entityFetchCount = 0; 98 99 collectionRemoveCount = 0; 100 collectionUpdateCount = 0; 101 collectionRecreateCount = 0; 102 collectionLoadCount = 0; 103 collectionFetchCount = 0; 104 105 queryExecutionCount = 0; 106 queryCacheHitCount = 0; 107 queryExecutionMaxTime = 0; 108 queryCacheMissCount = 0; 109 queryCachePutCount = 0; 110 111 secondLevelCacheStatistics.clear(); 112 entityStatistics.clear(); 113 collectionStatistics.clear(); 114 queryStatistics.clear(); 115 116 startTime = System.currentTimeMillis(); 117 } 118 119 public synchronized void openSession() { 120 sessionOpenCount++; 121 } 122 123 public synchronized void closeSession() { 124 sessionCloseCount++; 125 } 126 127 public synchronized void flush() { 128 flushCount++; 129 } 130 131 public synchronized void connect() { 132 connectCount++; 133 } 134 135 public synchronized void loadEntity(String entityName) { 136 entityLoadCount++; 137 getEntityStatistics(entityName).loadCount++; 138 } 139 140 public synchronized void fetchEntity(String entityName) { 141 entityFetchCount++; 142 getEntityStatistics(entityName).fetchCount++; 143 } 144 145 151 public synchronized EntityStatistics getEntityStatistics(String entityName) { 152 EntityStatistics es = (EntityStatistics) entityStatistics.get(entityName); 153 if (es==null) { 154 es = new EntityStatistics(entityName); 155 entityStatistics.put(entityName, es); 156 } 157 return es; 158 } 159 160 public synchronized void updateEntity(String entityName) { 161 entityUpdateCount++; 162 EntityStatistics es = getEntityStatistics(entityName); 163 es.updateCount++; 164 } 165 166 public synchronized void insertEntity(String entityName) { 167 entityInsertCount++; 168 EntityStatistics es = getEntityStatistics(entityName); 169 es.insertCount++; 170 } 171 172 public synchronized void deleteEntity(String entityName) { 173 entityDeleteCount++; 174 EntityStatistics es = getEntityStatistics(entityName); 175 es.deleteCount++; 176 } 177 178 184 public synchronized CollectionStatistics getCollectionStatistics(String role) { 185 CollectionStatistics cs = (CollectionStatistics) collectionStatistics.get(role); 186 if (cs==null) { 187 cs = new CollectionStatistics(role); 188 collectionStatistics.put(role, cs); 189 } 190 return cs; 191 } 192 193 public synchronized void loadCollection(String role) { 194 collectionLoadCount++; 195 getCollectionStatistics(role).loadCount++; 196 } 197 198 public synchronized void fetchCollection(String role) { 199 collectionFetchCount++; 200 getCollectionStatistics(role).fetchCount++; 201 } 202 203 public synchronized void updateCollection(String role) { 204 collectionUpdateCount++; 205 getCollectionStatistics(role).updateCount++; 206 } 207 208 public synchronized void recreateCollection(String role) { 209 collectionRecreateCount++; 210 getCollectionStatistics(role).recreateCount++; 211 } 212 213 public synchronized void removeCollection(String role) { 214 collectionRemoveCount++; 215 getCollectionStatistics(role).removeCount++; 216 } 217 218 224 public synchronized SecondLevelCacheStatistics getSecondLevelCacheStatistics(String regionName) { 225 SecondLevelCacheStatistics slcs = (SecondLevelCacheStatistics) secondLevelCacheStatistics.get(regionName); 226 if (slcs==null) { 227 if (sessionFactory == null) return null; 228 Cache cache = sessionFactory.getSecondLevelCacheRegion(regionName); 229 if (cache==null) return null; 230 slcs = new SecondLevelCacheStatistics(cache); 231 secondLevelCacheStatistics.put(regionName, slcs); 232 } 233 return slcs; 234 } 235 236 public synchronized void secondLevelCachePut(String regionName) { 237 secondLevelCachePutCount++; 238 getSecondLevelCacheStatistics(regionName).putCount++; 239 } 240 241 public synchronized void secondLevelCacheHit(String regionName) { 242 secondLevelCacheHitCount++; 243 getSecondLevelCacheStatistics(regionName).hitCount++; 244 } 245 246 public synchronized void secondLevelCacheMiss(String regionName) { 247 secondLevelCacheMissCount++; 248 getSecondLevelCacheStatistics(regionName).missCount++; 249 } 250 251 public synchronized void queryExecuted(String hql, int rows, long time) { 252 queryExecutionCount++; 253 if (queryExecutionMaxTime<time) queryExecutionMaxTime=time; 254 if (hql!=null) { 255 QueryStatistics qs = getQueryStatistics(hql); 256 qs.executed(rows, time); 257 } 258 } 259 260 public synchronized void queryCacheHit(String hql, String regionName) { 261 queryCacheHitCount++; 262 if (hql!=null) { 263 QueryStatistics qs = getQueryStatistics(hql); 264 qs.cacheHitCount++; 265 } 266 SecondLevelCacheStatistics slcs = getSecondLevelCacheStatistics(regionName); 267 slcs.hitCount++; 268 } 269 270 public synchronized void queryCacheMiss(String hql, String regionName) { 271 queryCacheMissCount++; 272 if (hql!=null) { 273 QueryStatistics qs = getQueryStatistics(hql); 274 qs.cacheMissCount++; 275 } 276 SecondLevelCacheStatistics slcs = getSecondLevelCacheStatistics(regionName); 277 slcs.missCount++; 278 } 279 280 public synchronized void queryCachePut(String hql, String regionName) { 281 queryCachePutCount++; 282 if (hql!=null) { 283 QueryStatistics qs = getQueryStatistics(hql); 284 qs.cachePutCount++; 285 } 286 SecondLevelCacheStatistics slcs = getSecondLevelCacheStatistics(regionName); 287 slcs.putCount++; 288 } 289 290 296 public synchronized QueryStatistics getQueryStatistics(String queryString) { 297 QueryStatistics qs = (QueryStatistics) queryStatistics.get(queryString); 298 if (qs==null) { 299 qs = new QueryStatistics(queryString); 300 queryStatistics.put(queryString, qs); 301 } 302 return qs; 303 } 304 305 308 public long getEntityDeleteCount() { 309 return entityDeleteCount; 310 } 311 312 315 public long getEntityInsertCount() { 316 return entityInsertCount; 317 } 318 319 322 public long getEntityLoadCount() { 323 return entityLoadCount; 324 } 325 326 329 public long getEntityFetchCount() { 330 return entityFetchCount; 331 } 332 333 336 public long getEntityUpdateCount() { 337 return entityUpdateCount; 338 } 339 340 public long getQueryExecutionCount() { 341 return queryExecutionCount; 342 } 343 344 public long getQueryCacheHitCount() { 345 return queryCacheHitCount; 346 } 347 348 public long getQueryCacheMissCount() { 349 return queryCacheMissCount; 350 } 351 352 public long getQueryCachePutCount() { 353 return queryCachePutCount; 354 } 355 356 359 public long getFlushCount() { 360 return flushCount; 361 } 362 363 366 public long getConnectCount() { 367 return connectCount; 368 } 369 370 373 public long getSecondLevelCacheHitCount() { 374 return secondLevelCacheHitCount; 375 } 376 377 380 public long getSecondLevelCacheMissCount() { 381 return secondLevelCacheMissCount; 382 } 383 384 387 public long getSecondLevelCachePutCount() { 388 return secondLevelCachePutCount; 389 } 390 391 394 public long getSessionCloseCount() { 395 return sessionCloseCount; 396 } 397 398 401 public long getSessionOpenCount() { 402 return sessionOpenCount; 403 } 404 405 408 public long getCollectionLoadCount() { 409 return collectionLoadCount; 410 } 411 412 415 public long getCollectionFetchCount() { 416 return collectionFetchCount; 417 } 418 419 422 public long getCollectionUpdateCount() { 423 return collectionUpdateCount; 424 } 425 426 430 public long getCollectionRemoveCount() { 431 return collectionRemoveCount; 432 } 433 436 public long getCollectionRecreateCount() { 437 return collectionRecreateCount; 438 } 439 440 443 public long getStartTime() { 444 return startTime; 445 } 446 447 450 public void logSummary() { 451 log.info("Logging statistics...."); 452 log.info("start time: " + startTime); 453 log.info("sessions opened: " + sessionOpenCount); 454 log.info("sessions closed: " + sessionCloseCount); 455 log.info("transactions: " + transactionCount); 456 log.info("successful transactions: " + commitedTransactionCount); 457 log.info("flushes: " + flushCount); 458 log.info("connections obtained: " + connectCount); 459 log.info("statements prepared: " + prepareStatementCount); 460 log.info("statements closed: " + closeStatementCount); 461 log.info("second level cache puts: " + secondLevelCachePutCount); 462 log.info("second level cache hits: " + secondLevelCacheHitCount); 463 log.info("second level cache misses: " + secondLevelCacheMissCount); 464 log.info("entities loaded: " + entityLoadCount); 465 log.info("entities updated: " + entityUpdateCount); 466 log.info("entities inserted: " + entityInsertCount); 467 log.info("entities deleted: " + entityDeleteCount); 468 log.info("entities fetched (minimize this): " + entityFetchCount); 469 log.info("collections loaded: " + collectionLoadCount); 470 log.info("collections updated: " + collectionUpdateCount); 471 log.info("collections removed: " + collectionRemoveCount); 472 log.info("collections recreated: " + collectionRecreateCount); 473 log.info("collections fetched (minimize this): " + collectionFetchCount); 474 log.info("queries executed to database: " + queryExecutionCount); 475 log.info("query cache puts: " + queryCachePutCount); 476 log.info("query cache hits: " + queryCacheHitCount); 477 log.info("query cache misses: " + queryCacheMissCount); 478 log.info("max query time: " + queryExecutionMaxTime + "ms"); 479 } 480 481 484 public boolean isStatisticsEnabled() { 485 return isStatisticsEnabled; 486 } 487 488 491 public void setStatisticsEnabled(boolean b) { 492 isStatisticsEnabled = b; 493 } 494 495 499 public long getQueryExecutionMaxTime() { 500 return queryExecutionMaxTime; 501 } 502 503 506 public String [] getQueries() { 507 return ArrayHelper.toStringArray( queryStatistics.keySet() ); 508 } 509 510 513 public String [] getEntityNames() { 514 if (sessionFactory==null) { 515 return ArrayHelper.toStringArray( entityStatistics.keySet() ); 516 } 517 else { 518 return ArrayHelper.toStringArray( sessionFactory.getAllClassMetadata().keySet() ); 519 } 520 } 521 522 525 public String [] getCollectionRoleNames() { 526 if (sessionFactory==null) { 527 return ArrayHelper.toStringArray( collectionStatistics.keySet() ); 528 } 529 else { 530 return ArrayHelper.toStringArray( sessionFactory.getAllCollectionMetadata().keySet() ); 531 } 532 } 533 534 537 public String [] getSecondLevelCacheRegionNames() { 538 if (sessionFactory==null) { 539 return ArrayHelper.toStringArray( secondLevelCacheStatistics.keySet() ); 540 } 541 else { 542 return ArrayHelper.toStringArray( sessionFactory.getAllSecondLevelCacheRegions().keySet() ); 543 } 544 } 545 546 public void endTransaction(boolean success) { 547 transactionCount++; 548 if (success) commitedTransactionCount++; 549 } 550 551 public long getSuccessfulTransactionCount() { 552 return commitedTransactionCount; 553 } 554 555 public long getTransactionCount() { 556 return transactionCount; 557 } 558 559 public void closeStatement() { 560 closeStatementCount++; 561 } 562 563 public void prepareStatement() { 564 prepareStatementCount++; 565 } 566 567 public long getCloseStatementCount() { 568 return closeStatementCount; 569 } 570 571 public long getPrepareStatementCount() { 572 return prepareStatementCount; 573 } 574 575 public String toString() { 576 return new StringBuffer () 577 .append("Statistics[") 578 .append("start time=").append(startTime) 579 .append(",sessions opened=").append(sessionOpenCount) 580 .append(",sessions closed=").append(sessionCloseCount) 581 .append(",transactions=").append(transactionCount) 582 .append(",successful transactions=").append(commitedTransactionCount) 583 .append(",flushes=").append(flushCount) 584 .append(",connections obtained=").append(connectCount) 585 .append(",statements prepared=").append(prepareStatementCount) 586 .append(",statements closed=").append(closeStatementCount) 587 .append(",second level cache puts=").append(secondLevelCachePutCount) 588 .append(",second level cache hits=").append(secondLevelCacheHitCount) 589 .append(",second level cache misses=").append(secondLevelCacheMissCount) 590 .append(",entities loaded=").append(entityLoadCount) 591 .append(",entities updated=").append(entityUpdateCount) 592 .append(",entities inserted=").append(entityInsertCount) 593 .append(",entities deleted=").append(entityDeleteCount) 594 .append(",entities fetched=").append(entityFetchCount) 595 .append(",collections loaded=").append(collectionLoadCount) 596 .append(",collections updated=").append(collectionUpdateCount) 597 .append(",collections removed=").append(collectionRemoveCount) 598 .append(",collections recreated=").append(collectionRecreateCount) 599 .append(",collections fetched=").append(collectionFetchCount) 600 .append(",queries executed to database=").append(queryExecutionCount) 601 .append(",query cache puts=").append(queryCachePutCount) 602 .append(",query cache hits=").append(queryCacheHitCount) 603 .append(",query cache misses=").append(queryCacheMissCount) 604 .append(",max query time=").append(queryExecutionMaxTime) 605 .append(']') 606 .toString(); 607 } 608 609 } | Popular Tags |