예제 #1
0
        public void JoinedSubclassCascade()
        {
            G            g1 = new G("thing", "white", "10x10");
            F            f1 = new F("thing2", "blue");
            ISession     s  = OpenSession();
            ITransaction t  = s.BeginTransaction();

            s.Save(g1);
            s.Save(f1);
            t.Commit();
            s.Close();

            IStatistics statistics = Sfi.Statistics;

            statistics.Clear();

            s = OpenSession();
            t = s.BeginTransaction();
            IList <E> l = s.CreateQuery("from E").List <E>();

            statistics.Clear();

            s.Delete(l[0]);
            s.Delete(l[1]);
            t.Commit();
            s.Close();

            Assert.AreEqual(2, statistics.EntityDeleteCount);

            // In this case the batcher reuse the same command because have same SQL and same parametersTypes
            Assert.AreEqual(1, statistics.PrepareStatementCount);
        }
예제 #2
0
        private void OneToOneFetchTest <TPerson, TDetails>() where TPerson : Person, new() where TDetails : Details, new()
        {
            List <object> ids = this.CreatePersonAndDetails <TPerson, TDetails>();

            IStatistics statistics = Sfi.Statistics;

            // Clear the second level cache and the statistics
            Sfi.EvictEntity(typeof(TPerson).FullName);
            Sfi.EvictEntity(typeof(TDetails).FullName);
            Sfi.EvictQueries();

            statistics.Clear();

            // Fill the empty caches with data.
            this.FetchPeopleById <TPerson>(ids);

            // Verify that no data was retrieved from the cache.
            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");

            statistics.Clear();

            this.FetchPeopleById <TPerson>(ids);

            Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
        }
예제 #3
0
        private async Task OneToOneFetchTestAsync <TPerson, TDetails>(CancellationToken cancellationToken = default(CancellationToken)) where TPerson : Person, new() where TDetails : Details, new()
        {
            List <object> ids = await(this.CreatePersonAndDetailsAsync <TPerson, TDetails>(cancellationToken));

            IStatistics statistics = Sfi.Statistics;

            // Clear the second level cache and the statistics
            await(Sfi.EvictEntityAsync(typeof(TPerson).FullName, cancellationToken));
            await(Sfi.EvictEntityAsync(typeof(TDetails).FullName, cancellationToken));
            await(Sfi.EvictQueriesAsync(cancellationToken));

            statistics.Clear();

            // Fill the empty caches with data.
            await(this.FetchPeopleByIdAsync <TPerson>(ids, cancellationToken));

            // Verify that no data was retrieved from the cache.
            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");

            statistics.Clear();

            await(this.FetchPeopleByIdAsync <TPerson>(ids, cancellationToken));

            Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
        }
예제 #4
0
        public void IncrementQueryExecutionCount_WhenExplicitQueryIsExecuted()
        {
            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    FillDb(s);
                    tx.Commit();
                }

            IStatistics stats = Sfi.Statistics;

            stats.Clear();
            using (ISession s = OpenSession())
            {
                var r = s.CreateCriteria <Country>().List();
            }
            Assert.AreEqual(1, stats.QueryExecutionCount);

            stats.Clear();
            using (ISession s = OpenSession())
            {
                var r = s.CreateQuery("from Country").List();
            }
            Assert.AreEqual(1, stats.QueryExecutionCount);

            stats.Clear();

            var driver = Sfi.ConnectionProvider.Driver;

            if (driver.SupportsMultipleQueries)
            {
                using (var s = OpenSession())
                {
                    var r = s.CreateMultiQuery().Add("from Country").Add("from Continent").List();
                }
                Assert.AreEqual(1, stats.QueryExecutionCount);

                stats.Clear();
                using (var s = OpenSession())
                {
                    var r = s.CreateMultiCriteria().Add(DetachedCriteria.For <Country>()).Add(DetachedCriteria.For <Continent>()).List();
                }
                Assert.AreEqual(1, stats.QueryExecutionCount);
            }

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    CleanDb(s);
                    tx.Commit();
                }
        }
        public async Task JoinedSubclassAsync()
        {
            IStatistics statistics = Sfi.Statistics;

            statistics.Clear();

            ISession     s = OpenSession();
            ITransaction t = s.BeginTransaction();

            Salesperson mark = new Salesperson();

            mark.Name            = "Mark";
            mark.Title           = "internal sales";
            mark.Sex             = 'M';
            mark.Address.address = "buckhead";
            mark.Address.zip     = "30305";
            mark.Address.country = "USA";

            Person joe = new Person();

            joe.Name            = "Joe";
            joe.Address.address = "San Francisco";
            joe.Address.zip     = "XXXXX";
            joe.Address.country = "USA";
            joe.Sex             = 'M';
            joe.Salesperson     = mark;
            mark.Customers.Add(joe);

            await(s.SaveAsync(mark));
            await(t.CommitAsync());

            Assert.AreEqual(2, statistics.EntityInsertCount);
            Assert.IsTrue(5 >= statistics.PrepareStatementCount);

            statistics.Clear();

            t = s.BeginTransaction();
            await(s.DeleteAsync(mark));
            await(t.CommitAsync());

            Assert.AreEqual(2, statistics.EntityDeleteCount);
            Assert.AreEqual(1, statistics.PrepareStatementCount);

            t = s.BeginTransaction();
            IList names = await(s.CreateQuery("select p.name from Person p").ListAsync());

            Assert.AreEqual(0, names.Count);
            await(t.CommitAsync());

            s.Close();
        }
