PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideView/FormStopsTests.cs
2024-04-09 13:48:30 +04:00

144 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using RouteGuideBusinessLogics.BusinessLogics;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.BusinessLogicsContracts;
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;
namespace RouteGuideView
{
/// <summary>
/// Форма с тестами для сущности "Остановка"
/// </summary>
public partial class FormStopsTests : Form
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика
/// </summary>
private readonly IStopLogic _stopLogic;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="stopLogic"></param>
public FormStopsTests(ILogger<FormStopsTests> logger, IStopLogic stopLogic)
{
InitializeComponent();
_logger = logger;
_stopLogic = stopLogic;
numericUpDownInsert.Maximum = 1000;
numericUpDownRead.Maximum = 1000;
numericUpDownDelete.Maximum = 1000;
}
/// <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("StopInsertTest. Count: {Count}", entitiesCount);
try
{
double time = 0;
var stopwatch = new Stopwatch();
for (int i = 1; i <= entitiesCount; i++)
{
var model = new StopBindingModel
{
Id = 0,
Name = $"Остановка {i}",
Street = $"Улица {i}",
Number = i
};
stopwatch.Start();
var operationResult = _stopLogic.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении сущности 'Остановка'. Дополнительная информация в логах.");
}
stopwatch.Stop();
time += stopwatch.ElapsedMilliseconds;
}
labelInsertTime.Text = $"{Convert.ToInt32(time / entitiesCount)} ms";
}
catch (Exception ex)
{
_logger.LogError(ex, "StopInsertTest. 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("StopReadTest. Count: {Count}", entitiesCount);
try
{
var stopwatch = new Stopwatch();
stopwatch.Start();
_stopLogic.ReadList(entitiesCount);
stopwatch.Stop();
labelReadTime.Text = $"{stopwatch.ElapsedMilliseconds} ms";
}
catch (Exception ex)
{
_logger.LogError(ex, "StopReadTest. 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("StopDeleteTest. Count: {Count}", entitiesCount);
try
{
double time = 0;
var stopwatch = new Stopwatch();
for (int i = 1; i <= entitiesCount; i++)
{
stopwatch.Start();
_stopLogic.Delete();
stopwatch.Stop();
time += stopwatch.ElapsedMilliseconds;
}
labelDeleteTime.Text = $"{Convert.ToInt32(time / entitiesCount)} ms";
}
catch (Exception ex)
{
_logger.LogError(ex, "StopDeleteTest. Test failed");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}