Browse Source

ASP.NET: Update test + Fortunes test for MongoDB

Pēteris Ņikiforovs 12 years ago
parent
commit
fe2dbfbdda

+ 1 - 1
aspnet/lib/packages.config

@@ -8,5 +8,5 @@
   <package id="MSBuild.Microsoft.VisualStudio.Web.targets" version="11.0.2.1" />
   <package id="mongocsharpdriver" version="1.8.1" targetFramework="net45" />
   <package id="MySql.Data.Entity" version="6.7.2-beta-ef6" targetFramework="net45" />
-  <package id="Npgsql.EF6" version="2.0.12-pre3" targetFramework="net45" />
+  <package id="Npgsql.EF6" version="2.0.12-pre4" targetFramework="net45" />
 </packages>

+ 2 - 1
aspnet/src/Application.cs

@@ -40,7 +40,8 @@ namespace Benchmarks.AspNet
             RouteTable.Routes.MapRoute(
                 name: "WithProviders",
                 url: "{controller}/{providerName}/{action}",
-                defaults: new { action = "Index" }
+                defaults: new { action = "Index" },
+                constraints: new { controller = "ado|entityframework", providerName = "mysql|postgresql" }
             );
 
             RouteTable.Routes.MapRoute(

+ 12 - 6
aspnet/src/Benchmarks.AspNet.csproj

@@ -42,6 +42,7 @@
     <Reference Include="System.Configuration" />
     <Reference Include="System.Data" />
     <Reference Include="System.Web" />
+    <Reference Include="System.Web.Extensions" />
     <Reference Include="System.Web.Mvc">
       <Private>True</Private>
       <HintPath>..\lib\Microsoft.AspNet.Mvc.4.0.20710.0\lib\net40\System.Web.Mvc.dll</HintPath>
@@ -74,27 +75,31 @@
       <Private>True</Private>
       <HintPath>..\lib\EntityFramework.6.0.0-alpha3\lib\net45\EntityFramework.dll</HintPath>
     </Reference>
-    <Reference Include="MySql.Data">
+    <Reference Include="EntityFramework">
+      <Private>True</Private>
+      <HintPath>..\lib\EntityFramework.6.0.0-alpha3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
+    </Reference>
+    <Reference Include="MySql">
       <Private>True</Private>
       <HintPath>..\lib\MySql.Data.Entity.6.7.2-beta-ef6\lib\net45\MySql.Data.dll</HintPath>
     </Reference>
-    <Reference Include="MySql.Data.Entity">
+    <Reference Include="MySql">
       <Private>True</Private>
       <HintPath>..\lib\MySql.Data.Entity.6.7.2-beta-ef6\lib\net45\MySql.Data.Entity.dll</HintPath>
     </Reference>
     <Reference Include="Npgsql">
       <Private>True</Private>
-      <HintPath>..\lib\Npgsql.EF6.2.0.12-pre2\lib\net45\Npgsql.dll</HintPath>
+      <HintPath>..\lib\Npgsql.EF6.2.0.12-pre4\lib\net45\Npgsql.dll</HintPath>
     </Reference>
     <Reference Include="Npgsql">
       <Private>True</Private>
-      <HintPath>..\lib\Npgsql.EF6.2.0.12-pre2\lib\net45\Mono.Security.dll</HintPath>
+      <HintPath>..\lib\Npgsql.EF6.2.0.12-pre4\lib\net45\Mono.Security.dll</HintPath>
     </Reference>
-    <Reference Include="MongoDB.Bson">
+    <Reference Include="MongoDB">
       <Private>True</Private>
       <HintPath>..\lib\mongocsharpdriver.1.8.1\lib\net35\MongoDB.Bson.dll</HintPath>
     </Reference>
-    <Reference Include="MongoDB.Driver">
+    <Reference Include="MongoDB">
       <Private>True</Private>
       <HintPath>..\lib\mongocsharpdriver.1.8.1\lib\net35\MongoDB.Driver.dll</HintPath>
     </Reference>
@@ -111,6 +116,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Models\EntityFramework.cs" />
+    <Compile Include="Models\MongoDB.cs" />
     <Compile Include="Models\World.cs" />
     <Compile Include="Models\Fortune.cs" />
   </ItemGroup>

+ 73 - 13
aspnet/src/Controllers/AdoController.cs

@@ -11,6 +11,8 @@ namespace Benchmarks.AspNet.Controllers
 {
     public class AdoController : Controller
     {
+        Random random = new Random();
+
         private DbConnection CreateConnection(string providerName)
         {
             ConnectionStringSettings connectionSettings = ConfigurationManager.ConnectionStrings[providerName];
@@ -22,7 +24,7 @@ namespace Benchmarks.AspNet.Controllers
         
         public ActionResult Index(string providerName, int? queries)
         {
-            List<World> worlds = new List<World>(queries ?? 1);
+            List<World> worlds = new List<World>(Math.Max(1, Math.Min(500, queries ?? 1)));
             
             using (DbConnection connection = CreateConnection(providerName))
             {
@@ -31,9 +33,7 @@ namespace Benchmarks.AspNet.Controllers
                 using (DbCommand command = connection.CreateCommand())
                 {
                     command.CommandText = "SELECT * FROM World WHERE id = @ID";
-                    
-                    Random random = new Random();
-                    
+
                     for (int i = 0; i < worlds.Capacity; i++)
                     {
                         int randomID = random.Next(0, 10000) + 1;
@@ -49,11 +49,11 @@ namespace Benchmarks.AspNet.Controllers
                         {
                             if (reader.Read())
                             {
-                                World world = new World();
-                                world.id = reader.GetInt32(0);
-                                world.randomNumber = reader.GetInt32(1);
-                                
-                                worlds.Add(world);
+                                worlds.Add(new World
+                                {
+                                    id = reader.GetInt32(0),
+                                    randomNumber = reader.GetInt32(1)
+                                });
                             }
                         }
                     }
@@ -80,13 +80,11 @@ namespace Benchmarks.AspNet.Controllers
                     {
                         while (reader.Read())
                         {
-                            Fortune fortune = new Fortune
+                            fortunes.Add(new Fortune
                             {
                                 ID = reader.GetInt32(0),
                                 Message = reader.GetString(1)
-                            };
-                            
-                            fortunes.Add(fortune);
+                            });
                         }
                     }
                 }
@@ -97,5 +95,67 @@ namespace Benchmarks.AspNet.Controllers
             
             return View("Fortunes", fortunes);
         }
+
+        public ActionResult Update(string providerName, int? queries)
+        {
+            List<World> worlds = new List<World>(Math.Max(1, Math.Min(500, queries ?? 1)));
+
+            using (DbConnection connection = CreateConnection(providerName))
+            {
+                connection.Open();
+
+                using (DbCommand selectCommand = connection.CreateCommand(),
+                                 updateCommand = connection.CreateCommand())
+                {
+                    selectCommand.CommandText = "SELECT * FROM World WHERE id = @ID";
+                    updateCommand.CommandText = "UPDATE World SET randomNumber = @Number WHERE id = @ID";
+
+                    for (int i = 0; i < worlds.Capacity; i++)
+                    {
+                        int randomID = random.Next(0, 10000) + 1;
+                        int randomNumber = random.Next(0, 10000) + 1;
+
+                        DbParameter idParameter = selectCommand.CreateParameter();
+                        idParameter.ParameterName = "@ID";
+                        idParameter.Value = randomID;
+
+                        selectCommand.Parameters.Clear();
+                        selectCommand.Parameters.Add(idParameter);
+
+                        World world = null;
+
+                        using (DbDataReader reader = selectCommand.ExecuteReader(CommandBehavior.SingleRow))
+                        {
+                            if (reader.Read())
+                            {
+                                world = new World
+                                {
+                                    id = reader.GetInt32(0),
+                                    randomNumber = reader.GetInt32(1)
+                                };
+                            }
+                        }
+
+                        if (world == null)
+                            continue;
+                        
+                        DbParameter numberParameter = updateCommand.CreateParameter();
+                        numberParameter.ParameterName = "@Number";
+                        numberParameter.Value = randomNumber;
+
+                        updateCommand.Parameters.Clear();
+                        updateCommand.Parameters.Add(idParameter);
+                        updateCommand.Parameters.Add(numberParameter);
+
+                        updateCommand.ExecuteNonQuery();
+                        
+                        world.randomNumber = randomNumber;
+                        worlds.Add(world);
+                    }
+                }
+            }
+
+            return Json(worlds, JsonRequestBehavior.AllowGet);
+        }
     }
 }

+ 26 - 3
aspnet/src/Controllers/EntityFrameworkController.cs

@@ -8,14 +8,14 @@ namespace Benchmarks.AspNet.Controllers
 {
     public class EntityFrameworkController : Controller
     {
+        Random random = new Random();
+
         public ActionResult Index(string providerName, int? queries)
         {
-            List<World> worlds = new List<World>(queries ?? 1);
+            List<World> worlds = new List<World>(Math.Max(1, Math.Min(500, queries ?? 1)));
 
             using (EntityFramework db = new EntityFramework(providerName))
             {
-                Random random = new Random();
-                
                 for (int i = 0; i < worlds.Capacity; i++)
                 {
                     int randomID = random.Next(0, 10000) + 1;
@@ -41,5 +41,28 @@ namespace Benchmarks.AspNet.Controllers
 
             return View("Fortunes", fortunes);
         }
+
+        public ActionResult Update(string providerName, int? queries)
+        {
+            List<World> worlds = new List<World>(Math.Max(1, Math.Min(500, queries ?? 1)));
+
+            using (EntityFramework db = new EntityFramework(providerName))
+            {
+                for (int i = 0; i < worlds.Capacity; i++)
+                {
+                    int randomID = random.Next(0, 10000) + 1;
+                    int randomNumber = random.Next(0, 10000) + 1;
+                    
+                    World world = db.Worlds.Find(randomID);
+                    world.randomNumber = randomNumber;
+                    worlds.Add(world);
+                }
+
+                // batch update
+                db.SaveChanges();
+            }
+
+            return Json(worlds, JsonRequestBehavior.AllowGet);
+        }
     }
 }

+ 38 - 20
aspnet/src/Controllers/MongoDBController.cs

@@ -1,10 +1,9 @@
 using System;
 using System.Collections.Generic;
 using System.Configuration;
+using System.Linq;
 using System.Web.Mvc;
 
-using MongoDB.Bson.Serialization;
-using MongoDB.Driver;
 using MongoDB.Driver.Builders;
 
 using Benchmarks.AspNet.Models;
@@ -13,36 +12,55 @@ namespace Benchmarks.AspNet.Controllers
 {
     public class MongoDBController : Controller
     {
-        private static string connectionString = ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString;
+        Random random = new Random();
         
-        static MongoDBController()
+        public ActionResult Index(int? queries)
         {
-            BsonClassMap.RegisterClassMap<World>(m =>
+            List<World> worlds = new List<World>(Math.Max(1, Math.Min(500, queries ?? 1)));
+
+            Models.MongoDB db = new Models.MongoDB("MongoDB");
+
+            for (int i = 0; i < worlds.Capacity; i++)
             {
-                m.MapProperty(w => w.id);
-                m.MapProperty(w => w.randomNumber);
-            });
+                int randomID = random.Next(0, 10000) + 1;
+                worlds.Add(db.Worlds.FindOne(Query<World>.EQ(w => w.id, randomID)));
+            }
+
+            return queries != null ? Json(worlds, JsonRequestBehavior.AllowGet)
+                                   : Json(worlds[0], JsonRequestBehavior.AllowGet);
         }
 
-        public ActionResult Index(int? queries)
+        public ActionResult Fortunes()
         {
-            MongoClient client = new MongoClient(connectionString);
-            MongoServer server = client.GetServer();
-            MongoDatabase database = server.GetDatabase("hello_world");
-            MongoCollection<World> collection = database.GetCollection<World>("world");
-            
-            List<World> worlds = new List<World>(queries ?? 1);
-            
-            Random random = new Random();
+            Models.MongoDB db = new Models.MongoDB("MongoDB");
+
+            List<Fortune> fortunes = db.Fortunes.FindAll().ToList();
+
+            fortunes.Add(new Fortune { ID = 0, Message = "Additional fortune added at request time." });
+            fortunes.Sort();
+
+            return View("Fortunes", fortunes);
+        }
+
+        public ActionResult Update(int? queries)
+        {
+            Models.MongoDB db = new Models.MongoDB("MongoDB");
+
+            List<World> worlds = new List<World>(Math.Max(1, Math.Min(500, queries ?? 1)));
 
             for (int i = 0; i < worlds.Capacity; i++)
             {
                 int randomID = random.Next(0, 10000) + 1;
-                worlds.Add(collection.FindOne(Query<World>.EQ(w => w.id, randomID)));
+                int randomNumber = random.Next(0, 10000) + 1;
+
+                World world = db.Worlds.FindOne(Query<World>.EQ(w => w.id, randomID));
+                world.randomNumber = randomNumber;
+                worlds.Add(world);
+
+                db.Worlds.Save(world);
             }
 
-            return queries != null ? Json(worlds, JsonRequestBehavior.AllowGet)
-                                   : Json(worlds[0], JsonRequestBehavior.AllowGet);
+            return Json(worlds, JsonRequestBehavior.AllowGet);
         }
     }
 }

+ 5 - 0
aspnet/src/Models/EntityFramework.cs

@@ -20,7 +20,12 @@ namespace Benchmarks.AspNet.Models
         
         protected override void OnModelCreating(DbModelBuilder modelBuilder)
         {
+            // disable migrations checks
+            Database.SetInitializer<EntityFramework>(null);
+
             modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
+
+            modelBuilder.Ignore<MongoWorld>();
             
             if (Database.Connection is Npgsql.NpgsqlConnection)
                 modelBuilder.Conventions.Add<PostgreSqlConfigurationConvention>();

+ 53 - 0
aspnet/src/Models/MongoDB.cs

@@ -0,0 +1,53 @@
+using System.Configuration;
+using System.Web.Script.Serialization;
+
+using MongoDB.Bson;
+using MongoDB.Bson.Serialization;
+using MongoDB.Driver;
+
+namespace Benchmarks.AspNet.Models
+{
+    public class MongoDB
+    {
+        public MongoCollection<MongoWorld> Worlds { get; private set; }
+        public MongoCollection<Fortune> Fortunes { get; private set; }
+
+        static MongoDB()
+        {
+            BsonClassMap.RegisterClassMap<World>(m =>
+            {
+                m.MapProperty(w => w.id);
+                m.MapProperty(w => w.randomNumber);
+            });
+
+            BsonClassMap.RegisterClassMap<MongoWorld>(m =>
+            {
+                m.MapIdProperty(w => w._id);
+            });
+
+            BsonClassMap.RegisterClassMap<Fortune>(m =>
+            {
+                m.MapProperty(f => f.ID).SetElementName("id");
+                m.MapProperty(f => f.Message).SetElementName("message");
+            });
+        }
+
+        public MongoDB(string connectionStringName)
+        {
+            string connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
+
+            MongoClient client = new MongoClient(connectionString);
+            MongoServer server = client.GetServer();
+            MongoDatabase database = server.GetDatabase("hello_world");
+
+            Worlds = database.GetCollection<MongoWorld>("world");
+            Fortunes = database.GetCollection<Fortune>("fortune");
+        }
+    }
+
+    public class MongoWorld : World
+    {
+        [ScriptIgnore]
+        public ObjectId _id { get; set; }
+    }
+}

+ 9 - 4
aspnet/src/Views/Index.cshtml

@@ -17,28 +17,32 @@
     <h3>MySQL</h3>
     <ul>
         <li><em>DB: </em> <a href="/ado/mysql">/ado/mysql</a></li>
-        <li><em>Queries: </em> <a href="/ado/mysql">/ado/mysql?queries=10</a></li>
+        <li><em>Queries: </em> <a href="/ado/mysql?queries=10">/ado/mysql?queries=10</a></li>
         <li><em>Fortunes: </em> <a href="/ado/mysql/fortunes">/ado/mysql/fortunes</a></li>
+        <li><em>Update: </em> <a href="/ado/mysql/update?queries=10">/ado/mysql/update?queries=10</a></li>
     </ul>
     <h3>PostgreSQL</h3>
     <ul>
         <li><em>DB: </em> <a href="/ado/postgresql">/ado/postgresql</a></li>
-        <li><em>Queries: </em> <a href="/ado/postgresql">/ado/postgresql?queries=10</a></li>
+        <li><em>Queries: </em> <a href="/ado/postgresql?queries=10">/ado/postgresql?queries=10</a></li>
         <li><em>Fortunes: </em> <a href="/ado/postgresql/fortunes">/ado/postgresql/fortunes</a></li>
+        <li><em>Update: </em> <a href="/ado/postgresql/update?queries=10">/ado/postgresql/update?queries=10</a></li>
     </ul>
 
     <h2>Entity Framework (ORM)</h2>
     <h3>MySQL</h3>
     <ul>
         <li><em>DB: </em> <a href="/entityframework/mysql">/entityframework/mysql</a></li>
-        <li><em>Queries: </em> <a href="/entityframework/mysql">/entityframework/mysql?queries=10</a></li>
+        <li><em>Queries: </em> <a href="/entityframework/mysql?queries=10">/entityframework/mysql?queries=10</a></li>
         <li><em>Fortunes: </em> <a href="/entityframework/mysql/fortunes">/entityframework/mysql/fortunes</a></li>
+        <li><em>Update: </em> <a href="/entityframework/mysql/update?queries=10">/entityframework/mysql/update?queries=10</a></li>
     </ul>
     <h3>PostgreSQL</h3>
     <ul>
         <li><em>DB: </em> <a href="/entityframework/postgresql">/entityframework/postgresql</a></li>
-        <li><em>Queries: </em> <a href="/entityframework/postgresql">/entityframework/postgresql?queries=10</a></li>
+        <li><em>Queries: </em> <a href="/entityframework/postgresql?queries=10">/entityframework/postgresql?queries=10</a></li>
         <li><em>Fortunes: </em> <a href="/entityframework/postgresql/fortunes">/entityframework/postgresql/fortunes</a></li>
+        <li><em>Update: </em> <a href="/entityframework/postgresql/update?queries=10">/entityframework/postgresql/update?queries=10</a></li>
     </ul>
 
     <h2>MongoDB</h2>
@@ -46,6 +50,7 @@
         <li><em>DB: </em> <a href="/mongodb">/mongodb</a></li>
         <li><em>Queries: </em> <a href="/mongodb?queries=10">/mongodb?queries=10</a></li>
         <li><em>Fortunes: </em> <a href="/mongodb/fortunes">/mongodb/fortunes</a></li>
+        <li><em>Update: </em> <a href="/mongodb/update?queries=10">/mongodb/update?queries=10</a></li>
     </ul>
 
 </body>

+ 2 - 8
aspnet/src/Web.config

@@ -3,15 +3,13 @@
   <configSections>
     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
   </configSections>
-  
   <!-- Connection Strings -->
   <!-- Names are case insensitive -->
   <connectionStrings>
-    <add name="MySQL" connectionString="server=localhost;user id=benchmarkdbuser;password=benchmarkdbpass;database=hello_world" providerName="MySql.Data.MySqlClient"/>
-    <add name="PostgreSQL" connectionString="server=localhost;user id=benchmarkdbuser;password=benchmarkdbpass;database=hello_world" providerName="Npgsql"/>
+    <add name="MySQL" connectionString="server=localhost; user id=benchmarkdbuser; password=benchmarkdbpass; database=hello_world" providerName="MySql.Data.MySqlClient"/>
+    <add name="PostgreSQL" connectionString="server=localhost; user id=benchmarkdbuser; password=benchmarkdbpass; database=hello_world" providerName="Npgsql"/>
     <add name="MongoDB" connectionString="mongodb://localhost"/>
   </connectionStrings>
-  
   <!-- ADO.NET -->
   <system.data>
     <DbProviderFactories>
@@ -20,7 +18,6 @@
       <add name="Npgsql" description="Data Provider for PostgreSQL" invariant="Npgsql" type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0"/>
     </DbProviderFactories>
   </system.data>
-  
   <!-- Entity Framework -->
   <entityFramework>
     <providers>
@@ -28,12 +25,10 @@
       <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql, Version=2.0.12.0"/>
     </providers>
   </entityFramework>
-
   <system.web>
     <!-- Show errors -->
     <customErrors mode="Off"/>
   </system.web>
-
   <!-- Register the application as an HTTP module -->
   <system.webServer>
     <!-- Used by IIS -->
@@ -51,5 +46,4 @@
       <add name="Framework Benchmarks" type="Benchmarks.AspNet.Application"/>
     </httpModules>
   </system.web>
-  
 </configuration>