2016-05-18 13:51:47 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.NodeServices;
|
2016-09-01 17:51:53 +03:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2016-05-18 13:51:47 +03:00
|
|
|
|
|
|
|
|
|
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),
|
2016-06-01 18:15:34 +03:00
|
|
|
|
// but is a starting point for comparing the overhead of different hosting models and transports.
|
2016-05-18 13:51:47 +03:00
|
|
|
|
public class Program
|
|
|
|
|
{
|
|
|
|
|
public static void Main(string[] args) {
|
2016-09-01 17:51:53 +03:00
|
|
|
|
// Set up the DI system
|
|
|
|
|
var services = new ServiceCollection();
|
|
|
|
|
services.AddNodeServices(new NodeServicesOptions {
|
|
|
|
|
HostingModel = NodeServicesOptions.DefaultNodeHostingModel,
|
|
|
|
|
ProjectPath = Directory.GetCurrentDirectory(),
|
|
|
|
|
WatchFileExtensions = new string[] {} // Don't watch anything
|
|
|
|
|
});
|
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
// Now instantiate an INodeServices and use it
|
|
|
|
|
using (var nodeServices = serviceProvider.GetRequiredService<INodeServices>()) {
|
2016-05-18 13:51:47 +03:00
|
|
|
|
MeasureLatency(nodeServices).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task MeasureLatency(INodeServices nodeServices) {
|
|
|
|
|
// Ensure the connection is open, so we can measure per-request timings below
|
2016-07-06 20:23:25 +03:00
|
|
|
|
var response = await nodeServices.InvokeAsync<string>("latencyTest", "C#");
|
2016-05-18 13:51:47 +03:00
|
|
|
|
Console.WriteLine(response);
|
2016-06-01 18:15:34 +03:00
|
|
|
|
|
2016-05-18 13:51:47 +03:00
|
|
|
|
// 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++) {
|
2016-07-06 20:23:25 +03:00
|
|
|
|
await nodeServices.InvokeAsync<string>("latencyTest", "C#");
|
2016-05-18 13:51:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|