Performance improvements using parallelism
This commit is contained in:
parent
62f2636a69
commit
56a29b6745
|
@ -5,6 +5,8 @@ using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
@ -30,39 +32,42 @@ namespace Core.Main.DataObjects
|
||||||
string html = "";
|
string html = "";
|
||||||
string url = systemConfiguration.GeneralSettings.Application.ProfitTrailerMonitorURL + "api/data?token=" + systemConfiguration.GeneralSettings.Application.ProfitTrailerServerAPIToken;
|
string url = systemConfiguration.GeneralSettings.Application.ProfitTrailerMonitorURL + "api/data?token=" + systemConfiguration.GeneralSettings.Application.ProfitTrailerServerAPIToken;
|
||||||
|
|
||||||
try
|
// Get the data from PT
|
||||||
{
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
request.AutomaticDecompression = DecompressionMethods.GZip;
|
||||||
request.AutomaticDecompression = DecompressionMethods.GZip;
|
request.KeepAlive = true;
|
||||||
|
|
||||||
WebResponse response = request.GetResponse();
|
WebResponse response = request.GetResponse();
|
||||||
Stream dataStream = response.GetResponseStream();
|
Stream dataStream = response.GetResponseStream();
|
||||||
StreamReader reader = new StreamReader(dataStream);
|
StreamReader reader = new StreamReader(dataStream);
|
||||||
html = reader.ReadToEnd();
|
html = reader.ReadToEnd();
|
||||||
reader.Close();
|
reader.Close();
|
||||||
response.Close();
|
response.Close();
|
||||||
|
|
||||||
}
|
|
||||||
catch (System.Exception)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Parse the JSON and build the data sets
|
||||||
PTData rawPTData = JsonConvert.DeserializeObject<PTData>(html);
|
PTData rawPTData = JsonConvert.DeserializeObject<PTData>(html);
|
||||||
if (rawPTData.SellLogData != null)
|
|
||||||
{
|
|
||||||
this.BuildSellLogData(rawPTData.SellLogData, _systemConfiguration);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rawPTData.bbBuyLogData != null)
|
Parallel.Invoke(() =>
|
||||||
{
|
{
|
||||||
this.BuildBuyLogData(rawPTData.bbBuyLogData, _systemConfiguration);
|
if (rawPTData.SellLogData != null)
|
||||||
}
|
{
|
||||||
|
this.BuildSellLogData(rawPTData.SellLogData, _systemConfiguration);
|
||||||
if (rawPTData.DCALogData != null)
|
}
|
||||||
|
},
|
||||||
|
() =>
|
||||||
{
|
{
|
||||||
this.BuildDCALogData(rawPTData.DCALogData, rawPTData.GainLogData, _systemConfiguration);
|
if (rawPTData.bbBuyLogData != null)
|
||||||
}
|
{
|
||||||
|
this.BuildBuyLogData(rawPTData.bbBuyLogData, _systemConfiguration);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
() =>
|
||||||
|
{
|
||||||
|
if (rawPTData.DCALogData != null)
|
||||||
|
{
|
||||||
|
this.BuildDCALogData(rawPTData.DCALogData, rawPTData.GainLogData, _systemConfiguration);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Convert local offset time to UTC
|
// Convert local offset time to UTC
|
||||||
TimeSpan offsetTimeSpan = TimeSpan.Parse(systemConfiguration.GeneralSettings.Application.TimezoneOffset.Replace("+", ""));
|
TimeSpan offsetTimeSpan = TimeSpan.Parse(systemConfiguration.GeneralSettings.Application.TimezoneOffset.Replace("+", ""));
|
||||||
|
@ -168,7 +173,7 @@ namespace Core.Main.DataObjects
|
||||||
sellLogData.Profit = Math.Round(sellLogData.SoldValue - sellLogData.TotalCost, 8);
|
sellLogData.Profit = Math.Round(sellLogData.SoldValue - sellLogData.TotalCost, 8);
|
||||||
|
|
||||||
//Convert Unix Timestamp to Datetime
|
//Convert Unix Timestamp to Datetime
|
||||||
System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,System.DateTimeKind.Utc);
|
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
|
||||||
dtDateTime = dtDateTime.AddSeconds(rsld.soldDate).ToUniversalTime();
|
dtDateTime = dtDateTime.AddSeconds(rsld.soldDate).ToUniversalTime();
|
||||||
|
|
||||||
// Profit Trailer sales are saved in UTC
|
// Profit Trailer sales are saved in UTC
|
||||||
|
@ -256,7 +261,7 @@ namespace Core.Main.DataObjects
|
||||||
}
|
}
|
||||||
|
|
||||||
//Convert Unix Timestamp to Datetime
|
//Convert Unix Timestamp to Datetime
|
||||||
System.DateTime rdldDateTime = new DateTime(1970,1,1,0,0,0,System.DateTimeKind.Utc);
|
System.DateTime rdldDateTime = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
|
||||||
rdldDateTime = rdldDateTime.AddSeconds(rdld.firstBoughtDate).ToUniversalTime();
|
rdldDateTime = rdldDateTime.AddSeconds(rdld.firstBoughtDate).ToUniversalTime();
|
||||||
|
|
||||||
// Profit Trailer bought times are saved in UTC
|
// Profit Trailer bought times are saved in UTC
|
||||||
|
@ -318,7 +323,7 @@ namespace Core.Main.DataObjects
|
||||||
}
|
}
|
||||||
|
|
||||||
//Convert Unix Timestamp to Datetime
|
//Convert Unix Timestamp to Datetime
|
||||||
System.DateTime rpldDateTime = new DateTime(1970,1,1,0,0,0,System.DateTimeKind.Utc);
|
System.DateTime rpldDateTime = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
|
||||||
rpldDateTime = rpldDateTime.AddSeconds(rpld.firstBoughtDate).ToUniversalTime();
|
rpldDateTime = rpldDateTime.AddSeconds(rpld.firstBoughtDate).ToUniversalTime();
|
||||||
|
|
||||||
// Profit Trailer bought times are saved in UTC
|
// Profit Trailer bought times are saved in UTC
|
||||||
|
|
|
@ -9,6 +9,8 @@ using Core.Main.DataObjects.PTMagicData;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Core.ProfitTrailer;
|
using Core.ProfitTrailer;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Core.MarketAnalyzer
|
namespace Core.MarketAnalyzer
|
||||||
{
|
{
|
||||||
|
@ -358,7 +360,10 @@ namespace Core.MarketAnalyzer
|
||||||
// Get Ticks for all markets
|
// Get Ticks for all markets
|
||||||
log.DoLogDebug("Binance - Getting ticks for '" + markets.Count + "' markets");
|
log.DoLogDebug("Binance - Getting ticks for '" + markets.Count + "' markets");
|
||||||
Dictionary<string, List<MarketTick>> marketTicks = new Dictionary<string, List<MarketTick>>();
|
Dictionary<string, List<MarketTick>> marketTicks = new Dictionary<string, List<MarketTick>>();
|
||||||
foreach (string key in markets.Keys)
|
|
||||||
|
Parallel.ForEach( markets.Keys,
|
||||||
|
new ParallelOptions { MaxDegreeOfParallelism = 5 },
|
||||||
|
(key) =>
|
||||||
{
|
{
|
||||||
marketTicks.Add(key, Binance.GetMarketTicks(key, totalTicks, systemConfiguration, log));
|
marketTicks.Add(key, Binance.GetMarketTicks(key, totalTicks, systemConfiguration, log));
|
||||||
|
|
||||||
|
@ -366,7 +371,7 @@ namespace Core.MarketAnalyzer
|
||||||
{
|
{
|
||||||
log.DoLogInfo("Binance - No worries, I am still alive... " + marketTicks.Count + "/" + markets.Count + " markets done...");
|
log.DoLogInfo("Binance - No worries, I am still alive... " + marketTicks.Count + "/" + markets.Count + " markets done...");
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
log.DoLogInfo("Binance - Ticks completed.");
|
log.DoLogInfo("Binance - Ticks completed.");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue