Реализовал замеры времени исполнения запросов к БД
This commit is contained in:
parent
4173a05542
commit
59aebfd136
@ -4,10 +4,15 @@ namespace MedicalDatabaseContracts
|
||||
{
|
||||
public interface IStorage<T> where T : AbstractModel
|
||||
{
|
||||
T? Get(int id, out long elapsedMilliseconds);
|
||||
T? Get(int id);
|
||||
List<T> GetAll(out long elapsedMilliseconds);
|
||||
List<T> GetAll();
|
||||
void Insert(T item, out long elapsedMilliseconds);
|
||||
void Insert(T item);
|
||||
void Update(T item, out long elapsedMilliseconds);
|
||||
void Update(T item);
|
||||
void Delete(int id, out long elapsedMilliseconds);
|
||||
void Delete(int id);
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,37 @@ namespace MedicalPostgresqlDatabase
|
||||
{
|
||||
public abstract class AbstractPostgresqlStorage<T> : IStorage<T> where T : AbstractModel
|
||||
{
|
||||
public abstract void Delete(int id);
|
||||
public abstract T? Get(int id);
|
||||
public abstract List<T> GetAll();
|
||||
public abstract void Insert(T item);
|
||||
public abstract void Update(T item);
|
||||
public virtual void Delete(int id)
|
||||
{
|
||||
long elapsedMilliseconds;
|
||||
Delete(id, out elapsedMilliseconds);
|
||||
}
|
||||
public abstract void Delete(int id, out long elapsedMilliseconds);
|
||||
public virtual T? Get(int id)
|
||||
{
|
||||
long elapsedMilliseconds;
|
||||
return Get(id, out elapsedMilliseconds);
|
||||
}
|
||||
public abstract T? Get(int id, out long elapsedMilliseconds);
|
||||
public virtual List<T> GetAll()
|
||||
{
|
||||
long elapsedMilliseconds;
|
||||
return GetAll(out elapsedMilliseconds);
|
||||
}
|
||||
public abstract List<T> GetAll(out long elapsedMilliseconds);
|
||||
public virtual void Insert(T item)
|
||||
{
|
||||
long elapsedMilliseconds;
|
||||
Insert(item, out elapsedMilliseconds);
|
||||
}
|
||||
public abstract void Insert(T item, out long elapsedMilliseconds);
|
||||
public virtual void Update(T item)
|
||||
{
|
||||
long elapsedMilliseconds;
|
||||
Update(item, out elapsedMilliseconds);
|
||||
}
|
||||
public abstract void Update(T item, out long elapsedMilliseconds);
|
||||
|
||||
protected NpgsqlConnection GetConnection()
|
||||
{
|
||||
return new NpgsqlConnection("Host=127.0.0.1;Port=5555;Username=postgres;Database=medicalbase;Password=postgres");
|
||||
|
@ -1,26 +1,42 @@
|
||||
using MedicalDatabaseContracts.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MedicalPostgresqlDatabase.Storages
|
||||
{
|
||||
public class DiagnosesStorage : AbstractPostgresqlStorage<Diagnose>
|
||||
{
|
||||
public override void Delete(int id)
|
||||
private ILogger _logger;
|
||||
public DiagnosesStorage(ILogger<DiagnosesStorage> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override void Delete(int id, out long elapsedMilliseconds)
|
||||
{
|
||||
using var connection = GetConnection();
|
||||
connection.Open();
|
||||
using var cmd = new NpgsqlCommand("DELETE FROM diagnoses WHERE diagnose_id = @id", connection);
|
||||
cmd.Parameters.AddWithValue("@id", id);
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
cmd.ExecuteNonQuery();
|
||||
stopwatch.Stop();
|
||||
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
|
||||
public override Diagnose? Get(int id)
|
||||
public override Diagnose? Get(int id, out long elapsedMilliseconds)
|
||||
{
|
||||
using var connection = GetConnection();
|
||||
connection.Open();
|
||||
using var cmd = new NpgsqlCommand("SELECT * FROM diagnoses WHERE diagnose_id = @id", connection);
|
||||
cmd.Parameters.AddWithValue("@id", id);
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
using var reader = cmd.ExecuteReader();
|
||||
stopwatch.Stop();
|
||||
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
||||
if (reader.Read())
|
||||
{
|
||||
return new Diagnose
|
||||
@ -32,13 +48,17 @@ namespace MedicalPostgresqlDatabase.Storages
|
||||
return null;
|
||||
}
|
||||
|
||||
public override List<Diagnose> GetAll()
|
||||
public override List<Diagnose> GetAll(out long elapsedMilliseconds)
|
||||
{
|
||||
var items = new List<Diagnose>();
|
||||
using var connection = GetConnection();
|
||||
connection.Open();
|
||||
using var cmd = new NpgsqlCommand("SELECT * FROM diagnoses ORDER BY diagnose_id", connection);
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
using var reader = cmd.ExecuteReader();
|
||||
stopwatch.Stop();
|
||||
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
||||
while (reader.Read())
|
||||
{
|
||||
items.Add(new Diagnose
|
||||
@ -50,23 +70,31 @@ namespace MedicalPostgresqlDatabase.Storages
|
||||
return items;
|
||||
}
|
||||
|
||||
public override void Insert(Diagnose item)
|
||||
public override void Insert(Diagnose item, out long elapsedMilliseconds)
|
||||
{
|
||||
using var connection = GetConnection();
|
||||
connection.Open();
|
||||
using var cmd = new NpgsqlCommand("INSERT INTO diagnoses (diagnose_id, name) VALUES ((nextval('diagnoses_id_seq')), @name)", connection);
|
||||
cmd.Parameters.AddWithValue("@name", item.Name);
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
cmd.ExecuteNonQuery();
|
||||
stopwatch.Stop();
|
||||
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
|
||||
public override void Update(Diagnose item)
|
||||
public override void Update(Diagnose item, out long elapsedMilliseconds)
|
||||
{
|
||||
using var connection = GetConnection();
|
||||
connection.Open();
|
||||
using var cmd = new NpgsqlCommand("UPDATE diagnoses SET name = @name WHERE diagnose_id = @id", connection);
|
||||
cmd.Parameters.AddWithValue("@id", item.Id);
|
||||
cmd.Parameters.AddWithValue("@name", item.Name);
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
cmd.ExecuteNonQuery();
|
||||
stopwatch.Stop();
|
||||
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,13 +30,14 @@ namespace MedicalView
|
||||
SetStatusStripText("Загрузка диагнозов...");
|
||||
try
|
||||
{
|
||||
var items = _diagnosesStorage.GetAll();
|
||||
long elapsed;
|
||||
var items = _diagnosesStorage.GetAll(out elapsed);
|
||||
|
||||
dataGridView.DataSource = items;
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
|
||||
_logger.LogInformation("Список диагнозов загружен успешно");
|
||||
SetStatusStripText($"Готово. Загружено записей: {items.Count}");
|
||||
SetStatusStripText($"Готово. Загружено записей: {items.Count}, время загрузки {elapsed} мск");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user