Benutzer-Werkzeuge

Webseiten-Werkzeuge


prog:java_hibernate_statistik

Hibernate Statistiken abfragen

In Hibernate können intern Statistiken gesammelt und ausgewertet werden.

Das kann einfach programmatisch im einen Code erfolgen (siehe Beispiel anbei) oder aber eleganter über Java Management Extension (JMX) Services.

Bzgl. JMX siehe : http://openbook.galileodesign.de/javainsel5/javainsel23_000.htm

Beispiel:

Session öffnen und Statisikerfassung einschalten:

....
 
        // read configuration and create factory for the session
        Configuration config = new Configuration().configure();
 
        // Namesgebung per Regelwerk erstellen:
        config.setNamingStrategy(new DefaultComponentSafeNamingStrategy());
 
        // Session holen
        // Vor Version 4
        //SessionFactory sessionFactory = config.buildSessionFactory();
 
        // ab Hibernate 4
        ServiceRegistry serviceRegistry =
            new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
 
        // Statistik einschalten
        Statistics stats = sessionFactory.getStatistics();
        stats.setStatisticsEnabled(true);
....       

Statistiken ausgeben:

    public void printStatistc(Statistics stats) {
        System.out.println("--------------------------Over All Statisic ---------------------------------------");
        System.out.println("-- Info :: Number of connection requestt, this is the number of times Hibernate asked for a connection");
        System.out.println("   +-- ::" + stats.getConnectCount());
        System.out.println("-- Info :: Number of flushes done on the session (either by client code or by hibernate).");
        System.out.println("   +-- ::" + stats.getFlushCount());
        System.out.println("-- Info :: The number of completed transactions (failed and successful).");
        System.out.println("   +-- ::" + stats.getTransactionCount());
        System.out.println("-- Info :: The number of transactions completed without failure");
        System.out.println("   +-- ::" + stats.getSuccessfulTransactionCount());
        System.out.println("-- Info :: The number of sessions your code has opened.");
        System.out.println("   +-- ::" + stats.getSessionOpenCount());
        System.out.println("-- Info :: The number of sessions your code has closed.");
        System.out.println("   +-- ::" + stats.getSessionCloseCount());
 
        System.out.println("-------------------- Query Statistic ---------------------------------------");
 
        System.out.println("-- Info :: All of the queries that have executed.");
 
        System.out.println("-- Info :: Total number of queries executed.");
        System.out.println("   +-- ::" + stats.getQueryExecutionCount());
        System.out.println("-- Info :: Time of the slowest query executed.");
        System.out.println("   +-- ::" + stats.getQueryExecutionMaxTime());
 
 
        String[] selects = stats.getQueries();
        System.out.println("-- Info :: All of the queries that have executed.");
        System.out.println("   +-- ::" + selects.length);
 
        QueryStatistics qstat = null;
        for (String s : selects) {
            System.out.println("   +-- :: Query :: " + s);
            qstat = stats.getQueryStatistics(s);
            System.out.println("   +-- :: Execution Time Max");
            System.out.println("   +-- :: " + qstat.getExecutionMaxTime());
            System.out.println("   +-- :: Execution Count");
            System.out.println("   +-- :: " + qstat.getExecutionCount());
            System.out.println("   +-- :: Query Cache Hit count");
            System.out.println("   +-- :: " + qstat.getCacheHitCount());
            System.out.println("   +-- :: Query Cache Miss count");
            System.out.println("   +-- :: " + qstat.getCacheMissCount());
 
        }
 
 
        System.out.println("-- Info ::----------------------------------------");
 
        System.out.println("-- Info :: the number of collections fetched from the DB.");
        System.out.println("   +-- ::" + stats.getCollectionFetchCount());
        System.out.println("-- Info :: The number of collections loaded from the DB.");
        System.out.println("   +-- ::" + stats.getCollectionLoadCount());
        System.out.println("-- Info :: The number of collections that were rebuilt");
        System.out.println("   +-- ::" + stats.getCollectionRecreateCount());
        System.out.println("-- Info :: The number of collections that were 'deleted' batch.");
        System.out.println("   +-- ::" + stats.getCollectionRemoveCount());
        System.out.println("-- Info :: The number of collections that were updated batch.");
        System.out.println("   +-- ::" + stats.getCollectionUpdateCount());
 
        System.out.println("-- Info ::----------------------------------------");
        System.out.println("-- Info :: The number of your objects deleted.");
        System.out.println("   +-- ::" + stats.getEntityDeleteCount());
        System.out.println("-- Info :: The number of your objects fetched.");
        System.out.println("   +-- ::" + stats.getEntityFetchCount());
        System.out.println("-- Info :: The number of your objects actually loaded (fully populated).");
        System.out.println("   +-- ::" + stats.getEntityLoadCount());
        System.out.println("-- Info :: The number of your objects inserted.");
        System.out.println("   +-- ::" + stats.getEntityInsertCount());
        System.out.println("-- Info :: The number of your object updated.");
        System.out.println("   +-- ::" + stats.getEntityUpdateCount());
 
        System.out.println("-- Info ::---------------Cache ----------------------");
        System.out.println("-- Info :: Cache Usage - Hit count");
        System.out.println("   +-- ::" + stats.getSecondLevelCacheHitCount());
        System.out.println("-- Info :: Cache Usage - Miss count");
        System.out.println("   +-- ::" + stats.getSecondLevelCacheMissCount());
        System.out.println("-- Info :: Query Cache Usage Hit count");
        System.out.println("   +-- ::" + stats.getQueryCacheHitCount());
        System.out.println("-- Info :: Query Cache Usage Miss count");
        System.out.println("   +-- ::" + stats.getQueryCacheMissCount());
        System.out.println("-- Info :: Query Cache Usage Put count");
        System.out.println("   +-- ::" + stats.getQueryCachePutCount());
 
        // Cache Effizents
        double queryCacheHitCount = stats.getQueryCacheHitCount();
        double queryCacheMissCount = stats.getQueryCacheMissCount();
        double queryCacheHitRatio = queryCacheHitCount / (queryCacheHitCount + queryCacheMissCount);
 
        System.out.println("-- Info :: Cache Effiziens ");
        System.out.println("   +-- ::" + queryCacheHitRatio);
 
 
        // get Statistic by region if in use
        /*
        SecondLevelCacheStatistics cacheStats = stats.getSecondLevelCacheStatistics("com.gpi.SimpleORMapper.Cache");
        cacheStats.getElementCountInMemory();
        cacheStats.getElementCountOnDisk();
        cacheStats.getEntries();
        cacheStats.getHitCount();
        cacheStats.getMissCount();
        cacheStats.getPutCount();
        cacheStats.getSizeInMemory();
        */
 
        EntityStatistics entStat = null;
        String[] entities = stats.getEntityNames();
        for (String s : entities) {
            System.out.println("-- Info ::---------------Entity in Cache  " + s + " Statistic----------------------");
 
            entStat = stats.getEntityStatistics(s);
 
            //System.out.println("   +-- ::"+entStat.toString());
 
            System.out.println("   +-- :: Query Cache Entity Insert count");
            System.out.println("   +-- :: " + entStat.getInsertCount());
            System.out.println("   +-- :: Query Cache Entity Fetch count");
            System.out.println("   +-- :: " + entStat.getFetchCount());
            System.out.println("   +-- :: Query Cache Entity Update count");
            System.out.println("   +-- :: " + entStat.getUpdateCount());
            System.out.println("   +-- :: Query Cache Entity Load count");
            System.out.println("   +-- :: " + entStat.getLoadCount());
 
 
        }
    }

Quellen

SQL Statements zählen

Java Performance Überwachung allgemein

Cookies helfen bei der Bereitstellung von Inhalten. Durch die Nutzung dieser Seiten erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Rechner gespeichert werden. Weitere Information
"Autor: Gunther Pipperr"
prog/java_hibernate_statistik.txt · Zuletzt geändert: 2014/02/10 13:42 von Gunther Pippèrr