feat: Change RAbbitMQ Conenction to Pool and Add k6 Test Script for max request per second.

This commit is contained in:
Suphonchai Phoonsawat 2024-08-20 10:37:47 +07:00
parent 0bac3630ec
commit f5355728ab
7 changed files with 186 additions and 44 deletions

View file

@ -1,5 +1,4 @@
using Amazon.S3.Model;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Commands;
using BMA.EHR.Application.Repositories.Leaves.LeaveRequests;
using BMA.EHR.Application.Repositories.Leaves.TimeAttendants;
@ -15,12 +14,10 @@ using BMA.EHR.Leave.Service.DTOs.CheckIn;
using BMA.EHR.Leave.Service.DTOs.DutyTime;
using BMA.EHR.Leave.Service.DTOs.LeaveRequest;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.ObjectPool;
using Newtonsoft.Json;
using Org.BouncyCastle.Ocsp;
using RabbitMQ.Client;
using Serilog;
using Swashbuckle.AspNetCore.Annotations;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
@ -58,6 +55,10 @@ namespace BMA.EHR.Leave.Service.Controllers
private readonly string _bucketName = "check-in";
private readonly ObjectPool<IModel> _objectPool;
private readonly string _fakeCheckInQueue = "fake-bma-checkin-queue";
private readonly string _realCheckInQueue = "bma-checkin-queue";
#endregion
#region " Constuctor and Destructor "
@ -75,7 +76,8 @@ namespace BMA.EHR.Leave.Service.Controllers
AdditionalCheckRequestRepository additionalCheckRequestRepository,
UserCalendarRepository userCalendarRepository,
CommandRepository commandRepository,
LeaveRequestRepository leaveRequestRepository)
LeaveRequestRepository leaveRequestRepository,
ObjectPool<IModel> objectPool)
{
_dutyTimeRepository = dutyTimeRepository;
_context = context;
@ -92,6 +94,7 @@ namespace BMA.EHR.Leave.Service.Controllers
_commandRepository = commandRepository;
_leaveRequestRepository = leaveRequestRepository;
_objectPool = objectPool;
}
#endregion
@ -428,6 +431,7 @@ namespace BMA.EHR.Leave.Service.Controllers
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> CheckInAsync([FromForm] CheckTimeDto data)
{
// prepare data and convert request body and send to queue
var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId);
var currentDate = DateTime.Now;
var checkFileBytes = new byte[0];
@ -458,29 +462,48 @@ namespace BMA.EHR.Leave.Service.Controllers
Token = AccessToken ?? ""
};
// create connection
var factory = new ConnectionFactory()
var channel = _objectPool.Get();
try
{
HostName = _configuration["Rabbit:Host"],
UserName = _configuration["Rabbit:User"],
Password = _configuration["Rabbit:Password"],
};
channel.QueueDeclare(queue: _realCheckInQueue, durable: true, exclusive: false, autoDelete: false, arguments: null);
var serializedObject = JsonConvert.SerializeObject(checkData);
var body = Encoding.UTF8.GetBytes(serializedObject);
// create channel
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "checkin-queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
channel.BasicPublish(exchange: "",
routingKey: _realCheckInQueue,
basicProperties: null,
body: body);
// แปลง Object เป็น JSON สตริง
var serializedObject = JsonConvert.SerializeObject(checkData);
return Success(new { date = currentDate });
}
finally
{
_objectPool.Return(channel);
}
// แปลง JSON สตริงเป็น byte array
var body = Encoding.UTF8.GetBytes(serializedObject);
//// create connection
//var factory = new ConnectionFactory()
//{
// HostName = _configuration["Rabbit:Host"],
// UserName = _configuration["Rabbit:User"],
// Password = _configuration["Rabbit:Password"],
//};
channel.BasicPublish(exchange: "", routingKey: "checkin-queue", basicProperties: null, body: body);
Console.WriteLine($"Send to Queue: {serializedObject}");
//// create channel
//using var connection = factory.CreateConnection();
//using var channel = connection.CreateModel();
//channel.QueueDeclare(queue: "checkin-queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
return Success(new { date = currentDate });
//// แปลง Object เป็น JSON สตริง
//var serializedObject = JsonConvert.SerializeObject(checkData);
//// แปลง JSON สตริงเป็น byte array
//var body = Encoding.UTF8.GetBytes(serializedObject);
//channel.BasicPublish(exchange: "", routingKey: "checkin-queue", basicProperties: null, body: body);
//Console.WriteLine($"Send to Queue: {serializedObject}");
//return Success(new { date = currentDate });
}
/// <summary>
@ -491,7 +514,7 @@ namespace BMA.EHR.Leave.Service.Controllers
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("check-in"), DisableRequestSizeLimit]
[HttpPost("fake-check-in"), DisableRequestSizeLimit]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
@ -499,30 +522,26 @@ namespace BMA.EHR.Leave.Service.Controllers
public ActionResult<ResponseObject> FakeCheckIn([FromBody] FakeCheckTimeDto data)
{
var currentDate = DateTime.Now;
// create connection
var factory = new ConnectionFactory()
var channel = _objectPool.Get();
try
{
HostName = _configuration["Rabbit:Host"],
UserName = _configuration["Rabbit:User"],
Password = _configuration["Rabbit:Password"],
};
channel.QueueDeclare(queue: _fakeCheckInQueue, durable: true, exclusive: false, autoDelete: false, arguments: null);
// create channel
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "fake-checkin-queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
var serializedObject = JsonConvert.SerializeObject(data);
// แปลง Object เป็น JSON สตริง
var serializedObject = JsonConvert.SerializeObject(data);
var body = Encoding.UTF8.GetBytes(serializedObject);
// แปลง JSON สตริงเป็น byte array
var body = Encoding.UTF8.GetBytes(serializedObject);
channel.BasicPublish(exchange: "",
routingKey: _fakeCheckInQueue,
basicProperties: null,
body: body);
channel.BasicPublish(exchange: "", routingKey: "fake-checkin-queue", basicProperties: null, body: body);
Console.WriteLine($"Send to Queue: {serializedObject}");
return Success(new { date = currentDate });
return Success(new { date = currentDate });
}
finally
{
_objectPool.Return(channel);
}
}
/// <summary>