예제 #6
0
        private void OneToOneUpdateTest <TPerson, TDetails>() where TPerson : Person, new() where TDetails : Details, new()
        {
            List <object> ids = this.CreatePersonAndDetails <TPerson, TDetails>();

            IStatistics statistics = Sfi.Statistics;

            // Clear the second level cache and the statistics
            Sfi.EvictEntity(typeof(TPerson).FullName);
            Sfi.EvictEntity(typeof(TDetails).FullName);
            Sfi.EvictQueries();

            statistics.Clear();

            // Fill the empty caches with data.
            this.FetchPeopleById <TPerson>(ids);

            // Verify that no data was retrieved from the cache.
            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
            statistics.Clear();

            int personId = DeleteDetailsFromFirstPerson <TPerson>();

            // Verify that the cache was updated
            Assert.AreEqual(1, statistics.SecondLevelCachePutCount, "Second level cache put count");
            statistics.Clear();

            // Verify that the Person was updated in the cache
            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    TPerson person = s.Get <TPerson>(personId);

                    Assert.IsNull(person.Details);
                }

            Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
            statistics.Clear();

            // Verify that the Details was removed from the cache and deleted.
            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    TDetails details = s.Get <TDetails>(personId);

                    Assert.Null(details);
                }

            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
        }
예제 #7
0
        private async Task OneToOneUpdateTestAsync <TPerson, TDetails>(CancellationToken cancellationToken = default(CancellationToken)) where TPerson : Person, new() where TDetails : Details, new()
        {
            List <object> ids = await(this.CreatePersonAndDetailsAsync <TPerson, TDetails>(cancellationToken));

            IStatistics statistics = Sfi.Statistics;

            // Clear the second level cache and the statistics
            await(Sfi.EvictEntityAsync(typeof(TPerson).FullName, cancellationToken));
            await(Sfi.EvictEntityAsync(typeof(TDetails).FullName, cancellationToken));
            await(Sfi.EvictQueriesAsync(cancellationToken));

            statistics.Clear();

            // Fill the empty caches with data.
            await(this.FetchPeopleByIdAsync <TPerson>(ids, cancellationToken));

            // Verify that no data was retrieved from the cache.
            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
            statistics.Clear();

            int personId = await(DeleteDetailsFromFirstPersonAsync <TPerson>(cancellationToken));

            // Verify that the cache was updated
            Assert.AreEqual(1, statistics.SecondLevelCachePutCount, "Second level cache put count");
            statistics.Clear();

            // Verify that the Person was updated in the cache
            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    TPerson person = await(s.GetAsync <TPerson>(personId, cancellationToken));

                    Assert.IsNull(person.Details);
                }

            Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
            statistics.Clear();

            // Verify that the Details was removed from the cache and deleted.
            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    TDetails details = await(s.GetAsync <TDetails>(personId, cancellationToken));

                    Assert.Null(details);
                }

            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
        }
