mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Add LatencyTest project
This commit is contained in:
46
samples/misc/LatencyTest/Program.cs
Executable file
46
samples/misc/LatencyTest/Program.cs
Executable file
@@ -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<string>("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<string>("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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
4
samples/misc/LatencyTest/latencyTest.js
Normal file
4
samples/misc/LatencyTest/latencyTest.js
Normal file
@@ -0,0 +1,4 @@
|
||||
module.exports = function(callback, incomingParam1) {
|
||||
var result = 'Hello, ' + incomingParam1 + '!';
|
||||
callback(/* error */ null, result);
|
||||
}
|
||||
18
samples/misc/LatencyTest/project.json
Executable file
18
samples/misc/LatencyTest/project.json
Executable file
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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-*",
|
||||
|
||||
Reference in New Issue
Block a user