From 22deb2ad28c02fdb0a4a13a8851957cb9d236800 Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Wed, 18 May 2016 11:51:47 +0100 Subject: [PATCH] Add LatencyTest project --- samples/misc/LatencyTest/Program.cs | 46 +++++++++++++++++++ samples/misc/LatencyTest/latencyTest.js | 4 ++ samples/misc/LatencyTest/project.json | 18 ++++++++ .../project.json | 1 + 4 files changed, 69 insertions(+) create mode 100755 samples/misc/LatencyTest/Program.cs create mode 100644 samples/misc/LatencyTest/latencyTest.js create mode 100755 samples/misc/LatencyTest/project.json diff --git a/samples/misc/LatencyTest/Program.cs b/samples/misc/LatencyTest/Program.cs new file mode 100755 index 0000000..8654e2c --- /dev/null +++ b/samples/misc/LatencyTest/Program.cs @@ -0,0 +1,46 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.NodeServices; + +namespace ConsoleApplication +{ + // This project is a micro-benchmark for .NET->Node RPC via NodeServices. It doesn't reflect + // real-world usage patterns (you're not likely to make hundreds of sequential calls like this), + // but is a starting point for comparing the overhead of different hosting models and transports. + public class Program + { + public static void Main(string[] args) { + using (var nodeServices = CreateNodeServices(NodeHostingModel.Http)) { + MeasureLatency(nodeServices).Wait(); + } + } + + private static async Task MeasureLatency(INodeServices nodeServices) { + // Ensure the connection is open, so we can measure per-request timings below + var response = await nodeServices.Invoke("latencyTest", "C#"); + Console.WriteLine(response); + + // Now perform a series of requests, capturing the time taken + const int requestCount = 100; + var watch = Stopwatch.StartNew(); + for (var i = 0; i < requestCount; i++) { + await nodeServices.Invoke("latencyTest", "C#"); + } + + // Display results + var elapsedSeconds = (float)watch.ElapsedTicks / Stopwatch.Frequency; + Console.WriteLine("\nTotal time: {0:F2} milliseconds", 1000 * elapsedSeconds); + Console.WriteLine("\nTime per invocation: {0:F2} milliseconds", 1000 * elapsedSeconds / requestCount); + } + + private static INodeServices CreateNodeServices(NodeHostingModel hostingModel) { + return Configuration.CreateNodeServices(new NodeServicesOptions { + HostingModel = hostingModel, + ProjectPath = Directory.GetCurrentDirectory(), + WatchFileExtensions = new string[] {} // Don't watch anything + }); + } + } +} diff --git a/samples/misc/LatencyTest/latencyTest.js b/samples/misc/LatencyTest/latencyTest.js new file mode 100644 index 0000000..9feb344 --- /dev/null +++ b/samples/misc/LatencyTest/latencyTest.js @@ -0,0 +1,4 @@ +module.exports = function(callback, incomingParam1) { + var result = 'Hello, ' + incomingParam1 + '!'; + callback(/* error */ null, result); +} diff --git a/samples/misc/LatencyTest/project.json b/samples/misc/LatencyTest/project.json new file mode 100755 index 0000000..780b662 --- /dev/null +++ b/samples/misc/LatencyTest/project.json @@ -0,0 +1,18 @@ +{ + "version": "1.0.0-*", + "buildOptions": { + "emitEntryPoint": true + }, + "dependencies": { + "Microsoft.NETCore.App": { + "version": "1.0.0-rc2-3002702", + "type": "platform" + }, + "Microsoft.AspNetCore.NodeServices": "1.0.0-*" + }, + "frameworks": { + "netcoreapp1.0": { + "imports": "dnxcore50" + } + } +} diff --git a/src/Microsoft.AspNetCore.NodeServices/project.json b/src/Microsoft.AspNetCore.NodeServices/project.json index 597a456..22a8074 100644 --- a/src/Microsoft.AspNetCore.NodeServices/project.json +++ b/src/Microsoft.AspNetCore.NodeServices/project.json @@ -6,6 +6,7 @@ }, "authors": [ "Microsoft" ], "dependencies": { + "Microsoft.Extensions.Configuration.Json": "1.0.0-*", "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-*", "Microsoft.Extensions.PlatformAbstractions": "1.0.0-*", "Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0-*",