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,39 +32,42 @@ namespace Core.Main.DataObjects
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;
// 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();
StreamReader reader = new StreamReader(dataStream);
html = reader.ReadToEnd();
reader.Close();
response.Close();
}
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<PTData>(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("+", ""));
@ -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.");