Browse Source

Update CSharp/Nancy

- At least nancy-mono runs now.
- Added plaintext route test.
- Separate query test from db test.
- Temporarily remove libevent2 test. (Currently, this one does not work.)
blee-techempower 9 years ago
parent
commit
ba86655810

+ 13 - 25
frameworks/CSharp/nancy/NancyModules/DbModule.cs

@@ -8,6 +8,7 @@
     using MySql.Data.MySqlClient;
     using Nancy;
 
+    
     public class DbModule : NancyModule
     {
         public static string MYSQL_CONNECTION_STRING;
@@ -16,35 +17,22 @@
         {
             MYSQL_CONNECTION_STRING = ConfigurationManager.AppSettings["ConnectionString.MySQL"];
         }
-
+        
+        /**
+         * NOTE:
+         * Return a single World objects as JSON, selected randomly from the World
+         * table.  Assume the table has 10,000 rows.
+         */
         public DbModule() : base("/db")
         {
             Get["/{queries?1}"] = paramz =>
             {
-                var queries = (int)paramz.queries;
-                
-                var random = new Random();
-                using (var db = new MySqlConnection(MYSQL_CONNECTION_STRING))
-                {
-                    db.Open();
-
-                    if (queries == 1)
-                        return GetRandomWorld(db, random);
-                    else
-                    {
-                        var worldCount = queries > 500 ? 500 : queries;
-                        worldCount = worldCount < 1 ? 1 : worldCount;
-
-                        // NOTE: Experiment with running the DB requests in parallel, on both Mono and Windows CLRs.
-                        var worlds = new World[worldCount];
-
-                        for (int i = 0; i < worldCount; ++i)
-                        {
-                            worlds[i] = GetRandomWorld(db, random);
-                        }
-                        return worlds;
-                    }
-                }
+              var random = new Random();
+              using (var db = new MySqlConnection(MYSQL_CONNECTION_STRING))
+              {
+                db.Open();
+                return Response.AsJson(GetRandomWorld(db, random));
+              }
             };
         }
 

+ 2 - 0
frameworks/CSharp/nancy/NancyModules/NancyModules.csproj

@@ -52,8 +52,10 @@
     <Reference Include="System.Data" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="PlainModule.cs" />
     <Compile Include="DbModule.cs" />
     <Compile Include="JsonModule.cs" />
+    <Compile Include="QueryModule.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>

+ 15 - 0
frameworks/CSharp/nancy/NancyModules/PlainModule.cs

@@ -0,0 +1,15 @@
+namespace NancyModules 
+{
+  using Nancy;
+
+  public class PlainModule : NancyModule
+  {
+    public PlainModule() : base("plaintext")
+    {
+      Get["/"] = x =>
+      {
+        return "Hello, World!";
+      };
+    }
+  }
+}

+ 91 - 0
frameworks/CSharp/nancy/NancyModules/QueryModule.cs

@@ -0,0 +1,91 @@
+namespace NancyModules
+{
+    using System;
+    using System.Configuration;
+    using System.Data;
+    using System.Linq;
+    using Dapper;
+    using MySql.Data.MySqlClient;
+    using Nancy;
+
+    public class QueryModule : NancyModule
+    {
+        public static string MYSQL_CONNECTION_STRING;
+        
+        static QueryModule()
+        {
+            MYSQL_CONNECTION_STRING = ConfigurationManager.AppSettings["ConnectionString.MySQL"];
+        }
+        /**
+         * Query:
+         * Return a list of World objects as JSON, selected randomly from the World
+         * table.  Assume the table has 10,000 rows.
+         */
+        public QueryModule() : base("/query")
+        {
+            Get["/{queries?1}"] = paramz =>
+            {
+                var queries = (int)paramz.queries;
+                
+                var random = new Random();
+                using (var db = new MySqlConnection(MYSQL_CONNECTION_STRING))
+                {
+                    db.Open();
+
+                    if (queries == 1)
+                        return Response.AsJson( GetRandomWorld(db, random) );
+                    else
+                    {
+                        var worldCount = queries > 500 ? 500 : queries;
+                        worldCount = worldCount < 1 ? 1 : worldCount;
+
+                        // NOTE: Experiment with running the DB requests in parallel, on both Mono and Windows CLRs.
+                        var worlds = new World[worldCount];
+
+                        for (int i = 0; i < worldCount; ++i)
+                        {
+                            worlds[i] = GetRandomWorld(db, random);
+                        }
+                        return Response.AsJson( worlds );
+                    }
+                }
+            };
+
+            Get["/{name}"] = paramz =>
+            {
+                 var queries = (int)paramz.queries;
+                
+                var random = new Random();
+                using (var db = new MySqlConnection(MYSQL_CONNECTION_STRING))
+                {
+                    db.Open();
+
+                    if (queries == 1)
+                        return Response.AsJson( GetRandomWorld(db, random) );
+                    else
+                    {
+                        var worldCount = queries > 500 ? 500 : queries;
+                        worldCount = worldCount < 1 ? 1 : worldCount;
+
+                        // NOTE: Experiment with running the DB requests in parallel, on both Mono and Windows CLRs.
+                        var worlds = new World[worldCount];
+
+                        for (int i = 0; i < worldCount; ++i)
+                        {
+                            worlds[i] = GetRandomWorld(db, random);
+                        }
+                        return Response.AsJson( worlds );
+                    }
+                }
+
+            };
+        }
+
+        private World GetRandomWorld(IDbConnection db, Random random)
+        {
+            var id = random.Next(1, 10001);
+            return db.Query<World>("SELECT id, randomNumber FROM world WHERE id = @id", new { id = id }).Single();
+        }
+    }
+
+}

+ 5 - 22
frameworks/CSharp/nancy/benchmark_config.json

@@ -3,9 +3,10 @@
   "tests": [{
     "default": {
       "setup_file": "setup_iis",
+      "plaintext_url":"/plaintext",
       "json_url": "/json",
       "db_url": "/db",
-      "query_url": "/db/",
+      "query_url": "/query/",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Micro",
@@ -23,9 +24,10 @@
     },
     "mono": {
       "setup_file": "setup_nginx",
+      "plaintext_url":"/plaintext",
       "json_url": "/json",
       "db_url": "/db",
-      "query_url": "/db/",
+      "query_url": "/query/",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Micro",
@@ -40,26 +42,7 @@
       "display_name": "nancy",
       "notes": "",
       "versus": ""
-    },
-    "libevent2": {
-      "setup_file": "setup_libevent",
-      "json_url": "/json",
-      "db_url": "/db",
-      "query_url": "/db/",
-      "port": 8080,
-      "approach": "Realistic",
-      "classification": "Micro",
-      "database": "MySQL",
-      "framework": "nancy",
-      "language": "C#",
-      "orm": "Raw",
-      "platform": "Mono",
-      "webserver": "nginx",
-      "os": "Linux",
-      "database_os": "Linux",
-      "display_name": "nancy-libevent",
-      "notes": "",
-      "versus": ""
     }
+
   }]
 }

