测试mongodb批量写入。
This commit is contained in:
parent
4ad10606ff
commit
86edf4290a
@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using DotNetCore.CAP;
|
||||
using DotNetCore.CAP.Messages;
|
||||
using FreeSql;
|
||||
using JiShe.CollectBus.Ammeters;
|
||||
using JiShe.CollectBus.Common;
|
||||
@ -14,6 +15,7 @@ using JiShe.CollectBus.Common.Enums;
|
||||
using JiShe.CollectBus.Common.Extensions;
|
||||
using JiShe.CollectBus.Common.Helpers;
|
||||
using JiShe.CollectBus.Common.Models;
|
||||
using JiShe.CollectBus.Enums;
|
||||
using JiShe.CollectBus.GatherItem;
|
||||
using JiShe.CollectBus.IotSystems.Devices;
|
||||
using JiShe.CollectBus.IotSystems.MessageIssueds;
|
||||
@ -40,16 +42,18 @@ namespace JiShe.CollectBus.ScheduledMeterReading
|
||||
private readonly ILogger<BasicScheduledMeterReadingService> _logger;
|
||||
private readonly ICapPublisher _capBus;
|
||||
private readonly IMeterReadingRecordRepository _meterReadingRecordsRepository;
|
||||
|
||||
private readonly IRepository<Device, Guid> _deviceRepository;
|
||||
|
||||
public BasicScheduledMeterReadingService(
|
||||
ILogger<BasicScheduledMeterReadingService> logger,
|
||||
ICapPublisher capBus,
|
||||
IRepository<Device, Guid> deviceRepository,
|
||||
IMeterReadingRecordRepository meterReadingRecordsRepository)
|
||||
{
|
||||
_capBus = capBus;
|
||||
_logger = logger;
|
||||
_meterReadingRecordsRepository = meterReadingRecordsRepository;
|
||||
_deviceRepository = deviceRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1058,5 +1062,78 @@ namespace JiShe.CollectBus.ScheduledMeterReading
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 测试MongoDB插入数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task TestBatchMongoDBInsert(int totalRecords = 100_00)
|
||||
{
|
||||
int fetchSize = 500;
|
||||
int processedSize = 4;
|
||||
var tasks = new List<Task<Device>>();
|
||||
var timer = Stopwatch.StartNew();
|
||||
List<Device> devices = new List<Device>();
|
||||
for (long timestamp = 0; timestamp < totalRecords; timestamp++)
|
||||
{
|
||||
var device = new Device(timestamp.ToString(),$"client{timestamp}",DateTime.Now, DateTime.Now, DeviceStatus.Online);
|
||||
devices.Add(device);
|
||||
|
||||
if (timestamp % fetchSize == 0)
|
||||
{
|
||||
await _deviceRepository.InsertManyAsync(devices);
|
||||
devices = new List<Device>();
|
||||
}
|
||||
}
|
||||
timer.Stop();
|
||||
var message = $"批量插入{totalRecords} 条记录,总共耗时:{timer.ElapsedMilliseconds}毫秒";
|
||||
_logger.LogError(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试MongoDB插入数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task TestBatchMongoDBInsert2(int totalRecords = 100_000)
|
||||
{
|
||||
int fetchSize = 500;
|
||||
int processedSize = 4;
|
||||
var tasks = new List<Task<Device>>();
|
||||
var timer = Stopwatch.StartNew();
|
||||
List<Device> devices = new List<Device>();
|
||||
for (long timestamp = 0; timestamp < totalRecords; timestamp++)
|
||||
{
|
||||
var device = new Device(timestamp.ToString(), $"client{timestamp}", DateTime.Now, DateTime.Now, DeviceStatus.Online);
|
||||
devices.Add(device);
|
||||
}
|
||||
|
||||
await _deviceRepository.InsertManyAsync(devices);
|
||||
|
||||
timer.Stop();
|
||||
var message = $"批量插入{totalRecords} 条记录,总共耗时:{timer.ElapsedMilliseconds}毫秒";
|
||||
_logger.LogError(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试MongoDB插入数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task TestSingleMongoDBInsert(int totalRecords = 100_00)
|
||||
{
|
||||
int fetchSize = 500;
|
||||
int processedSize = 4;
|
||||
var tasks = new List<Task<Device>>();
|
||||
var timer = Stopwatch.StartNew();
|
||||
List<Device> devices = new List<Device>();
|
||||
for (long timestamp = 0; timestamp < totalRecords; timestamp++)
|
||||
{
|
||||
var device = new Device(timestamp.ToString(), $"client{timestamp}", DateTime.Now, DateTime.Now, DeviceStatus.Online);
|
||||
devices.Add(device);
|
||||
await _deviceRepository.InsertAsync(device);
|
||||
}
|
||||
timer.Stop();
|
||||
var message = $"单次插入{totalRecords} 条记录,总共耗时:{timer.ElapsedMilliseconds}毫秒";
|
||||
_logger.LogError(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ namespace JiShe.CollectBus.ScheduledMeterReading
|
||||
{
|
||||
|
||||
public EnergySystemScheduledMeterReadingService(ILogger<EnergySystemScheduledMeterReadingService> logger,
|
||||
ICapPublisher capBus, IMeterReadingRecordRepository _meterReadingRecordsRepository) :base(logger, capBus, _meterReadingRecordsRepository)
|
||||
ICapPublisher capBus, IMeterReadingRecordRepository _meterReadingRecordsRepository, IRepository<Device, Guid> deviceRepository) :base(logger, capBus, deviceRepository, _meterReadingRecordsRepository)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@ -34,16 +34,17 @@
|
||||
"CorsOrigins": "http://localhost:4200,http://localhost:3100"
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"Default": "mongodb://admin:admin02023@118.190.144.92:37117,118.190.144.92:37119,118.190.144.92:37120/JiSheCollectBus?authSource=admin&maxPoolSize=400&minPoolSize=10&waitQueueTimeoutMS=5000",
|
||||
"Default": "mongodb://admin:lixiao1980@8.148.224.127:27017,8.148.224.21:27017,8.138.38.208:27017/JiSheCollectBus?authSource=admin&maxPoolSize=400&minPoolSize=10&waitQueueTimeoutMS=5000",
|
||||
//"Kafka": "121.42.242.91:29092,121.42.242.91:39092,121.42.242.91:49092",
|
||||
"Kafka": "8.148.227.21:9092,8.148.224.127:9092,8.138.38.208:9092",
|
||||
"PrepayDB": "server=118.190.144.92;database=jishe.sysdb;uid=sa;pwd=admin@2023;Encrypt=False;Trust Server Certificate=False",
|
||||
"EnergyDB": "server=118.190.144.92;database=db_energy;uid=sa;pwd=admin@2023;Encrypt=False;Trust Server Certificate=False"
|
||||
},
|
||||
"Redis": {
|
||||
"Configuration": "118.190.144.92:6379,syncTimeout=30000,abortConnect=false,connectTimeout=30000,allowAdmin=true",
|
||||
"Configuration": "8.138.38.208:6379,syncTimeout=30000,abortConnect=false,connectTimeout=30000,allowAdmin=true,password=lixiao1980",
|
||||
"DefaultDB": "14",
|
||||
"HangfireDB": "15"
|
||||
|
||||
},
|
||||
"Jwt": {
|
||||
"Audience": "JiShe.CollectBus",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user