コード例 #1
0
        static void Main(string[] args)
        {
            var hydrator = new Hydrator();

            hydrator.Configure(x =>
            {
                x.For <Person>().Property(y => y.Id).Skip();
                x.For <Person>().Property(y => y.FirstName).Is(CommonType.AmericanName);
                x.For <Person>().Property(y => y.LastName).Is(CommonType.AmericanLastName);
                x.For <Person>().Property(y => y.Weight).Use(new IntTypeConvention(99, 250));
                x.For <Person>().Property(y => y.Height).Use(new IntTypeConvention(100, 999));
            });

            var people = Enumerable.Range(1, 1000)
                         .Select(x => hydrator.Hydrate <Person>())
                         .ToList();

            IPagedList <Person> original;
            IPagedList <Person> optimized;

            using (var database = new LocalDb())
            {
                Context.Load(database.ConnectionString, people);

                Expression <Func <Context, IQueryable <Person> > > compiled = (db) => db.Persons.OrderBy(x => x.Id);
                var query = compiled.Compile();

                var stopwatch = new Stopwatch();

                stopwatch.Start();
                using (var context = new Context(database.ConnectionString))
                {
                    original = query(context).ToPagedList(1, 100);
                }
                stopwatch.Stop();
                Console.WriteLine("original : {0} ms", stopwatch.Elapsed.TotalMilliseconds);

                stopwatch.Restart();
                using (var context = new Context(database.ConnectionString))
                {
                    optimized = new EntityFrameworkPagedList <Person>(query(context), 1, 100);
                }
                stopwatch.Stop();
                Console.WriteLine("optimized : {0} ms", stopwatch.Elapsed.TotalMilliseconds);
            }

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var hydrator = new Hydrator();

            hydrator.Configure(x =>
            {
                x.For <Person>().Property(y => y.Id).Skip();
                x.For <Person>().Property(y => y.FirstName).Is(CommonType.AmericanName);
                x.For <Person>().Property(y => y.LastName).Is(CommonType.AmericanLastName);
                x.For <Person>().Property(y => y.Weight).Use(new IntTypeConvention(99, 250));
                x.For <Person>().Property(y => y.Height).Use(new IntTypeConvention(100, 999));
            });

            var people = Enumerable.Range(1, 1000)
                         .Select(x => hydrator.Hydrate <Person>())
                         .ToList();

            IPagedList <Person> original;
            IPagedList <Person> optimized;
            IPagedList <Person> manual;
            IPagedList <Person> future;

            using (var database = new LocalDb())
            {
                var connectionString = database.ConnectionString + "MultipleActiveResultSets=True;";

                Context.Load(connectionString, people);

                var stopwatch = new Stopwatch();

                Dictionary <string, List <double> > runs = new Dictionary <string, List <double> >()
                {
                    { "original", new List <double>() },
                    { "optimized", new List <double>() },
                    { "manual", new List <double>() },
                    { "future (static)", new List <double>() }
                };

                for (int i = 0; i < 100; i++)
                {
                    using (var context = new Context(connectionString))
                    {
                        var query = context.Persons.OrderBy(x => x.Id).AsNoTracking();
                        stopwatch.Start();
                        original = query.ToPagedList(1, 100);
                        stopwatch.Stop();
                        Print("original", original, stopwatch, runs);
                    }

                    using (var context = new Context(database.ConnectionString))
                    {
                        var query = context.Persons.OrderBy(x => x.Id).AsNoTracking();
                        stopwatch.Restart();
                        optimized = new EntityFrameworkPagedList <Person>(query, 1, 100);
                        stopwatch.Stop();
                        Print("optimized", optimized, stopwatch, runs);
                    }

                    using (var context = new Context(database.ConnectionString))
                    {
                        var query = context.Persons.OrderBy(x => x.Id).AsNoTracking();
                        stopwatch.Restart();
                        manual = new StaticPagedList <Person>(query.Skip(0).Take(100).ToList(), 1, 100, query.Count());
                        stopwatch.Stop();
                        Print("manual", manual, stopwatch, runs);
                    }

                    using (var context = new Context(database.ConnectionString))
                    {
                        var query = context.Persons.OrderBy(x => x.Id).AsNoTracking();

                        var items = query.Skip(0).Take(100).Future();
                        var total = query.FutureCount();

                        stopwatch.Restart();
                        future = new StaticPagedList <Person>(items, 1, 100, total);
                        stopwatch.Stop();
                        Print("future (static)", future, stopwatch, runs);
                    }
                    if (i % 100 == 0)
                    {
                        Console.WriteLine("{0} -----", i);
                    }
                }

                foreach (var run in runs)
                {
                    Console.WriteLine("{0} average : {1} ms", run.Key, run.Value.Average());
                }
                Console.WriteLine("-----");
            }

            Console.ReadLine();
        }