+ 2 - 2
frameworks/CSharp/nancy/nginx.conf

@@ -19,8 +19,8 @@ http {
 
         location / {
             fastcgi_pass mono;
-            include /usr/local/nginx/conf/fastcgi_params;
+            include nginx.osenv.conf; # read os env from this file
             fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
         }
     }
-}
+}

+ 7 - 0
frameworks/CSharp/nancy/setup_nginx.sh

@@ -19,6 +19,13 @@ for port in $(seq ${port_start} $port_end); do
 done
 conf+="}"
 
+# Store path of fastcgi_params dynamically in a file called "nginx.osenv.conf". 
+# The reason why I do this is Ngix "include" cannot recognize variables. (Well known issue of Nginx)
+# To use OS Environment at ngix.conf 3rd party library or perl module is needed
+# Current approach is one trick to solve the problem without utilize those 3rd party libraries.
+echo "include $IROOT/nginx/conf/fastcgi_params;" > $TROOT/nginx.osenv.conf
+
+
 echo -e $conf > $TROOT/nginx.upstream.conf
 ${NGINX_HOME}/sbin/nginx -c $TROOT/nginx.conf -g "worker_processes '"${MAX_THREADS}"';"
 

+ 7 - 8
toolset/setup/linux/languages/mono.sh

@@ -16,8 +16,7 @@ echo "deb http://jenkins.mono-project.com/repo/debian sid main" | sudo tee /etc/
 sudo apt-get update
 
 # Find the most recent snapshot
-#SNAPSHOT=$(apt-cache search 'mono-snapshot-.*-assemblies' | cut -d'-' -f3 | tail -1)
-SNAPSHOT="20150202010831"
+SNAPSHOT=$(apt-cache search 'mono-snapshot-.*-assemblies' | cut -d'-' -f3 | tail -1)
 
 # save environment
 
@@ -40,13 +39,13 @@ rm -rf $MONO_HOME && mkdir -p $MONO_HOME
 fw_apt_to_iroot mono-snapshot-$SNAPSHOT
 fw_apt_to_iroot mono-snapshot-$SNAPSHOT-assemblies mono-snapshot-$SNAPSHOT
 
-# Simplify paths
-mv $MONO_HOME/opt/mono-*/* $MONO_HOME
-file $MONO_HOME/bin/* | grep "POSIX shell script" | awk -F: '{print $1}' | xargs sed -i "s|/opt/mono-$SNAPSHOT|$MONO_HOME|g"
-sed -i "s|/opt/mono-$SNAPSHOT|$MONO_HOME|g" $MONO_HOME/lib/pkgconfig/*.pc $MONO_HOME/etc/mono/config
 
+# Simplify paths
+sudo mv $MONO_HOME/opt/mono-*/* $MONO_HOME
+file $MONO_HOME/bin/* | grep "POSIX shell script" | awk -F: '{print $1}' | xargs sudo sed -i "s|/opt/mono-$SNAPSHOT|$MONO_HOME|g"
+sudo sed -i "s|/opt/mono-$SNAPSHOT|$MONO_HOME|g" $MONO_HOME/lib/pkgconfig/*.pc $MONO_HOME/etc/mono/config
 echo "mozroots --import --sync" >> $IROOT/mono.installing
 
-mv $IROOT/mono.installing $IROOT/mono.installed
+sudo mv $IROOT/mono.installing $IROOT/mono.installed
 
-source $IROOT/mono.installed
+source $IROOT/mono.installed

+ 1 - 1
toolset/setup/linux/languages/xsp.sh

@@ -15,7 +15,7 @@ git checkout e272a2c006211b6b03be2ef5bbb9e3f8fefd0768
 # build
 ./autogen.sh --prefix=$MONO_HOME --disable-docs
 make
-make install
+sudo make install
 
 # cleanup
 cd ..