예제 #8
0
        public void ParentChildCascade()
        {
            ISession     s = OpenSession();
            ITransaction t = s.BeginTransaction();
            Parent       p = new Parent("foo");

            p.Children.Add(new Child(p, "foo1"));
            p.Children.Add(new Child(p, "foo2"));
            Parent q = new Parent("bar");

            q.Children.Add(new Child(q, "bar1"));
            q.Children.Add(new Child(q, "bar2"));
            s.Save(p);
            s.Save(q);
            t.Commit();
            s.Close();

            IStatistics statistics = Sfi.Statistics;

            statistics.Clear();

            s = OpenSession();
            t = s.BeginTransaction();
            IList <Parent> l = s.CreateQuery("from Parent").List <Parent>();

            p = l[0];
            q = l[1];
            statistics.Clear();

            s.Delete(p);
            s.Delete(q);
            t.Commit();

            Assert.AreEqual(2, statistics.PrepareStatementCount);
            Assert.AreEqual(6, statistics.EntityDeleteCount);

            t = s.BeginTransaction();
            IList names = s.CreateQuery("from Parent p").List();

            Assert.AreEqual(0, names.Count);

            t.Commit();
            s.Close();
        }
예제 #9
0
        public async Task BehaviorAsync()
        {
            object savedId;
            A      a = new A();

            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    savedId = await(s.SaveAsync(a));
                    await(t.CommitAsync());
                }

            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    a = await(s.GetAsync <A>(savedId));

                    IStatistics statistics = Sfi.Statistics;
                    statistics.Clear();

                    Assert.IsNotNull(a.B);             // an instance of B was created
                    await(s.FlushAsync());
                    await(t.CommitAsync());

                    // since we don't change anyproperties in a.B there are no dirty entity to commit
                    Assert.AreEqual(0, statistics.PrepareStatementCount);
                }

            // using proxy
            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    a = await(s.LoadAsync <A>(savedId));

                    IStatistics statistics = Sfi.Statistics;
                    statistics.Clear();

                    Assert.IsNotNull(a.B);             // an instance of B was created
                    await(s.FlushAsync());
                    await(t.CommitAsync());

                    Assert.AreEqual(0, statistics.PrepareStatementCount);
                }

            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    await(s.DeleteAsync("from A"));
                    await(t.CommitAsync());
                }
        }
예제 #10
0
        public void Behavior()
        {
            object savedId;
            A      a = new A();

            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    savedId = s.Save(a);
                    t.Commit();
                }

            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    a = s.Get <A>(savedId);

                    IStatistics statistics = sessions.Statistics;
                    statistics.Clear();

                    Assert.IsNotNull(a.B);             // an instance of B was created
                    s.Flush();
                    t.Commit();

                    // since we don't change anyproperties in a.B there are no dirty entity to commit
                    Assert.AreEqual(0, statistics.PrepareStatementCount);
                }

            // using proxy
            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    a = s.Load <A>(savedId);

                    IStatistics statistics = sessions.Statistics;
                    statistics.Clear();

                    Assert.IsNotNull(a.B);             // an instance of B was created
                    s.Flush();
                    t.Commit();

                    Assert.AreEqual(0, statistics.PrepareStatementCount);
                }

            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    s.Delete("from A");
                    t.Commit();
                }
        }
예제 #11
0
        public void Test()
        {
            int employeeId;

            using (ISession sess = OpenSession())
                using (ITransaction tx = sess.BeginTransaction())
                {
                    Department dept = new Department();
                    dept.Id   = 11;
                    dept.Name = "Animal Testing";

                    sess.Save(dept);

                    Employee emp = new Employee();
                    emp.Id         = 1;
                    emp.FirstName  = "John";
                    emp.LastName   = "Doe";
                    emp.Department = dept;

                    sess.Save(emp);

                    tx.Commit();

                    employeeId = emp.Id;
                }

            ExecuteStatement(string.Format("UPDATE EMPLOYEES SET DEPARTMENT_ID = 99999 WHERE EMPLOYEE_ID = {0}", employeeId));

            IStatistics stat = Sfi.Statistics;

            stat.Clear();
            using (ISession sess = OpenSession())
                using (ITransaction tx = sess.BeginTransaction())
                {
                    sess.Get <Employee>(employeeId);

                    Assert.AreEqual(1, stat.PrepareStatementCount);
                    tx.Commit();
                }

            using (ISession sess = OpenSession())
                using (ITransaction tx = sess.BeginTransaction())
                {
                    sess.Delete("from Employee");
                    sess.Delete("from Department");
                    tx.Commit();
                }
        }
        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 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;
        }
