private static Continent FillDb(ISession s)
		{
			Continent europe = new Continent();
			europe.Name="Europe";
			Country france = new Country();
			france.Name="France";
			europe.Countries=new IESI.HashedSet<Country>();
			europe.Countries.Add(france);
			s.Save(france);
			s.Save(europe);
			return europe;
		}
        public async Task SessionStatisticsAsync()
        {
            ISession     s     = OpenSession();
            ITransaction tx    = s.BeginTransaction();
            IStatistics  stats = Sfi.Statistics;

            stats.Clear();
            bool isStats = stats.IsStatisticsEnabled;

            stats.IsStatisticsEnabled = true;
            Continent europe = await(FillDbAsync(s));

            await(tx.CommitAsync());
            s.Clear();
            tx = s.BeginTransaction();
            ISessionStatistics sessionStats = s.Statistics;

            Assert.AreEqual(0, sessionStats.EntityKeys.Count);
            Assert.AreEqual(0, sessionStats.EntityCount);
            Assert.AreEqual(0, sessionStats.CollectionKeys.Count);
            Assert.AreEqual(0, sessionStats.CollectionCount);

            europe = await(s.GetAsync <Continent>(europe.Id));
            await(NHibernateUtil.InitializeAsync(europe.Countries));
            IEnumerator itr = europe.Countries.GetEnumerator();

            itr.MoveNext();
            await(NHibernateUtil.InitializeAsync(itr.Current));
            Assert.AreEqual(2, sessionStats.EntityKeys.Count);
            Assert.AreEqual(2, sessionStats.EntityCount);
            Assert.AreEqual(1, sessionStats.CollectionKeys.Count);
            Assert.AreEqual(1, sessionStats.CollectionCount);

            await(CleanDbAsync(s));
            await(tx.CommitAsync());
            s.Close();

            stats.IsStatisticsEnabled = isStats;
        }
        public void SessionStatistics()
        {
            ISession     s     = OpenSession();
            ITransaction tx    = s.BeginTransaction();
            IStatistics  stats = Sfi.Statistics;

            stats.Clear();
            bool isStats = stats.IsStatisticsEnabled;

            stats.IsStatisticsEnabled = true;
            Continent europe = FillDb(s);

            tx.Commit();
            s.Clear();
            tx = s.BeginTransaction();
            ISessionStatistics sessionStats = s.Statistics;

            Assert.AreEqual(0, sessionStats.EntityKeys.Count);
            Assert.AreEqual(0, sessionStats.EntityCount);
            Assert.AreEqual(0, sessionStats.CollectionKeys.Count);
            Assert.AreEqual(0, sessionStats.CollectionCount);

            europe = s.Get <Continent>(europe.Id);
            NHibernateUtil.Initialize(europe.Countries);
            IEnumerator itr = europe.Countries.GetEnumerator();

            itr.MoveNext();
            NHibernateUtil.Initialize(itr.Current);
            Assert.AreEqual(2, sessionStats.EntityKeys.Count);
            Assert.AreEqual(2, sessionStats.EntityCount);
            Assert.AreEqual(1, sessionStats.CollectionKeys.Count);
            Assert.AreEqual(1, sessionStats.CollectionCount);

            CleanDb(s);
            tx.Commit();
            s.Close();

            stats.IsStatisticsEnabled = isStats;
        }
        public void CollectionFetchVsLoad()
        {
            IStatistics stats = Sfi.Statistics;

            stats.Clear();

            ISession     s      = OpenSession();
            ITransaction tx     = s.BeginTransaction();
            Continent    europe = FillDb(s);

            tx.Commit();
            s.Clear();

            tx = s.BeginTransaction();
            Assert.AreEqual(0, stats.CollectionLoadCount);
            Assert.AreEqual(0, stats.CollectionFetchCount);
            Continent europe2 = s.Get <Continent>(europe.Id);

            Assert.AreEqual(0, stats.CollectionLoadCount, "Lazy true: no collection should be loaded");
            Assert.AreEqual(0, stats.CollectionFetchCount);

            int cc = europe2.Countries.Count;

            Assert.AreEqual(1, stats.CollectionLoadCount);
            Assert.AreEqual(1, stats.CollectionFetchCount, "Explicit fetch of the collection state");
            tx.Commit();
            s.Close();

            s  = OpenSession();
            tx = s.BeginTransaction();
            stats.Clear();
            europe = FillDb(s);
            tx.Commit();
            s.Clear();
            tx = s.BeginTransaction();
            Assert.AreEqual(0, stats.CollectionLoadCount);
            Assert.AreEqual(0, stats.CollectionFetchCount);

            europe2 = s.CreateQuery("from Continent a join fetch a.Countries where a.id = " + europe.Id).UniqueResult <Continent>();
            Assert.AreEqual(1, stats.CollectionLoadCount);
            Assert.AreEqual(0, stats.CollectionFetchCount, "collection should be loaded in the same query as its parent");
            tx.Commit();
            s.Close();

            Mapping.Collection coll = cfg.GetCollectionMapping("NHibernate.Test.Stats.Continent.Countries");
            coll.FetchMode = FetchMode.Join;
            coll.IsLazy    = false;
            ISessionFactory sf = cfg.BuildSessionFactory();

            stats = sf.Statistics;
            stats.Clear();
            stats.IsStatisticsEnabled = true;
            s      = sf.OpenSession();
            tx     = s.BeginTransaction();
            europe = FillDb(s);
            tx.Commit();
            s.Clear();
            tx = s.BeginTransaction();
            Assert.AreEqual(0, stats.CollectionLoadCount);
            Assert.AreEqual(0, stats.CollectionFetchCount);

            europe2 = s.Get <Continent>(europe.Id);
            Assert.AreEqual(1, stats.CollectionLoadCount);
            Assert.AreEqual(0, stats.CollectionFetchCount,
                            "Should do direct load, not indirect second load when lazy false and JOIN");
            tx.Commit();
            s.Close();
            sf.Close();

            coll           = cfg.GetCollectionMapping("NHibernate.Test.Stats.Continent.Countries");
            coll.FetchMode = FetchMode.Select;
            coll.IsLazy    = false;
            sf             = cfg.BuildSessionFactory();
            stats          = sf.Statistics;
            stats.Clear();
            stats.IsStatisticsEnabled = true;
            s      = sf.OpenSession();
            tx     = s.BeginTransaction();
            europe = FillDb(s);
            tx.Commit();
            s.Clear();
            tx = s.BeginTransaction();
            Assert.AreEqual(0, stats.CollectionLoadCount);
            Assert.AreEqual(0, stats.CollectionFetchCount);
            europe2 = s.Get <Continent>(europe.Id);
            Assert.AreEqual(1, stats.CollectionLoadCount);
            Assert.AreEqual(1, stats.CollectionFetchCount, "Should do explicit collection load, not part of the first one");
            foreach (Country country in europe2.Countries)
            {
                s.Delete(country);
            }
            CleanDb(s);
            tx.Commit();
            s.Close();
        }