Searching in tbody and jump over not wished stocks improved performance
This commit is contained in:
@ -13,5 +13,6 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DataDomain\DataDomain.csproj" />
|
||||
<ProjectReference Include="..\Helpers\Helpers.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using DataDomain;
|
||||
using Helpers;
|
||||
using OpenQA.Selenium;
|
||||
using System;
|
||||
using System.Collections;
|
||||
@ -64,7 +65,7 @@ namespace BrowserHelper.Extensions
|
||||
ColumnName = columns[colIndex].Text != "" ?
|
||||
columns[colIndex].Text : colIndex.ToString(),
|
||||
ColumnValue = colValue.Text,
|
||||
ColumnSpecialValue = GetControl(colValue)
|
||||
//ColumnSpecialValue = GetControl(colValue)
|
||||
});
|
||||
}
|
||||
colIndex++;
|
||||
@ -78,6 +79,46 @@ namespace BrowserHelper.Extensions
|
||||
return tableDataCollection;
|
||||
}
|
||||
|
||||
public static List<DiTraderStockRow> SimpleReadTablePartly(this IWebElement table, string[] shares, string[] shareHeaders)
|
||||
{
|
||||
var shareCollection = new List<DiTraderStockRow>();
|
||||
var columns = table.FindElements(By.TagName("th"));
|
||||
IWebElement tblBody = table.FindElement(By.XPath(".//tbody"));
|
||||
var rows = tblBody.FindElements(By.TagName("tr"));
|
||||
Dictionary<string, int> columnPos = new Dictionary<string, int>();
|
||||
GlobalStopwatch.PrintSecs("After allocations");
|
||||
for (int i = 0; i < columns.Count; i++)
|
||||
{
|
||||
if (shareHeaders.Contains(columns[i].Text)) columnPos.Add(columns[i].Text, i);
|
||||
}
|
||||
var antal = shares.Length;
|
||||
int rowIndex = 0;
|
||||
GlobalStopwatch.PrintSecs("Before scraplooping");
|
||||
foreach (var row in rows)
|
||||
{
|
||||
var element = row.FindElement(By.XPath(".//td"));
|
||||
if (!shares.Contains(element.Text)) { }
|
||||
else
|
||||
{
|
||||
var colDatas = row.FindElements(By.TagName("td"));
|
||||
if (colDatas.Count != 0 && shares.Contains(colDatas[0].Text))
|
||||
{
|
||||
shareCollection.Add(new DiTraderStockRow
|
||||
{
|
||||
StockName = colDatas[columnPos["Aktie"]].Text,
|
||||
LatestPrice = decimal.Parse(colDatas[columnPos["Senast"]].Text),
|
||||
TimeOfDay = TimeSpan.Parse(colDatas[columnPos["Tid"]].Text)
|
||||
});
|
||||
|
||||
rowIndex++;
|
||||
antal--;
|
||||
}
|
||||
}
|
||||
if (antal < 1) break;
|
||||
}
|
||||
GlobalStopwatch.PrintSecs("After scraplooping");
|
||||
return shareCollection;
|
||||
}
|
||||
|
||||
private static ColumnSpecialValue GetControl(IWebElement columnValue)
|
||||
{
|
||||
@ -131,45 +172,45 @@ namespace BrowserHelper.Extensions
|
||||
}
|
||||
|
||||
|
||||
public static List<DiTraderStockRow> GetCertainStocks(this IWebElement element, string[] shares, string[] shareHeaders)
|
||||
{
|
||||
List<DiTraderStockRow> shareList = new List<DiTraderStockRow>();
|
||||
var reducedList = ReadTablePartly(element, shares, shareHeaders);
|
||||
int rNr = -1;
|
||||
DiTraderStockRow Aktie = null;
|
||||
foreach (var shareRow in reducedList)
|
||||
{
|
||||
if (shareRow.RowNumber != rNr)
|
||||
{
|
||||
if (Aktie != null)
|
||||
{
|
||||
shareList.Add(Aktie);
|
||||
}
|
||||
Aktie = new();
|
||||
rNr = shareRow.RowNumber;
|
||||
}
|
||||
switch (shareRow.ColumnName)
|
||||
{
|
||||
case "Aktie":
|
||||
{
|
||||
Aktie.StockName = shareRow.ColumnValue;
|
||||
break;
|
||||
}
|
||||
case "Senast":
|
||||
{
|
||||
Aktie.LatestPrice = Convert.ToDecimal(shareRow.ColumnValue);
|
||||
break;
|
||||
}
|
||||
case "Tid":
|
||||
{
|
||||
Aktie.TimeOfDay = TimeSpan.Parse(shareRow.ColumnValue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
shareList.Add(Aktie);
|
||||
return shareList;
|
||||
}
|
||||
//public static List<DiTraderStockRow> GetCertainStocks(this IWebElement element, string[] shares, string[] shareHeaders)
|
||||
//{
|
||||
// List<DiTraderStockRow> shareList = new List<DiTraderStockRow>();
|
||||
// var reducedList = ReadTablePartly(element, shares, shareHeaders);
|
||||
// int rNr = -1;
|
||||
// DiTraderStockRow Aktie = null;
|
||||
// foreach (var shareRow in reducedList)
|
||||
// {
|
||||
// if (shareRow.RowNumber != rNr)
|
||||
// {
|
||||
// if (Aktie != null)
|
||||
// {
|
||||
// shareList.Add(Aktie);
|
||||
// }
|
||||
// Aktie = new();
|
||||
// rNr = shareRow.RowNumber;
|
||||
// }
|
||||
// switch (shareRow.ColumnName)
|
||||
// {
|
||||
// case "Aktie":
|
||||
// {
|
||||
// Aktie.StockName = shareRow.ColumnValue;
|
||||
// break;
|
||||
// }
|
||||
// case "Senast":
|
||||
// {
|
||||
// Aktie.LatestPrice = Convert.ToDecimal(shareRow.ColumnValue);
|
||||
// break;
|
||||
// }
|
||||
// case "Tid":
|
||||
// {
|
||||
// Aktie.TimeOfDay = TimeSpan.Parse(shareRow.ColumnValue);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// shareList.Add(Aktie);
|
||||
// return shareList;
|
||||
//}
|
||||
|
||||
|
||||
public static void PerformActionOnCell(this IWebElement element, string targetColumnIndex, string refColumnName, string refColumnValue, string controlToOperate = null)
|
||||
|
||||
100
Helpers/GlobalStopwatch.cs
Normal file
100
Helpers/GlobalStopwatch.cs
Normal file
@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Helpers
|
||||
{
|
||||
public static class GlobalStopwatch
|
||||
{
|
||||
private static Stopwatch Stopwatch { get; set; }
|
||||
|
||||
static GlobalStopwatch()
|
||||
{
|
||||
Stopwatch = new Stopwatch();
|
||||
}
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
if (!Stopwatch.IsRunning)
|
||||
{
|
||||
_previousEllapsedMillis = 0;
|
||||
Stopwatch.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Stop() => Stopwatch.Stop();
|
||||
|
||||
public static void Restart()
|
||||
{
|
||||
_previousEllapsedMillis = 0;
|
||||
Stopwatch.Restart();
|
||||
}
|
||||
|
||||
private static long _previousEllapsedMillis;
|
||||
public static long EllapsedMillisPrevious
|
||||
{
|
||||
get
|
||||
{
|
||||
var ellapsed = EllapsedMillis;
|
||||
var result = ellapsed - _previousEllapsedMillis;
|
||||
_previousEllapsedMillis = ellapsed;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static long EllapsedMillis => Stopwatch.ElapsedMilliseconds;
|
||||
|
||||
public static bool IsRunning
|
||||
{
|
||||
get
|
||||
{
|
||||
return Stopwatch.IsRunning;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Print(string label)
|
||||
{
|
||||
if (label != null)
|
||||
{
|
||||
Debug.WriteLine($"{label}: {EllapsedMillis}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine(EllapsedMillis);
|
||||
}
|
||||
}
|
||||
|
||||
public static void PrintWithLastTime(string label)
|
||||
{
|
||||
if (label != null)
|
||||
{
|
||||
Debug.WriteLine($"{label}: {EllapsedMillisPrevious}");
|
||||
}
|
||||
}
|
||||
public static void PrintSecs(string label)
|
||||
{
|
||||
var secs = Math.Round((decimal)EllapsedMillis / 1000, 2);
|
||||
if (label != null)
|
||||
{
|
||||
Debug.WriteLine($"{label}: {secs}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine(secs);
|
||||
}
|
||||
}
|
||||
|
||||
public static void PrintWithLastTimeSecs(string label)
|
||||
{
|
||||
var secs = Math.Round((decimal)EllapsedMillisPrevious / 1000, 2);
|
||||
if (label != null)
|
||||
{
|
||||
Debug.WriteLine($"{label}: {secs}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -17,6 +17,7 @@
|
||||
<ProjectReference Include="..\BrowserHelper\BrowserHelper.csproj" />
|
||||
<ProjectReference Include="..\DataDomain\DataDomain.csproj" />
|
||||
<ProjectReference Include="..\DatamodelLibrary\DatamodelLibrary.csproj" />
|
||||
<ProjectReference Include="..\Helpers\Helpers.csproj" />
|
||||
<ProjectReference Include="..\StockDal.Interface\StockDAL.Interface.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ using System.Diagnostics;
|
||||
using StockDAL.Interface;
|
||||
using BrowserHelper.Driver;
|
||||
using BrowserHelper.Settings;
|
||||
using Helpers;
|
||||
|
||||
namespace StockDal
|
||||
{
|
||||
@ -245,9 +246,10 @@ namespace StockDal
|
||||
{
|
||||
StockMarketList = new Dictionary<string, DiTraderStockRow>();
|
||||
DumpObjects = new List<DiTraderStockRow>();
|
||||
|
||||
GlobalStopwatch.PrintSecs("After dictionaries setup ");
|
||||
//var connStocks = _stockPersonConnect.GetAllConnectionsByPersId(persId);
|
||||
var groupedStocks = _stockRepository.GetStocksGroupedPerPerson(persId);
|
||||
GlobalStopwatch.PrintSecs("After groupedStocks fetched ");
|
||||
|
||||
var lastGroup = string.Empty;
|
||||
var stockList = new List<string>();
|
||||
@ -259,7 +261,9 @@ namespace StockDal
|
||||
{
|
||||
_testSettings.StockWishes = stockList.ToArray();
|
||||
stockList.Clear();
|
||||
GlobalStopwatch.PrintSecs("Before GetStockStatus");
|
||||
saveStockData( _scrapePage.GetMyStockStatus(lastGroup));
|
||||
GlobalStopwatch.PrintSecs("After GetStockStatus");
|
||||
lastGroup = groupedStock.StockGroup;
|
||||
}
|
||||
else
|
||||
|
||||
@ -31,12 +31,8 @@ namespace StockDAL
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
chooseList.SelectDropDownByText(listText);
|
||||
Thread.Sleep(3000);
|
||||
var stockList = tblList.GetCertainStocks(_testSettings.StockWishes, _testSettings.StocWishCols);
|
||||
//foreach (var stock in stockList)
|
||||
//{
|
||||
// Console.WriteLine($"Aktie: {stock.StockName} Senastepris: {stock.LatestPrice} TidPunkt {stock.TimeOfDay}");
|
||||
//}
|
||||
Thread.Sleep(800);
|
||||
var stockList = tblList.SimpleReadTablePartly(_testSettings.StockWishes, _testSettings.StocWishCols);
|
||||
return stockList;
|
||||
}
|
||||
|
||||
|
||||
@ -141,6 +141,10 @@ namespace StockInfoCore
|
||||
|
||||
private void btnValueView_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(!GlobalStopwatch.IsRunning && GlobalStopwatch.EllapsedMillis>0)
|
||||
GlobalStopwatch.Restart();
|
||||
else GlobalStopwatch.Start();
|
||||
|
||||
if (SelectedPersonId == 0)
|
||||
{
|
||||
MessageBox.Show($"Ingen person vald ({SelectedPersonId})");
|
||||
@ -155,6 +159,8 @@ namespace StockInfoCore
|
||||
_stockWindow.Stocks = _stockMarketRepository.StockMarketList;
|
||||
|
||||
Cursor.Current = DefaultCursor;
|
||||
GlobalStopwatch.Stop();
|
||||
GlobalStopwatch.PrintSecs("Before Show Stockwindow");
|
||||
_stockWindow.ShowDialog();
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using StockDAL.Interface;
|
||||
using Helpers;
|
||||
//
|
||||
namespace StockInfoCore
|
||||
{
|
||||
@ -72,6 +73,8 @@ namespace StockInfoCore
|
||||
}
|
||||
|
||||
lblOwnerName.Text = $"* {ConnectedPerson.FirstName} {ConnectedPerson.LastName} *";
|
||||
GlobalStopwatch.PrintSecs("In reload (New window)");
|
||||
|
||||
txtBuyTotal.Text = BoughtSum.ToString();
|
||||
txtBuyTotal.Refresh();
|
||||
txtCurrValue.Text = CurrentSum.ToString();
|
||||
|
||||
Reference in New Issue
Block a user