145 lines
5.4 KiB
C#
145 lines
5.4 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using RouteGuideContracts.BindingModels;
|
||
using RouteGuideContracts.BusinessLogicsContracts;
|
||
using RouteGuideContracts.SearchModels;
|
||
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
|
||
{
|
||
/// <summary>
|
||
/// Форма с тестами для сущности "Водитель"
|
||
/// </summary>
|
||
public partial class FormDriversTests : Form
|
||
{
|
||
/// <summary>
|
||
/// Логгер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
|
||
/// <summary>
|
||
/// Бизнес-логика
|
||
/// </summary>
|
||
private readonly IDriverLogic _driverLogic;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="logger"></param>
|
||
/// <param name="driverLogic"></param>
|
||
public FormDriversTests(ILogger<FormDriversTests> logger, IDriverLogic driverLogic)
|
||
{
|
||
InitializeComponent();
|
||
_logger = logger;
|
||
_driverLogic = driverLogic;
|
||
|
||
numericUpDownInsert.Maximum = 10000;
|
||
numericUpDownRead.Maximum = 10000;
|
||
numericUpDownDelete.Maximum = 10000;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Создать"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonInsert_Click(object sender, EventArgs e)
|
||
{
|
||
int entitiesCount = Convert.ToInt32(numericUpDownInsert.Value);
|
||
_logger.LogInformation("DriverInsertTest. Count: {Count}", entitiesCount);
|
||
try
|
||
{
|
||
double time = 0;
|
||
var stopwatch = new Stopwatch();
|
||
for (int i = 1; i <= entitiesCount; i++)
|
||
{
|
||
var model = new DriverBindingModel
|
||
{
|
||
Id = 0,
|
||
FullName = $"Водитель {i}",
|
||
Phone = $"Номер {i}",
|
||
Experience = 1
|
||
};
|
||
|
||
stopwatch.Restart();
|
||
var operationResult = _driverLogic.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, "DriverInsertTest. Test failed");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Получить"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonRead_Click(object sender, EventArgs e)
|
||
{
|
||
int entitiesCount = Convert.ToInt32(numericUpDownRead.Value);
|
||
_logger.LogInformation("DriverReadTest. Count: {Count}", entitiesCount);
|
||
try
|
||
{
|
||
var stopwatch = new Stopwatch();
|
||
stopwatch.Start();
|
||
_driverLogic.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, "DriverReadTest. Test failed");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Удалить"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonDelete_Click(object sender, EventArgs e)
|
||
{
|
||
int entitiesCount = Convert.ToInt32(numericUpDownDelete.Value);
|
||
_logger.LogInformation("DriverDeleteTest. Count: {Count}", entitiesCount);
|
||
try
|
||
{
|
||
double time = 0;
|
||
var stopwatch = new Stopwatch();
|
||
for (int i = 1; i <= entitiesCount; i++)
|
||
{
|
||
stopwatch.Restart();
|
||
_driverLogic.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, "DriverDeleteTest. Test failed");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
}
|