Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hibernate/test/stats/StatsTest.java


1   //$Id: StatsTest.java 9210 2006-02-03 22:15:19Z steveebersole $
2   package org.hibernate.test.stats;
3   
4   import java.util.HashSet;
5   import java.util.Iterator;
6   
7   import junit.framework.Test;
8   import junit.framework.TestSuite;
9   import org.hibernate.FetchMode;
10  import org.hibernate.Session;
11  import org.hibernate.SessionFactory;
12  import org.hibernate.Transaction;
13  import org.hibernate.Hibernate;
14  import org.hibernate.ScrollableResults;
15  import org.hibernate.cfg.Configuration;
16  import org.hibernate.cfg.Environment;
17  import org.hibernate.mapping.Collection;
18  import org.hibernate.stat.Statistics;
19  import org.hibernate.stat.QueryStatistics;
20  import org.hibernate.test.TestCase;
21  
22  /**
23   * Show the difference between fetch and load
24   *
25   * @author Emmanuel Bernard
26   */
27  public class StatsTest extends TestCase {
28    public void testCollectionFetchVsLoad() throws Exception {
29      Statistics stats = getSessions().getStatistics();
30      stats.clear();
31  
32      Session s = openSession();
33      Transaction tx = s.beginTransaction();
34      Continent europe = fillDb(s);
35      tx.commit();
36      s.clear();
37  
38      tx = s.beginTransaction();
39      assertEquals(0, stats.getCollectionLoadCount() );
40      assertEquals(0,  stats.getCollectionFetchCount() );
41      Continent europe2 = (Continent) s.get( Continent.class, europe.getId() );
42      assertEquals("Lazy true: no collection should be loaded", 0, stats.getCollectionLoadCount() );
43      assertEquals( 0, stats.getCollectionFetchCount() );
44      europe2.getCountries().size();
45      assertEquals( 1, stats.getCollectionLoadCount() );
46      assertEquals("Explicit fetch of the collection state", 1, stats.getCollectionFetchCount() );
47      tx.commit();
48      s.close();
49  
50      s = openSession();
51      tx = s.beginTransaction();
52      stats.clear();
53      europe = fillDb(s);
54      tx.commit();
55      s.clear();
56      tx = s.beginTransaction();
57      assertEquals( 0, stats.getCollectionLoadCount() );
58      assertEquals( 0, stats.getCollectionFetchCount() );
59      europe2 = (Continent) s.createQuery(
60          "from " + Continent.class.getName() + " a join fetch a.countries where a.id = " + europe.getId()
61        ).uniqueResult();
62      assertEquals( 1, stats.getCollectionLoadCount() );
63      assertEquals( "collection should be loaded in the same query as its parent", 0, stats.getCollectionFetchCount() );
64      tx.commit();
65      s.close();
66  
67      Collection coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
68      coll.setFetchMode(FetchMode.JOIN);
69      coll.setLazy(false);
70      SessionFactory sf = getCfg().buildSessionFactory();
71      stats = sf.getStatistics();
72      stats.clear();
73      stats.setStatisticsEnabled(true);
74      s = sf.openSession();
75      tx = s.beginTransaction();
76      europe = fillDb(s);
77      tx.commit();
78      s.clear();
79      tx = s.beginTransaction();
80      assertEquals( 0, stats.getCollectionLoadCount() );
81      assertEquals( 0, stats.getCollectionFetchCount() );
82      europe2 = (Continent) s.get( Continent.class, europe.getId() );
83      assertEquals( 1, stats.getCollectionLoadCount() );
84      assertEquals( "Should do direct load, not indirect second load when lazy false and JOIN", 0, stats.getCollectionFetchCount() );
85      tx.commit();
86      s.close();
87      sf.close();
88  
89      coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
90      coll.setFetchMode(FetchMode.SELECT);
91      coll.setLazy(false);
92      sf = getCfg().buildSessionFactory();
93      stats = sf.getStatistics();
94      stats.clear();
95      stats.setStatisticsEnabled(true);
96      s = sf.openSession();
97      tx = s.beginTransaction();
98      europe = fillDb(s);
99      tx.commit();
100     s.clear();
101     tx = s.beginTransaction();
102     assertEquals( 0, stats.getCollectionLoadCount() );
103     assertEquals( 0, stats.getCollectionFetchCount() );
104     europe2 = (Continent) s.get( Continent.class, europe.getId() );
105     assertEquals( 1, stats.getCollectionLoadCount() );
106     assertEquals( "Should do explicit collection load, not part of the first one", 1, stats.getCollectionFetchCount() );
107     Iterator countries = europe2.getCountries().iterator();
108     while ( countries.hasNext() ) {
109       s.delete( countries.next() );
110     }
111     cleanDb( s );
112     tx.commit();
113     s.close();
114   }
115 
116   public void testQueryStatGathering() {
117     Statistics stats = getSessions().getStatistics();
118     stats.clear();
119 
120     Session s = openSession();
121     Transaction tx = s.beginTransaction();
122     fillDb(s);
123     tx.commit();
124     s.close();
125 
126     s = openSession();
127     tx = s.beginTransaction();
128     final String continents = "from Continent";
129     int results = s.createQuery( continents ).list().size();
130     QueryStatistics continentStats = stats.getQueryStatistics( continents );
131     assertNotNull( "stats were null",  continentStats );
132     assertEquals( "unexpected execution count", 1, continentStats.getExecutionCount() );
133     assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() );
134     long maxTime = continentStats.getExecutionMaxTime();
135     assertEquals( maxTime, stats.getQueryExecutionMaxTime() );
136 //    assertEquals( continents, stats.getQueryExecutionMaxTimeQueryString() );
137 
138     Iterator itr = s.createQuery( continents ).iterate();
139     // iterate() should increment the execution count
140     assertEquals( "unexpected execution count", 2, continentStats.getExecutionCount() );
141     // but should not effect the cumulative row count
142     assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() );
143     Hibernate.close( itr );
144 
145     ScrollableResults scrollableResults = s.createQuery( continents ).scroll();
146     // same deal with scroll()...
147     assertEquals( "unexpected execution count", 3, continentStats.getExecutionCount() );
148     assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() );
149     scrollableResults.close();
150     tx.commit();
151     s.close();
152 
153     // explicitly check that statistics for "split queries" get collected
154     // under the original query
155     stats.clear();
156     s = openSession();
157     tx = s.beginTransaction();
158     final String localities = "from Locality";
159     results = s.createQuery( localities ).list().size();
160     QueryStatistics localityStats = stats.getQueryStatistics( localities );
161     assertNotNull( "stats were null",  localityStats );
162     // ...one for each split query
163     assertEquals( "unexpected execution count", 2, localityStats.getExecutionCount() );
164     assertEquals( "unexpected row count", results, localityStats.getExecutionRowCount() );
165     maxTime = localityStats.getExecutionMaxTime();
166     assertEquals( maxTime, stats.getQueryExecutionMaxTime() );
167 //    assertEquals( localities, stats.getQueryExecutionMaxTimeQueryString() );
168     tx.commit();
169     s.close();
170     assertFalse( s.isOpen() );
171 
172     // native sql queries
173     stats.clear();
174     s = openSession();
175     tx = s.beginTransaction();
176     final String sql = "select id, name from Country";
177     results = s.createSQLQuery( sql ).addEntity( Country.class ).list().size();
178     QueryStatistics sqlStats = stats.getQueryStatistics( sql );
179     assertNotNull( "sql stats were null", sqlStats );
180     assertEquals( "unexpected execution count", 1, sqlStats.getExecutionCount() );
181     assertEquals( "unexpected row count", results, sqlStats.getExecutionRowCount() );
182     maxTime = sqlStats.getExecutionMaxTime();
183     assertEquals( maxTime, stats.getQueryExecutionMaxTime() );
184 //    assertEquals( sql, stats.getQueryExecutionMaxTimeQueryString() );
185     tx.commit();
186     s.close();
187 
188     s = openSession();
189     tx = s.beginTransaction();
190     cleanDb( s );
191     tx.commit();
192     s.close();
193   }
194 
195   private Continent fillDb(Session s) {
196     Continent europe = new Continent();
197     europe.setName("Europe");
198     Country france = new Country();
199     france.setName("France");
200     europe.setCountries( new HashSet() );
201     europe.getCountries().add(france);
202     s.persist(france);
203     s.persist(europe);
204     return europe;
205   }
206 
207   private void cleanDb(Session s) {
208     s.createQuery( "delete Locality" ).executeUpdate();
209     s.createQuery( "delete Country" ).executeUpdate();
210     s.createQuery( "delete Continent" ).executeUpdate();
211   }
212 
213   protected void configure(Configuration cfg) {
214     super.configure( cfg );
215     cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
216   }
217 
218   protected String[] getMappings() {
219     return new String[] { "stats/Continent.hbm.xml" };
220   }
221 
222   public StatsTest(String x) {
223     super(x);
224   }
225 
226   public static Test suite() {
227     return new TestSuite(StatsTest.class);
228   }
229 }