public static async Task DuplicateAsync(bool parallel) { var mongoDriver = new MongoDbDriver( ConfigurationManager.ConnectionStrings["MongoDb"].ConnectionString, "User"); var mongoDriver2 = new MongoDbDriver( ConfigurationManager.ConnectionStrings["MongoDb"].ConnectionString, "User2"); await mongoDriver2.Database.DropCollectionAsync("User2"); var timer = new Stopwatch(); timer.Start(); var tasks = Enumerable.Range(0, 128).Select(async i => { var uids = await LoadUids(mongoDriver.Collection, i); foreach (var uid in uids) { var user = await mongoDriver.LoadUserAsync(uid); await mongoDriver2.CreateUserAsync(uid, user); } return uids.Count; }); var totalCount = await WaitForComplete(tasks, parallel); var elapsed = timer.Elapsed.TotalSeconds; var rowPerSec = totalCount / timer.Elapsed.TotalSeconds; Console.WriteLine($"Elapsed: {(int)elapsed}s RowPerSec: {(int)rowPerSec}"); }
public static async Task DuplicateAsync(bool parallel) { var mongoDriver = new MongoDbDriver( ConfigurationManager.ConnectionStrings["MongoDb"].ConnectionString, "User"); var mongoDriver2 = new MongoDbDriver( ConfigurationManager.ConnectionStrings["MongoDb"].ConnectionString, "User2"); await mongoDriver2.Database.DropCollectionAsync("User2"); var timer = new Stopwatch(); timer.Start(); var tasks = Enumerable.Range(0, 128).Select(async i => { var uids = await LoadUids(mongoDriver.Collection, i); foreach (var uid in uids) { var user = await mongoDriver.LoadUserAsync(uid); await mongoDriver2.CreateUserAsync(uid, user); } return(uids.Count); }); var totalCount = await WaitForComplete(tasks, parallel); var elapsed = timer.Elapsed.TotalSeconds; var rowPerSec = totalCount / timer.Elapsed.TotalSeconds; Console.WriteLine($"Elapsed: {(int)elapsed}s RowPerSec: {(int)rowPerSec}"); }
public static async Task MigrateAsync() { var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDb"].ConnectionString); sqlConnection.Open(); var sqlDriver = new SqlDriver(MsSqlProvider.Instance, sqlConnection); var mongoDriver = new MongoDbDriver( ConfigurationManager.ConnectionStrings["MongoDb"].ConnectionString, "User"); mongoDriver.Database.DropCollectionAsync("User").Wait(); for (int i = 0; i < 128; i++) { var uid0 = i * 0x1000000; var uid1 = uid0 + 0xFFFFFF; var uids = new List <int>(); mongoDriver.Database.GetCollection <BsonDocument>("User"); var sql = $"SELECT [Uid] FROM tblUser WHERE [Uid] BETWEEN ${uid0} AND ${uid1}"; using (var command = new SqlCommand(sql, sqlConnection)) { using (var reader = command.ExecuteReader()) { while (await reader.ReadAsync()) { var uid = (int)reader.GetValue(0); uids.Add(uid); } } } var timer = new Stopwatch(); timer.Start(); Console.Write($"[Step ({i + 1}/128)] Count:{uids.Count} "); foreach (var uid in uids) { var user = await sqlDriver.LoadUserAsync(uid); await mongoDriver.CreateUserAsync(uid, user); } timer.Stop(); var elapsed = timer.Elapsed.TotalSeconds; if (uids.Count > 0 && elapsed > 0) { var rowPerSec = uids.Count / timer.Elapsed.TotalSeconds; Console.WriteLine($"Elapsed: {(int)elapsed}s RowPerSec: {(int)rowPerSec}"); } else { Console.WriteLine(); } } }
public static async Task MigrateAsync() { var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDb"].ConnectionString); sqlConnection.Open(); var sqlDriver = new SqlDriver(MsSqlProvider.Instance, sqlConnection); var mongoDriver = new MongoDbDriver( ConfigurationManager.ConnectionStrings["MongoDb"].ConnectionString, "User"); mongoDriver.Database.DropCollectionAsync("User").Wait(); for (int i = 0; i < 128; i++) { var uid0 = i * 0x1000000; var uid1 = uid0 + 0xFFFFFF; var uids = new List<int>(); mongoDriver.Database.GetCollection<BsonDocument>("User"); var sql = $"SELECT [Uid] FROM tblUser WHERE [Uid] BETWEEN ${uid0} AND ${uid1}"; using (var command = new SqlCommand(sql, sqlConnection)) { using (var reader = command.ExecuteReader()) { while (await reader.ReadAsync()) { var uid = (int)reader.GetValue(0); uids.Add(uid); } } } var timer = new Stopwatch(); timer.Start(); Console.Write($"[Step ({i + 1}/128)] Count:{uids.Count} "); foreach (var uid in uids) { var user = await sqlDriver.LoadUserAsync(uid); await mongoDriver.CreateUserAsync(uid, user); } timer.Stop(); var elapsed = timer.Elapsed.TotalSeconds; if (uids.Count > 0 && elapsed > 0) { var rowPerSec = uids.Count / timer.Elapsed.TotalSeconds; Console.WriteLine($"Elapsed: {(int)elapsed}s RowPerSec: {(int)rowPerSec}"); } else { Console.WriteLine(); } } }
public static async Task SaveComplexAsync(bool parallel, bool full) { var mongoDriver2 = new MongoDbDriver( ConfigurationManager.ConnectionStrings["MongoDb"].ConnectionString, "User2"); var timer = new Stopwatch(); timer.Start(); var tasks = Enumerable.Range(0, 128).Select(async i => { var uids = await LoadUids(mongoDriver2.Collection, i); foreach (var uid in uids) { var user = await mongoDriver2.LoadUserAsync(uid); user.SetDefaultTracker(); user.Data.Gold += 1; user.Data.Gold -= 1; foreach (var key in user.Items.Keys.ToList()) { var item = user.Items[key]; user.Items[key] = item; if (full == false) { break; } } foreach (var key in user.Teams.Keys.ToList()) { var team = user.Teams[key]; user.Teams[key] = team; if (full == false) { break; } } await mongoDriver2.SaveUserAsync(uid, user.Tracker); } return(uids.Count); }); var totalCount = await WaitForComplete(tasks, parallel); var elapsed = timer.Elapsed.TotalSeconds; var rowPerSec = totalCount / timer.Elapsed.TotalSeconds; Console.WriteLine($"Elapsed: {(int)elapsed}s RowPerSec: {(int)rowPerSec}"); }
public static async Task SaveComplexAsync(bool parallel, bool full) { var mongoDriver2 = new MongoDbDriver( ConfigurationManager.ConnectionStrings["MongoDb"].ConnectionString, "User2"); var timer = new Stopwatch(); timer.Start(); var tasks = Enumerable.Range(0, 128).Select(async i => { var uids = await LoadUids(mongoDriver2.Collection, i); foreach (var uid in uids) { var user = await mongoDriver2.LoadUserAsync(uid); user.SetDefaultTracker(); user.Data.Gold += 1; user.Data.Gold -= 1; foreach (var key in user.Items.Keys.ToList()) { var item = user.Items[key]; user.Items[key] = item; if (full == false) break; } foreach (var key in user.Teams.Keys.ToList()) { var team = user.Teams[key]; user.Teams[key] = team; if (full == false) break; } await mongoDriver2.SaveUserAsync(uid, user.Tracker); } return uids.Count; }); var totalCount = await WaitForComplete(tasks, parallel); var elapsed = timer.Elapsed.TotalSeconds; var rowPerSec = totalCount / timer.Elapsed.TotalSeconds; Console.WriteLine($"Elapsed: {(int)elapsed}s RowPerSec: {(int)rowPerSec}"); }