Created
April 8, 2016 15:54
-
-
Save ctoestreich/4a8f649c6984d95d25d0b2c8149cfc29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.bsb.interceptor | |
| import grails.artefact.Interceptor | |
| import grails.util.Holders | |
| import groovy.transform.CompileStatic | |
| import org.hibernate.SessionFactory | |
| import org.hibernate.stat.Statistics | |
| @CompileStatic | |
| class HibernateInterceptor implements Interceptor { | |
| SessionFactory sessionFactory | |
| int order = HIGHEST_PRECEDENCE | |
| private static final String START_TIME = 'Hibernate_Start_Time' | |
| private static final boolean hibernateFilterEnabled = Boolean.parseBoolean(Holders.config.getProperty("hibernate.filter.enabled")?.toString() ?: "false") | |
| HibernateInterceptor() { | |
| matchAll() | |
| .excludes(controller: 'apiDocs') | |
| .excludes(controller: /.*MobileApi$/) | |
| .excludes(controller: 'jaxrs') | |
| .excludes(controller: 'assets') | |
| } | |
| boolean before() { | |
| if (!hibernateFilterEnabled) { | |
| return true | |
| } | |
| request[START_TIME] = System.currentTimeMillis() | |
| Statistics stats = sessionFactory.statistics | |
| log.info "\n### In action: $controllerName/$actionName ###" | |
| if (!stats.statisticsEnabled) { | |
| stats.statisticsEnabled = true | |
| } | |
| true | |
| } | |
| boolean after() { return true } | |
| void afterView() { | |
| if (!hibernateFilterEnabled) { | |
| return | |
| } | |
| Long end = System.currentTimeMillis() | |
| Long start = request[START_TIME] as Long | |
| log.info "Total time: ${end - start} ms" | |
| Statistics stats = sessionFactory.getStatistics() | |
| double queryCacheHitCount = stats.getQueryCacheHitCount(); | |
| double queryCacheMissCount = stats.getQueryCacheMissCount(); | |
| double queryCacheHitRatio = (queryCacheHitCount / ((queryCacheHitCount + queryCacheMissCount) ?: 1)) | |
| log.info """ | |
| ######################## Hibernate Stats ############################################## | |
| In action: $controllerName/$actionName | |
| Transaction Count:${stats.transactionCount} | |
| Flush Count:${stats.flushCount} | |
| Total Collections Fetched:${stats.collectionFetchCount} | |
| Total Collections Loaded:${stats.collectionLoadCount} | |
| Total Entities Fetched:${stats.entityFetchCount} | |
| Total Entities Loaded:${stats.entityFetchCount} | |
| Total Queries:${stats.queryExecutionCount} | |
| queryCacheHitCount:${queryCacheHitCount} | |
| queryCacheMissCount:${queryCacheMissCount} | |
| queryCacheHitRatio:${queryCacheHitRatio} | |
| ######################## Hibernate Stats ############################################## | |
| """ | |
| stats.clear() // We assume no one else is using stats | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment