sharding/samples/Sample.BulkConsole/Program.cs

135 lines
5.5 KiB
C#
Raw Normal View History

using EFCore.BulkExtensions;
2021-09-08 10:44:29 +08:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sample.BulkConsole.DbContexts;
using Sample.BulkConsole.Entities;
using ShardingCore;
using ShardingCore.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ShardingCore.Bootstrapers;
using ShardingCore.Extensions.ShardingPageExtensions;
2021-09-08 10:44:29 +08:00
namespace Sample.BulkConsole
{
class Program
{
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddLogging();
2021-10-11 20:58:55 +08:00
services.AddShardingDbContext<MyShardingDbContext>(
2021-11-04 17:42:26 +08:00
(conn, o) => o.UseSqlServer(conn).UseLoggerFactory(efLogger))
.Begin(o =>
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.AutoTrackEntity = true;
})
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=MyOrderSharding;Integrated Security=True;")
.AddShardingTableRoute(op=> {
2021-09-08 10:44:29 +08:00
op.AddShardingTableRoute<OrderVirtualRoute>();
}).End();
2021-09-08 10:44:29 +08:00
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetService<IShardingBootstrapper>().Start();
using (var serviceScope = serviceProvider.CreateScope())
{
var myShardingDbContext = serviceScope.ServiceProvider.GetService<MyShardingDbContext>();
2021-09-08 10:44:29 +08:00
if (!myShardingDbContext.Set<Order>().Any())
{
var begin = DateTime.Now.Date.AddDays(-3);
var now = DateTime.Now;
var current = begin;
ICollection<Order> orders = new LinkedList<Order>();
int i = 0;
while (current < now)
{
orders.Add(new Order()
{
Id = i.ToString(),
OrderNo = $"orderno-" + i.ToString(),
Seq = i,
CreateTime = current
});
i++;
current = current.AddMilliseconds(100);
}
var startNew = Stopwatch.StartNew();
var bulkShardingEnumerable = myShardingDbContext.BulkShardingTableEnumerable(orders);
2021-09-08 10:44:29 +08:00
startNew.Stop();
Console.WriteLine($"订单总数:{i}条,myShardingDbContext.BulkShardingEnumerable(orders)用时:{startNew.ElapsedMilliseconds}毫秒");
startNew.Restart();
foreach (var dataSourceMap in bulkShardingEnumerable)
2021-09-08 10:44:29 +08:00
{
dataSourceMap.Key.BulkInsert(dataSourceMap.Value.ToList());
2021-09-08 10:44:29 +08:00
}
startNew.Stop();
Console.WriteLine($"订单总数:{i}条,myShardingDbContext.BulkInsert(orders)用时:{startNew.ElapsedMilliseconds}毫秒");
Console.WriteLine("ok");
}
2021-09-17 13:16:52 +08:00
2021-09-15 16:50:00 +08:00
var b = DateTime.Now.Date.AddDays(-3);
2021-09-08 10:44:29 +08:00
var queryable = myShardingDbContext.Set<Order>().Where(o => o.CreateTime >= b).OrderBy(o => o.CreateTime);
2021-09-08 10:44:29 +08:00
var startNew1 = Stopwatch.StartNew();
startNew1.Restart();
var list2 = queryable.Take(1000).ToList();
startNew1.Stop();
Console.WriteLine($"获取1000条用时:{startNew1.ElapsedMilliseconds}毫秒");
startNew1.Restart();
var list = queryable.Take(10).ToList();
startNew1.Stop();
Console.WriteLine($"获取10条用时:{startNew1.ElapsedMilliseconds}毫秒");
startNew1.Restart();
var list1 = queryable.Take(100).ToList();
startNew1.Stop();
Console.WriteLine($"获取100条用时:{startNew1.ElapsedMilliseconds}毫秒");
startNew1.Restart();
var list3 = queryable.Take(10000).ToList();
startNew1.Stop();
Console.WriteLine($"获取100000条用时:{startNew1.ElapsedMilliseconds}毫秒");
startNew1.Restart();
var list4 = queryable.Take(20000).ToList();
startNew1.Stop();
Console.WriteLine($"获取20000条用时:{startNew1.ElapsedMilliseconds}毫秒");
int skip = 0, take = 1000;
for (int i = 20000; i < 30000; i++)
2021-09-08 10:44:29 +08:00
{
skip = take * i;
startNew1.Restart();
var shardingPagedResult = queryable.ToShardingPage(i+1, take);
startNew1.Stop();
Console.WriteLine($"流式分页skip:[{skip}],take:[{take}]耗时用时:{startNew1.ElapsedMilliseconds}毫秒");
}
Console.WriteLine("ok");
}
}
}
}