using Microsoft.Extensions.Logging;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.BusinessLogicsContracts;
using RouteGuideDataModels.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
namespace RouteGuideView
{
///
/// Форма с тестами для сущности "Транспорт"
///
public partial class FormTransportTests : Form
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Бизнес-логика для транспорта
///
private readonly ITransportLogic _transportLogic;
///
/// Бизнес-логика для водителей
///
private readonly IDriverLogic _driverLogic;
///
/// Конструктор
///
///
///
///
public FormTransportTests(ILogger logger, ITransportLogic transportLogic, IDriverLogic driverLogic)
{
InitializeComponent();
_logger = logger;
_transportLogic = transportLogic;
_driverLogic = driverLogic;
numericUpDownInsert.Maximum = 10000;
numericUpDownRead.Maximum = 10000;
numericUpDownDelete.Maximum = 10000;
}
///
/// Кнопка "Создать"
///
///
///
private void buttonInsert_Click(object sender, EventArgs e)
{
int entitiesCount = Convert.ToInt32(numericUpDownInsert.Value);
_logger.LogInformation("TransportInsertTest. Count: {Count}", entitiesCount);
try
{
double time = 0;
var stopwatch = new Stopwatch();
object driverId = _driverLogic.ReadList(1)![0].Id;
for (int i = 1; i <= entitiesCount; i++)
{
var model = new TransportBindingModel
{
Id = 0,
License = i.ToString(),
Type = TransportType.Маршрутка,
Capacity = i,
DriverId = driverId
};
stopwatch.Restart();
var operationResult = _transportLogic.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении сущности 'Транспорт'. Дополнительная информация в логах.");
}
stopwatch.Stop();
time += stopwatch.ElapsedMilliseconds;
}
labelInsertTime.Text = $"Total time: {Convert.ToInt32(time)} ms / Average time: {Convert.ToInt32(time / entitiesCount)} ms";
}
catch (Exception ex)
{
_logger.LogError(ex, "TransportInsertTest. Test failed");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// Кнопка "Получить"
///
///
///
private void buttonRead_Click(object sender, EventArgs e)
{
int entitiesCount = Convert.ToInt32(numericUpDownRead.Value);
_logger.LogInformation("TransportReadTest. Count: {Count}", entitiesCount);
try
{
var stopwatch = new Stopwatch();
stopwatch.Start();
_transportLogic.ReadList(entitiesCount);
stopwatch.Stop();
labelReadTime.Text = $"Total time: {Convert.ToInt32(stopwatch.ElapsedMilliseconds)} ms / Average time: {Convert.ToInt32(stopwatch.ElapsedMilliseconds / entitiesCount)} ms";
}
catch (Exception ex)
{
_logger.LogError(ex, "TransportReadTest. Test failed");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// Кнопка "Удалить"
///
///
///
private void buttonDelete_Click(object sender, EventArgs e)
{
int entitiesCount = Convert.ToInt32(numericUpDownDelete.Value);
_logger.LogInformation("TransportDeleteTest. Count: {Count}", entitiesCount);
try
{
double time = 0;
var stopwatch = new Stopwatch();
for (int i = 1; i <= entitiesCount; i++)
{
stopwatch.Restart();
_transportLogic.Delete();
stopwatch.Stop();
time += stopwatch.ElapsedMilliseconds;
}
labelDeleteTime.Text = $"Total time: {Convert.ToInt32(time)} ms / Average time: {Convert.ToInt32(time / entitiesCount)} ms";
}
catch (Exception ex)
{
_logger.LogError(ex, "TransportDeleteTest. Test failed");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}