예제 #14
0
        public void IStatisticsTesting()
        {
            TestInfrastructure.DebugLineStart(TestContext);
            if (TestInfrastructure.IsActive(TestContext) && TestInfrastructure.ConnectionType(TestContext) == "File")       //TODO??? Only in case of XML
            {
                int lmid;
                using (IDictionary target = TestInfrastructure.GetLMConnection(TestContext, "IStatisticsTest"))
                {
                    if (target.Parent.CurrentUser.ConnectionString.Typ == DatabaseType.Xml)
                    {
                        return;
                    }

                    lmid = target.Id;
                    IStatistics statistics = target.Statistics;
                    statistics.Clear();

                    Log.OpenUserSession(target.Id, target.Parent);

                    IStatistic stat = statistics[0];
                    stat.StartTimestamp = new DateTime(2008, 10, 10, 12, 0, 0);
                    stat.Right          = 10;
                    stat.Wrong          = 12;
                    stat.Boxes[0]       = 1;
                    stat.Boxes[1]       = 2;
                    stat.Boxes[2]       = 3;
                    stat.Boxes[3]       = 4;
                    stat.Boxes[4]       = 5;
                    stat.Boxes[5]       = 6;
                    stat.Boxes[6]       = 7;
                    stat.Boxes[7]       = 8;
                    stat.Boxes[8]       = 9;
                    stat.Boxes[9]       = 10;
                    stat.EndTimestamp   = new DateTime(2008, 10, 10, 12, 30, 0);

                    target.Save();
                }

                using (IDictionary target = TestInfrastructure.GetPersistentLMConnection(TestContext, "IStatisticsTest", lmid))
                {
                    IStatistics statistics = target.Statistics;
                    Assert.AreEqual <int>(1, statistics.Count, "Statistics Count is wrong!");

                    IStatistic stat = statistics[0];

                    Assert.AreEqual <DateTime>(new DateTime(2008, 10, 10, 12, 0, 0), stat.StartTimestamp, "Start timestamp not saved correctly!");
                    Assert.AreEqual <int>(10, stat.Right, "Right Count not saved correctly!");
                    Assert.AreEqual <int>(12, stat.Wrong, "Wrong Count not saved correctly!");
                    Assert.AreEqual <int>(1, stat.Boxes[0], "Box size not saved correctly!");
                    Assert.AreEqual <int>(2, stat.Boxes[1], "Box size not saved correctly!");
                    Assert.AreEqual <int>(3, stat.Boxes[2], "Box size not saved correctly!");
                    Assert.AreEqual <int>(4, stat.Boxes[3], "Box size not saved correctly!");
                    Assert.AreEqual <int>(5, stat.Boxes[4], "Box size not saved correctly!");
                    Assert.AreEqual <int>(6, stat.Boxes[5], "Box size not saved correctly!");
                    Assert.AreEqual <int>(7, stat.Boxes[6], "Box size not saved correctly!");
                    Assert.AreEqual <int>(8, stat.Boxes[7], "Box size not saved correctly!");
                    Assert.AreEqual <int>(9, stat.Boxes[8], "Box size not saved correctly!");
                    Assert.AreEqual <int>(10, stat.Boxes[9], "Box size not saved correctly!");
                    Assert.AreEqual <DateTime>(new DateTime(2008, 10, 10, 12, 30, 0), stat.EndTimestamp, "End timestamp not saved correctly!");
                }
            }
            TestInfrastructure.DebugLineEnd(TestContext);
        }
