Skip to content

Instantly share code, notes, and snippets.

@Kikimora
Created July 14, 2018 01:23
Show Gist options
  • Save Kikimora/0d9f57f9e62589864ece1a8471eb8dcb to your computer and use it in GitHub Desktop.
Save Kikimora/0d9f57f9e62589864ece1a8471eb8dcb to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using MyExchange.BackOffice.Client;
using MyExchange.Data;
using MyExchange.Test;
using MyExchange.Test.XUnit;
using Serilog;
using Xunit;
using Xunit.Abstractions;
namespace MyExchange.FrontOffice.Test
{
[TestTimeout(30, TimeUnit.Minutes)]
public class MemoryUsageTest : MyExchangeTest
{
private readonly double _initialMemory;
private readonly Random _random;
private const double ToMb = (1024 * 1024d);
protected IFrontOffice Client => Fixture.Client;
protected BackOfficeFixture BackOffice => Fixture.BackOffice;
internal OrderMatchingEngineFixture OrderMatcher => Fixture.OrderMatcher;
internal FrontOfficeFixture FrontOffice => Fixture.FrontOffice;
protected AccountSnapshot Alice;
protected AccountSnapshot Bob;
public MemoryUsageTest(ITestOutputHelper output) : base(output)
{
Fixture = new FrontOfficeTestFixture();
MyExchangeFixture.ResetAll(
BackOffice,
FrontOffice,
OrderMatcher).Wait();
var sw = Stopwatch.StartNew();
Alice = BackOffice.CreateAccount(Guid.NewGuid());
Bob = BackOffice.CreateAccount(Guid.NewGuid());
FrontOffice.LoadAccount(Alice.ExternalId);
FrontOffice.LoadAccount(Bob.ExternalId);
Log.Debug($"Test accounts created {sw.Elapsed.TotalMilliseconds}");
if (Thread.CurrentThread.Name == null)
{
Thread.CurrentThread.Name = "Test Thread";
}
BackOffice.AnnounceUsers(Alice, Bob);
var transfer = new BackOfficeTransferRequest
{
Amount = 10000,
AssetId = "btc",
Status = TransferStatus.Pending,
Type = TransferType.Deposit,
Comment = "Test deposit"
};
transfer.AccountId = Alice.Account.Id;
transfer.ExternalId = Alice.Account.ExternalId;
Fixture.BackOffice.Transfer(Alice, transfer).GetAwaiter().GetResult();
transfer.AssetId = "usdt";
Fixture.BackOffice.Transfer(Alice, transfer).GetAwaiter().GetResult();
transfer.AssetId = "btc";
transfer.AccountId = Bob.Account.Id;
transfer.ExternalId = Bob.Account.ExternalId;
Fixture.BackOffice.Transfer(Bob, transfer).GetAwaiter().GetResult();
transfer.AssetId = "usdt";
Fixture.BackOffice.Transfer(Bob, transfer).GetAwaiter().GetResult();
_random = new Random();
var memory = GC.GetTotalMemory(true) / ToMb;
_initialMemory = memory;
Logger.Debug("!!! Current memory is {mem} Mb", memory);
}
public FrontOfficeTestFixture Fixture { get; set; }
protected override void Dispose(bool disposing)
{
if (disposing)
{
var store = Fixture.FrontOffice.Services.GetRequiredService<IRequestStore>();
store.Clear();
var endMemory = GC.GetTotalMemory(true);
Logger.Debug("!!! Memory grow by {mem}", (endMemory - _initialMemory) / ToMb);
}
base.Dispose(disposing);
Fixture.Dispose();
Fixture = null;
Alice = null;
Bob = null;
if (disposing)
{
var endMemory = GC.GetTotalMemory(true);
Logger.Debug("!!! Memory grow after Dispose by {mem}", (endMemory - _initialMemory) / ToMb);
}
}
[Fact]
public void MemoryUsage()
{
try
{
for (var i = 0; i < 150000; ++i)
{
var action = _random.Next(2);
if (action == 0)
{
Client.NewOrderAsync(x => Bob.Order(x,
OrderSide.Sell,
0.01m,
0.01m,
Markets.BtcUsdt,
0,
OrderType.Limit));
}
else
{
Client.NewOrderAsync(x => Alice.Order(x,
OrderSide.Buy,
0.01m,
0.01m,
Markets.BtcUsdt,
0,
OrderType.Limit));
}
if (i % 1000 == 0)
{
var memory = GC.GetTotalMemory(true) / ToMb;
Logger.Debug("!!! Current memory is {mem} Mb", memory);
}
}
}
catch (Exception e)
{
Logger.Debug(e, "!!! error");
throw;
}
}
[Fact]
public void MemoryOrderCancel()
{
var random = new Random();
var bob = Bob;
for (var i = 0; i < 100000; ++i)
{
var action = random.Next(2);
if (action == 0 || bob.OpenPositions.Count == 0)
{
bob = Client.NewOrder(bob, OrderSide.Sell, 1, 0.01m);
}
else
{
Client.CancelOrder(bob, record => true);
}
if (i % 10000 == 0)
{
var memory = GC.GetTotalMemory(true) / ToMb;
Logger.Debug("Current memory is {mem} Mb", memory);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment