Files
spark/src/client/sparkcmd/Cmd/ServiceTestCommand.cs
T

139 lines
4.3 KiB
C#

using bts.api;
using bts.api.core;
using bts.api.synthetic;
using bts.test;
using bts.md;
using CommandLine;
using System;
using System.Threading;
namespace bts {
[Verb("servicetest", HelpText="Scratch space for testing services")]
public class ServiceTestOptions {
[Option(nameof(synthetic))]
public bool synthetic { get; set; }
[Option(nameof(user))]
public bool user { get; set; }
[Option(nameof(admin))]
public bool admin { get; set; }
[Option(nameof(latency))]
public bool latency { get; set; }
}
public class ServiceTest {
public static int Run(ServiceTestOptions o) {
if (o.synthetic) {
synthetic_test();
}
if (o.user) {
user_test();
}
if (o.admin) {
admin_test();
}
if (o.latency) {
latency_test();
}
return 0;
}
static string try_get(string[] args, int index) {
if (args.Length < index) {
return args[index];
}
return "";
}
private static void synthetic_test() {
using (var ctx = new ServiceContext()) {
var security = new Security {
Symbol = "ES",
Exchange = Venue.Bts,
Currency = Currency.Usd,
Synthetic = new SyntheticContract {
Name = "Synthetic1",
PriceCalc = SyntheticContract.Types.PriceCalc.Standard,
Legs = {
new ContractLeg {
Security = new Security {
Id = 2,
Symbol = "ES",
// ESH8
Future = new FutureContract {
ExpirationYear = 2018,
ExpirationMonth = 3,
ExpirationDay = 16
}
}
},
new ContractLeg {
Security = new Security {
Id = 5,
Symbol = "ES",
// ESU8
Future = new FutureContract {
ExpirationYear = 2018,
ExpirationMonth = 9,
ExpirationDay = 21
}
}
}
}
}
};
var service = ctx.locate<SyntheticSecurityService>();
try {
var result = service.create_synthetic(security);
if (result.Error == CreateResult.Types.Error.Ok) {
Console.WriteLine(result.Key);
}
}
catch (ApiException e) {
Console.WriteLine(e.Message);
}
}
}
private static void user_test() {
var ctx = new ServiceContext();
var s = ctx.locate<api.user.AuthService>();
var r = new api.user.GetUsersRequest();
var resp = s.get_users(r);
Console.WriteLine("{0}", resp);
}
private static void admin_test() {
var ctx = new ServiceContext();
var s = ctx.locate<api.admin.AdminService>();
var d = s.subscribei_admin_data().Subscribe(Console.WriteLine);
//var resp = s.get_admin_data(new VoidMessage());
//Console.WriteLine("{0}", resp);
while (true) {
Thread.Sleep(100);
}
}
private static void latency_test() {
var ctx = new ServiceContext();
var s = ctx.locate<bts.metrics.ShellLatencyService>();
UInt64 sequence = 0;
var factor = TimeSpan.TicksPerMillisecond / 1000;
while(true) {
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
var reply = s.ping(new metrics.BufferMessage() { Sequence = ++sequence });
sw.Stop();
Console.WriteLine($"{reply.Sequence} - {sw.ElapsedMilliseconds}ms -- {sw.ElapsedTicks/factor} us");
System.Threading.Thread.Sleep(1000);
}
}
}
}