예제 #15
0
        public async Task IncrementQueryExecutionCount_WhenExplicitQueryIsExecutedAsync()
        {
            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    await(FillDbAsync(s));
                    await(tx.CommitAsync());
                }

            IStatistics stats = Sfi.Statistics;

            stats.Clear();
            using (ISession s = OpenSession())
            {
                var r = await(s.CreateCriteria <Country>().ListAsync());
            }
            Assert.AreEqual(1, stats.QueryExecutionCount);

            stats.Clear();
            using (ISession s = OpenSession())
            {
                var r = await(s.CreateQuery("from Country").ListAsync());
            }
            Assert.AreEqual(1, stats.QueryExecutionCount);

            stats.Clear();

            var driver = Sfi.ConnectionProvider.Driver;

            if (driver.SupportsMultipleQueries)
            {
#pragma warning disable 618
                using (var s = OpenSession())
                {
                    var r = await(s.CreateMultiQuery().Add("from Country").Add("from Continent").ListAsync());
                }
                Assert.AreEqual(1, stats.QueryExecutionCount);

                stats.Clear();
                using (var s = OpenSession())
                {
                    var r = await(s.CreateMultiCriteria().Add(DetachedCriteria.For <Country>()).Add(DetachedCriteria.For <Continent>()).ListAsync());
                }
                Assert.AreEqual(1, stats.QueryExecutionCount);
#pragma warning restore 618

                stats.Clear();
                using (var s = OpenSession())
                {
                    await(s.CreateQueryBatch()
                          .Add <Country>(s.CreateQuery("from Country"))
                          .Add <Continent>(s.CreateQuery("from Continent"))
                          .ExecuteAsync());
                }
                Assert.That(stats.QueryExecutionCount, Is.EqualTo(1));

                stats.Clear();
                using (var s = OpenSession())
                {
                    await(s.CreateQueryBatch()
                          .Add <Country>(DetachedCriteria.For <Country>())
                          .Add <Continent>(DetachedCriteria.For <Continent>())
                          .ExecuteAsync());
                }
                Assert.That(stats.QueryExecutionCount, Is.EqualTo(1));
            }

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    await(CleanDbAsync(s));
                    await(tx.CommitAsync());
                }
        }
 /// <summary>
 /// Deletes all statistics.
 /// </summary>
 /// <remarks>Documented by Dev05, 2007-09-03</remarks>
 public void DeleteAllStatistics()
 {
     statistics.Clear();
 }
예제 #17
0
        public void QueryStatGathering()
        {
            IStatistics stats = Sfi.Statistics;

            stats.Clear();

            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

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

            s  = OpenSession();
            tx = s.BeginTransaction();
            string          continents     = "from Continent";
            int             results        = s.CreateQuery(continents).List().Count;
            QueryStatistics continentStats = stats.GetQueryStatistics(continents);

            Assert.IsNotNull(continentStats, "stats were null");
            Assert.AreEqual(1, continentStats.ExecutionCount, "unexpected execution count");
            Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count");
            var maxTime = continentStats.ExecutionMaxTime;

            Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
            Assert.AreEqual(continents, stats.QueryExecutionMaxTimeQueryString);

            IEnumerable itr = s.CreateQuery(continents).Enumerable();

            // Enumerable() should increment the execution count
            Assert.AreEqual(2, continentStats.ExecutionCount, "unexpected execution count");
            // but should not effect the cumulative row count
            Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count");
            NHibernateUtil.Close(itr);
            tx.Commit();
            s.Close();

            // explicitly check that statistics for "split queries" get collected
            // under the original query
            stats.Clear();
            s  = OpenSession();
            tx = s.BeginTransaction();
            string localities = "from Locality";

            results = s.CreateQuery(localities).List().Count;
            QueryStatistics localityStats = stats.GetQueryStatistics(localities);

            Assert.IsNotNull(localityStats, "stats were null");
            // ...one for each split query
            Assert.AreEqual(2, localityStats.ExecutionCount, "unexpected execution count");
            Assert.AreEqual(results, localityStats.ExecutionRowCount, "unexpected row count");
            maxTime = localityStats.ExecutionMaxTime;
            Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
            Assert.AreEqual(localities, stats.QueryExecutionMaxTimeQueryString);
            tx.Commit();
            s.Close();
            Assert.IsFalse(s.IsOpen);

            // native sql queries
            stats.Clear();
            s  = OpenSession();
            tx = s.BeginTransaction();
            string sql = "select Id, Name from Country";

            results = s.CreateSQLQuery(sql).AddEntity(typeof(Country)).List().Count;
            QueryStatistics sqlStats = stats.GetQueryStatistics(sql);

            Assert.IsNotNull(sqlStats, "sql stats were null");
            Assert.AreEqual(1, sqlStats.ExecutionCount, "unexpected execution count");
            Assert.AreEqual(results, sqlStats.ExecutionRowCount, "unexpected row count");
            maxTime = sqlStats.ExecutionMaxTime;
            Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
            Assert.AreEqual(sql, stats.QueryExecutionMaxTimeQueryString);
            tx.Commit();
            s.Close();

            s  = OpenSession();
            tx = s.BeginTransaction();
            CleanDb(s);
            tx.Commit();
            s.Close();
        }
