Performance improvements using parallelism

This commit is contained in:
djbadders 2019-02-25 23:17:10 +00:00
parent 62f2636a69
commit 56a29b6745
2 changed files with 45 additions and 35 deletions

View File

@ -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;
@ -30,10 +32,10 @@ namespace Core.Main.DataObjects
string html = "";
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);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.KeepAlive = true;
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
@ -42,27 +44,30 @@ namespace Core.Main.DataObjects
reader.Close();
response.Close();
}
catch (System.Exception)
{
throw;
}
// Parse the JSON and build the data sets
PTData rawPTData = JsonConvert.DeserializeObject<PTData>(html);
Parallel.Invoke(() =>
{
if (rawPTData.SellLogData != null)
{
this.BuildSellLogData(rawPTData.SellLogData, _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("+", ""));
@ -168,7 +173,7 @@ namespace Core.Main.DataObjects
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);
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
@ -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

View File

@ -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<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));
@ -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.");