From 56a29b67456a79bc663f7865e7b8cc599e4d54bc Mon Sep 17 00:00:00 2001 From: djbadders <34887832+djbadders@users.noreply.github.com> Date: Mon, 25 Feb 2019 23:17:10 +0000 Subject: [PATCH] Performance improvements using parallelism --- Core/DataObjects/ProfitTrailerData.cs | 71 ++++++++++++++------------- Core/MarketAnalyzer/Binance.cs | 9 +++- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/Core/DataObjects/ProfitTrailerData.cs b/Core/DataObjects/ProfitTrailerData.cs index dae645f..65a4601 100644 --- a/Core/DataObjects/ProfitTrailerData.cs +++ b/Core/DataObjects/ProfitTrailerData.cs @@ -5,6 +5,8 @@ using System.Globalization; using System.Linq; using System.Net; using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; @@ -22,47 +24,50 @@ namespace Core.Main.DataObjects private PTMagicConfiguration _systemConfiguration = null; private TransactionData _transactionData = null; private DateTimeOffset _dateTimeNow = Constants.confMinDate; - + public ProfitTrailerData(PTMagicConfiguration systemConfiguration) { _systemConfiguration = systemConfiguration; string html = ""; string url = systemConfiguration.GeneralSettings.Application.ProfitTrailerMonitorURL + "api/data?token=" + systemConfiguration.GeneralSettings.Application.ProfitTrailerServerAPIToken; - - try - { - HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); - request.AutomaticDecompression = DecompressionMethods.GZip; - WebResponse response = request.GetResponse(); - Stream dataStream = response.GetResponseStream(); - StreamReader reader = new StreamReader(dataStream); - html = reader.ReadToEnd(); - reader.Close(); - response.Close(); + // Get the data from PT + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.AutomaticDecompression = DecompressionMethods.GZip; + request.KeepAlive = true; - } - catch (System.Exception) - { - throw; - } + WebResponse response = request.GetResponse(); + Stream dataStream = response.GetResponseStream(); + StreamReader reader = new StreamReader(dataStream); + html = reader.ReadToEnd(); + reader.Close(); + response.Close(); + // Parse the JSON and build the data sets PTData rawPTData = JsonConvert.DeserializeObject(html); - if (rawPTData.SellLogData != null) - { - this.BuildSellLogData(rawPTData.SellLogData, _systemConfiguration); - } - if (rawPTData.bbBuyLogData != null) + Parallel.Invoke(() => { - this.BuildBuyLogData(rawPTData.bbBuyLogData, _systemConfiguration); - } - - if (rawPTData.DCALogData != null) + if (rawPTData.SellLogData != null) + { + this.BuildSellLogData(rawPTData.SellLogData, _systemConfiguration); + } + }, + () => { - 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 TimeSpan offsetTimeSpan = TimeSpan.Parse(systemConfiguration.GeneralSettings.Application.TimezoneOffset.Replace("+", "")); @@ -165,11 +170,11 @@ namespace Core.Main.DataObjects double soldValueRaw = (sellLogData.SoldAmount * sellLogData.SoldPrice); double soldValueAfterFees = soldValueRaw - (soldValueRaw * (rsld.fee / 100)); sellLogData.SoldValue = soldValueAfterFees; - sellLogData.Profit = Math.Round(sellLogData.SoldValue - sellLogData.TotalCost, 8); + sellLogData.Profit = Math.Round(sellLogData.SoldValue - sellLogData.TotalCost, 8); //Convert Unix Timestamp to Datetime - System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,System.DateTimeKind.Utc); - dtDateTime = dtDateTime.AddSeconds(rsld.soldDate).ToUniversalTime(); + System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc); + dtDateTime = dtDateTime.AddSeconds(rsld.soldDate).ToUniversalTime(); // Profit Trailer sales are saved in UTC DateTimeOffset ptSoldDate = DateTimeOffset.Parse(dtDateTime.Year.ToString() + "-" + dtDateTime.Month.ToString("00") + "-" + dtDateTime.Day.ToString("00") + "T" + dtDateTime.Hour.ToString("00") + ":" + dtDateTime.Minute.ToString("00") + ":" + dtDateTime.Second.ToString("00"), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); @@ -256,7 +261,7 @@ namespace Core.Main.DataObjects } //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(); // Profit Trailer bought times are saved in UTC @@ -318,7 +323,7 @@ namespace Core.Main.DataObjects } //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(); // Profit Trailer bought times are saved in UTC diff --git a/Core/MarketAnalyzer/Binance.cs b/Core/MarketAnalyzer/Binance.cs index 083911f..519c3c6 100644 --- a/Core/MarketAnalyzer/Binance.cs +++ b/Core/MarketAnalyzer/Binance.cs @@ -9,6 +9,8 @@ using Core.Main.DataObjects.PTMagicData; using Newtonsoft.Json; using Core.ProfitTrailer; using System.Net; +using System.Threading; +using System.Threading.Tasks; namespace Core.MarketAnalyzer { @@ -358,7 +360,10 @@ namespace Core.MarketAnalyzer // Get Ticks for all markets log.DoLogDebug("Binance - Getting ticks for '" + markets.Count + "' markets"); Dictionary> marketTicks = new Dictionary>(); - foreach (string key in markets.Keys) + + Parallel.ForEach( markets.Keys, + new ParallelOptions { MaxDegreeOfParallelism = 5 }, + (key) => { 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 - Ticks completed.");