예제 #18
0
        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();
        }
예제 #19
0
        public async Task QueryStatGatheringAsync()
        {
            IStatistics stats = Sfi.Statistics;

            stats.Clear();

            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

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

            s  = OpenSession();
            tx = s.BeginTransaction();
            string          continents     = "from Continent";
            int             results        = (await(s.CreateQuery(continents).ListAsync())).Count;
            QueryStatistics continentStats = stats.GetQueryStatistics(continents);

            Assert.IsNotNull(continentStats, "stats were null");
            Assert.AreEqual(1, continentStats.ExecutionCount, "unexpected execution count");
            Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count");
            var maxTime = continentStats.ExecutionMaxTime;

            Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
            Assert.AreEqual(continents, stats.QueryExecutionMaxTimeQueryString);

            IEnumerable itr = await(s.CreateQuery(continents).EnumerableAsync());

            // Enumerable() should increment the execution count
            Assert.AreEqual(2, continentStats.ExecutionCount, "unexpected execution count");
            // but should not effect the cumulative row count
            Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count");
            NHibernateUtil.Close(itr);
            await(tx.CommitAsync());
            s.Close();

            // explicitly check that statistics for "split queries" get collected
            // under the original query
            stats.Clear();
            s  = OpenSession();
            tx = s.BeginTransaction();
            string localities = "from Locality";

            results = (await(s.CreateQuery(localities).ListAsync())).Count;
            QueryStatistics localityStats = stats.GetQueryStatistics(localities);

            Assert.IsNotNull(localityStats, "stats were null");
            // ...one for each split query
            Assert.AreEqual(2, localityStats.ExecutionCount, "unexpected execution count");
            Assert.AreEqual(results, localityStats.ExecutionRowCount, "unexpected row count");
            maxTime = localityStats.ExecutionMaxTime;
            Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
            Assert.AreEqual(localities, stats.QueryExecutionMaxTimeQueryString);
            await(tx.CommitAsync());
            s.Close();
            Assert.IsFalse(s.IsOpen);

            // native sql queries
            stats.Clear();
            s  = OpenSession();
            tx = s.BeginTransaction();
            string sql = "select Id, Name from Country";

            results = (await(s.CreateSQLQuery(sql).AddEntity(typeof(Country)).ListAsync())).Count;
            QueryStatistics sqlStats = stats.GetQueryStatistics(sql);

            Assert.IsNotNull(sqlStats, "sql stats were null");
            Assert.AreEqual(1, sqlStats.ExecutionCount, "unexpected execution count");
            Assert.AreEqual(results, sqlStats.ExecutionRowCount, "unexpected row count");
            maxTime = sqlStats.ExecutionMaxTime;
            Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
            Assert.AreEqual(sql, stats.QueryExecutionMaxTimeQueryString);

            // check that 2nd query correctly updates query statistics
            results = (await(s.CreateSQLQuery(sql).AddEntity(typeof(Country)).ListAsync())).Count;
            var queryTime1 = maxTime;
            var queryTime2 = sqlStats.ExecutionMaxTime == maxTime ? sqlStats.ExecutionMinTime : sqlStats.ExecutionMaxTime;

            Assert.AreEqual(2, sqlStats.ExecutionCount, "unexpected execution count");
            Assert.AreEqual((queryTime1 + queryTime2).Ticks / 2, sqlStats.ExecutionAvgTime.Ticks, 2, "unexpected average time");

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

            s  = OpenSession();
            tx = s.BeginTransaction();
            await(CleanDbAsync(s));
            await(tx.CommitAsync());
            s.Close();
        }