Advertisement
Guest User

LINQ Query Performance Test Code

a guest
Sep 26th, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.92 KB | None | 0 0
  1.  
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Project;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using Project.Extensions;
  9. using System.Diagnostics;
  10.  
  11. using LocationModel = Project.Model.Entities.Location;
  12. using UserModel = Project.Model.Entities.User;
  13.  
  14.  
  15. namespace ProjectTests
  16. {
  17.     // ServiceStack Request DTO, returning a List<LocationModel>
  18.     public class GetLocations
  19.     {
  20.         // The user id owning these Locations
  21.         public int UserId { get; set; }
  22.         // If specified, narrows the search by a minimum timestamp
  23.         public long? MinTimestamp { get; set; }
  24.         // If specified, narrows the search by a maximum timestamp
  25.         public long? MaxTimestamp { get; set; }
  26.         // If specified, returns the last N Location objects stored by the user (filters in conjunction with Min/MaxTimestamp)
  27.         public int? Last { get; set; }
  28.     }
  29.  
  30.     // Model object
  31.     public class LocationModelExample
  32.     {
  33.         public virtual int Id { get; protected set; }
  34.  
  35.         public virtual UserModel User { get; set; }
  36.         public virtual long Timestamp { get; set; }
  37.  
  38.         public virtual decimal Latitude { get; set; }
  39.         public virtual decimal Longitude { get; set; }
  40.  
  41.         public LocationModelExample()
  42.         {
  43.             Timestamp = default(long);
  44.  
  45.             Latitude = default(decimal);
  46.             Longitude = default(decimal);
  47.         }
  48.  
  49.         public override string ToString()
  50.         {
  51.             return String.Format("{Timestamp={0}, Latitude={0}, Longitude={1}", Timestamp, Latitude, Longitude);
  52.         }
  53.     }
  54.  
  55.     [TestClass]
  56.     public class TestLinqQueryPerformance
  57.     {
  58.         private Stopwatch stopwatch;
  59.         private GetLocations request;
  60.  
  61.         // Results of the queries are stored here
  62.         private IEnumerable<LocationModel> locationsList = null;
  63.  
  64.         public TestLinqQueryPerformance()
  65.         {
  66.             stopwatch = new Stopwatch();
  67.  
  68.             request = new GetLocations()
  69.             {
  70.                 UserId = 1,
  71.                 MinTimestamp = 0,
  72.                 MaxTimestamp = DateTime.Now.ToTimestamp(),
  73.                 Last = 5,
  74.             };
  75.         }
  76.  
  77.         [TestMethod]
  78.         public void TestWarmup()
  79.         {
  80.             stopwatch.Start();
  81.  
  82.             // Warmup method one
  83.             for (int i = 0; i < 100; i++)
  84.             {
  85.                 using (var session = NHibernateHelper.OpenSession())
  86.                 {
  87.                     using (var transaction = session.BeginTransaction())
  88.                     {
  89.                         var user = session.Get<UserModel>(request.UserId);
  90.  
  91.                         var locations = user.Locations.Where(location =>
  92.                             location.Timestamp >= (request.MinTimestamp.HasValue ? request.MinTimestamp.Value : 0) &&
  93.                             location.Timestamp <= (request.MaxTimestamp.HasValue ? request.MaxTimestamp.Value : DateTime.Now.ToTimestamp()));
  94.  
  95.                         if (request.Last.HasValue)
  96.                         {
  97.                             locationsList = locations.TakeLastN(request.Last.Value);
  98.                         }
  99.                         else
  100.                         {
  101.                             locationsList = locations;
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.  
  107.             stopwatch.Stop();
  108.  
  109.             Trace.WriteLine(String.Format("Method 1 Warmup took {0} seconds over 100 iterations.", stopwatch.ElapsedMilliseconds / 1000.0));
  110.             Trace.WriteLine(String.Empty);
  111.             Trace.WriteLine("Results of the last query:");
  112.             Trace.WriteLine(String.Join(Environment.NewLine, locationsList.Select(x => x))); // Print the list of Locations
  113.             Trace.WriteLine(String.Empty);
  114.  
  115.             stopwatch.Reset();
  116.             stopwatch.Start();
  117.  
  118.             // Warmup method two
  119.             for (int i = 0; i < 100; i++)
  120.             {
  121.                 using (var session = NHibernateHelper.OpenSession())
  122.                 {
  123.                     using (var transaction = session.BeginTransaction())
  124.                     {
  125.                         var locationsQuery = session.QueryOver<LocationModel>()
  126.                                     .Where(table => table.User.Id == request.UserId)
  127.                                     .And(table => table.Timestamp >= (request.MinTimestamp.HasValue ? request.MinTimestamp.Value : 0))
  128.                                     .And(table => table.Timestamp <= (request.MaxTimestamp.HasValue ? request.MaxTimestamp.Value : DateTime.Now.ToTimestamp()));
  129.  
  130.                         if (request.Last.HasValue)
  131.                         {
  132.                             locationsList = locationsQuery.List().TakeLastN(request.Last.Value);
  133.                         }
  134.                         else
  135.                         {
  136.                             locationsList = locationsQuery.List();
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.  
  142.             stopwatch.Stop();
  143.  
  144.             Trace.WriteLine(String.Format("Method 2 Warmup took {0} seconds over 100 iterations.", stopwatch.ElapsedMilliseconds / 1000.0));
  145.             Trace.WriteLine(String.Empty);
  146.             Trace.WriteLine("Results of the last query:");
  147.             Trace.WriteLine(String.Join(Environment.NewLine, locationsList.Select(x => x))); // Print the list of Locations
  148.             Trace.WriteLine(String.Empty);
  149.         }
  150.  
  151.         [TestMethod]
  152.         public void TestMethod1()
  153.         {
  154.             stopwatch.Reset();
  155.             stopwatch.Start();
  156.  
  157.             var timeStampNow = DateTime.Now.ToTimestamp();
  158.  
  159.             // Warmup method one
  160.             for (int i = 0; i < 5000; i++)
  161.             {
  162.                 using (var session = NHibernateHelper.OpenSession())
  163.                 {
  164.                     using (var transaction = session.BeginTransaction())
  165.                     {
  166.                         var user = session.Get<UserModel>(request.UserId);
  167.  
  168.                         var locations = user.Locations.Where(location =>
  169.                             location.Timestamp >= (request.MinTimestamp.HasValue ? request.MinTimestamp.Value : 0) &&
  170.                             location.Timestamp <= (request.MaxTimestamp.HasValue ? request.MaxTimestamp.Value : timeStampNow));
  171.  
  172.                         if (request.Last.HasValue)
  173.                         {
  174.                             locationsList = locations.TakeLastN(request.Last.Value);
  175.                         }
  176.                         else
  177.                         {
  178.                             locationsList = locations;
  179.                         }
  180.                     }
  181.                 }
  182.             }
  183.  
  184.             stopwatch.Stop();
  185.  
  186.             Trace.WriteLine(String.Format("Method 1 took {0} seconds over 5000 iterations.", stopwatch.ElapsedMilliseconds / 1000.0));
  187.             Trace.WriteLine(String.Empty);
  188.             Trace.WriteLine("Results of the last query:");
  189.             Trace.WriteLine(String.Join(Environment.NewLine, locationsList.Select(x => x))); // Print the list of Locations
  190.             Trace.WriteLine(String.Empty);
  191.         }
  192.  
  193.         [TestMethod]
  194.         public void TestMethod2()
  195.         {
  196.             stopwatch.Reset();
  197.             stopwatch.Start();
  198.  
  199.             var timeStampNow = DateTime.Now.ToTimestamp();
  200.  
  201.             // Warmup method two
  202.             for (int i = 0; i < 5000; i++)
  203.             {
  204.                 using (var session = NHibernateHelper.OpenSession())
  205.                 {
  206.                     using (var transaction = session.BeginTransaction())
  207.                     {
  208.                         var locationsQuery = session.QueryOver<LocationModel>()
  209.                                     .Where(table => table.User.Id == request.UserId)
  210.                                     .And(table => table.Timestamp >= (request.MinTimestamp.HasValue ? request.MinTimestamp.Value : 0))
  211.                                     .And(table => table.Timestamp <= (request.MaxTimestamp.HasValue ? request.MaxTimestamp.Value : timeStampNow));
  212.  
  213.                         if (request.Last.HasValue)
  214.                         {
  215.                             locationsList = locationsQuery.List().TakeLastN(request.Last.Value);
  216.                         }
  217.                         else
  218.                         {
  219.                             locationsList = locationsQuery.List();
  220.                         }
  221.                     }
  222.                 }
  223.             }
  224.  
  225.             stopwatch.Stop();
  226.  
  227.             Trace.WriteLine(String.Format("Method 2 took {0} seconds over 5000 iterations.", stopwatch.ElapsedMilliseconds / 1000.0));
  228.             Trace.WriteLine(String.Empty);
  229.             Trace.WriteLine("Results of the last query:");
  230.             Trace.WriteLine(String.Join(Environment.NewLine, locationsList.Select(x => x))); // Print the list of Locations
  231.             Trace.WriteLine(String.Empty);
  232.         }
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement