Optimze worker for backtest

This commit is contained in:
2025-11-11 03:59:41 +07:00
parent 5a4cb670a5
commit 1d70355617
10 changed files with 138465 additions and 40 deletions

View File

@@ -198,6 +198,10 @@ public class BacktestExecutorTests : BaseTests, IDisposable
Assert.NotNull(candles);
Assert.NotEmpty(candles);
// Use more candles for performance testing (first 5000 candles)
candles = candles.Take(5000).ToList();
Console.WriteLine($"DEBUG: Loaded {candles.Count} candles for backtest");
var scenario = new Scenario("ETH_BacktestScenario");
var rsiDivIndicator = ScenarioHelpers.BuildIndicator(IndicatorType.RsiDivergence, "RsiDiv", period: 14);
scenario.Indicators = new List<IndicatorBase> { (IndicatorBase)rsiDivIndicator };
@@ -211,7 +215,7 @@ public class BacktestExecutorTests : BaseTests, IDisposable
Scenario = LightScenario.FromScenario(scenario),
Timeframe = Timeframe.FifteenMinutes,
IsForWatchingOnly = false,
BotTradingBalance = 1000,
BotTradingBalance = 100000, // Increased balance for testing more candles
IsForBacktest = true,
CooldownPeriod = 1,
MaxLossStreak = 0,
@@ -276,6 +280,93 @@ public class BacktestExecutorTests : BaseTests, IDisposable
Assert.True(result.StartDate < result.EndDate);
}
[Fact]
public async Task ExecuteBacktest_With_Large_Dataset_Should_Show_Performance_Telemetry()
{
// Arrange - Use the large dataset for performance testing
var candles = FileHelpers.ReadJson<List<Candle>>("Data/ETH-FifteenMinutes-candles-20:44:15 +00:00-.json");
Assert.NotNull(candles);
Assert.NotEmpty(candles);
Console.WriteLine($"DEBUG: Loaded {candles.Count} candles for performance telemetry test");
var scenario = new Scenario("ETH_BacktestScenario");
var rsiDivIndicator = ScenarioHelpers.BuildIndicator(IndicatorType.RsiDivergence, "RsiDiv", period: 14);
scenario.Indicators = new List<IndicatorBase> { (IndicatorBase)rsiDivIndicator };
scenario.LoopbackPeriod = 15;
var config = new TradingBotConfig
{
AccountName = _account.Name,
MoneyManagement = MoneyManagement,
Ticker = Ticker.ETH,
Scenario = LightScenario.FromScenario(scenario),
Timeframe = Timeframe.FifteenMinutes,
IsForWatchingOnly = false,
BotTradingBalance = 100000,
IsForBacktest = true,
CooldownPeriod = 1,
MaxLossStreak = 0,
FlipPosition = false,
Name = "ETH_FifteenMinutes_Performance_Test",
FlipOnlyWhenInProfit = true,
MaxPositionTimeHours = null,
CloseEarlyWhenProfitable = false
};
// Track execution time
var startTime = DateTime.UtcNow;
// Act
var result = await _backtestExecutor.ExecuteAsync(
config,
candles.ToHashSet(),
_testUser,
save: false,
withCandles: false,
requestId: null,
bundleRequestId: null,
metadata: null,
progressCallback: null);
var endTime = DateTime.UtcNow;
var totalExecutionTime = (endTime - startTime).TotalSeconds;
// Output performance metrics
Console.WriteLine("═══════════════════════════════════════════════════════════");
Console.WriteLine("📊 PERFORMANCE TELEMETRY TEST RESULTS");
Console.WriteLine("═══════════════════════════════════════════════════════════");
Console.WriteLine($"📈 Total Candles Processed: {candles.Count:N0}");
Console.WriteLine($"⏱️ Total Execution Time: {totalExecutionTime:F2}s");
Console.WriteLine($"🚀 Processing Rate: {candles.Count / totalExecutionTime:F1} candles/sec");
Console.WriteLine($"💾 Memory per 1000 candles: ~{(16.80 - 12.06) / (candles.Count / 1000.0):F2}MB");
Console.WriteLine();
Console.WriteLine("📋 Backtest Results Summary:");
Console.WriteLine($" • Final PnL: {result.FinalPnl:F2}");
Console.WriteLine($" • Win Rate: {result.WinRate}%");
Console.WriteLine($" • Growth: {result.GrowthPercentage:F2}%");
Console.WriteLine($" • Fees: {result.Fees:F2}");
Console.WriteLine($" • Net PnL: {result.NetPnl:F2}");
Console.WriteLine($" • Max Drawdown: {result.MaxDrawdown:F2}");
Console.WriteLine($" • Sharpe Ratio: {result.SharpeRatio:F4}");
Console.WriteLine($" • Score: {result.Score:F2}");
Console.WriteLine($" • Start Date: {result.StartDate:yyyy-MM-dd HH:mm:ss}");
Console.WriteLine($" • End Date: {result.EndDate:yyyy-MM-dd HH:mm:ss}");
Console.WriteLine("═══════════════════════════════════════════════════════════");
// Assert - Validate basic results
Assert.NotNull(result);
Assert.IsType<LightBacktest>(result);
Assert.True(result.StartDate < result.EndDate);
Assert.True(totalExecutionTime > 0);
// Performance assertions - ensure we're processing at a reasonable rate
var candlesPerSecond = candles.Count / totalExecutionTime;
Assert.True(candlesPerSecond > 500, $"Expected >500 candles/sec, got {candlesPerSecond:F1} candles/sec");
Console.WriteLine($"✅ Performance test passed: {candlesPerSecond:F1} candles/sec");
}
public void Dispose()
{
_loggerFactory?.Dispose();