using System; using System.Diagnostics; namespace MysqlHandling { class GamePlayerManager { public int m_realmId; public static int randNumOffset = 0; // m_index 取值范围 1-10 public int m_index; // 每个管理器随机生成10个玩家的数据, 后续DB性能测试就从这10个玩家中随机, 否则测试数据内存占用太大 private GamePlayer[] m_playerList = new GamePlayer[10]; public Stopwatch m_watch = new Stopwatch(); public Simulation m_simulation; public long m_startUid; public long m_currUid; // 测试数量 public int m_testCount; public long m_insertMs; public long m_getMs; public long m_updteMs; public long m_deleteMs; public GamePlayerManager(int testCount, int realmId, int index) { m_realmId = realmId; m_index = index; m_testCount = testCount; // 每个线程独占1亿的uid范围 m_startUid = index * 100000000 + Config.m_DBConfig.uidOffset; m_currUid = m_startUid; string dbname = Config.m_DBConfig.dbname; string dbIp = Config.m_DBConfig.ip; string user = Config.m_DBConfig.user; string password = Config.m_DBConfig.password; int dbtype = Config.m_DBConfig.dbtype; m_simulation = new Simulation(); m_simulation.DBinit(dbname, dbIp, user, password, dbtype, index); MakePlayerTemplate(); } // 生成10个玩家的数据模版 public void MakePlayerTemplate() { Random rand = new Random(DateTime.Now.Millisecond); long startUid = m_index * 1000; for (int i = 0; i < m_playerList.Length; ++i) { int bagCount = rand.Next(200, 512); int heroCount = rand.Next(200, 512); int equipmentCount = rand.Next(200, 1024); m_playerList[i] = new GamePlayer(startUid + i, m_realmId, bagCount, heroCount, equipmentCount); } } public void TestAll() { if (Config.m_DBConfig.testInsert == 1) { TestInsert(); } if (Config.m_DBConfig.testGet == 1) { //TestGet(); } //TestUpdateNoBson(); if (Config.m_DBConfig.testUpdate == 1) { TestUpdate(); } if (Config.m_DBConfig.testDelete == 1) { TestDelete(); } } public void TestInsert() { //EasyLog.WriteInfo($"{m_index} test insert data"); m_watch.Reset(); m_watch.Start(); int succ = 0; int count = m_testCount; try { for (int i = 0; i < count;) { for (int j = 0; j < 10 && i < count; j++) { if (m_simulation.Insert(m_currUid++, m_playerList[j].m_role)) { succ++; } i++; } } } catch (Exception e) { EasyLog.WriteErr(e.Message); EasyLog.WriteErr(e.StackTrace); } m_watch.Stop(); m_insertMs = m_watch.ElapsedMilliseconds; EasyLog.WriteInfo($"{m_index} startUid {m_startUid}, currUid {m_currUid} insert count {count}, succ {succ} time {m_watch.ElapsedMilliseconds} ms"); } public void TestGet() { //EasyLog.WriteInfo($"{m_index} test get data"); m_watch.Reset(); m_watch.Start(); int succ = 0; int count = m_testCount; try { long startUid = m_startUid; for (int i = 0; i < count;) { for (int j = 0; j < 10 && i < count; j++) { if (m_simulation.Get(startUid++)) { succ++; } i++; } } } catch (Exception e) { EasyLog.WriteErr(e.Message); EasyLog.WriteErr(e.StackTrace); } m_watch.Stop(); m_getMs = m_watch.ElapsedMilliseconds; EasyLog.WriteInfo($"{m_index} get count {count}, succ {succ} time {m_watch.ElapsedMilliseconds} ms"); } public void TestUpdateNoBson() { //EasyLog.WriteInfo($"{m_index} test update data"); m_watch.Reset(); m_watch.Start(); int succ = 0; int count = m_testCount; try { long startUid = m_startUid; for (int i = 0; i < count;) { for (int j = 9; j >= 0 && i < count; j--) { if (m_simulation.UpdateNoBson(startUid++, m_playerList[j].m_role)) { succ++; } i++; } } } catch (Exception e) { EasyLog.WriteErr(e.Message); EasyLog.WriteErr(e.StackTrace); } m_watch.Stop(); m_updteMs = m_watch.ElapsedMilliseconds; EasyLog.WriteInfo($"{m_index} updateNoBson count {count}, succ {succ} time {m_watch.ElapsedMilliseconds} ms"); } public void TestUpdate() { //EasyLog.WriteInfo($"{m_index} test update data"); m_watch.Reset(); m_watch.Start(); int succ = 0; int count = m_testCount; try { long startUid = m_startUid; for (int i = 0; i < count;) { for (int j = 0; j < 10 && i < count; j++) { if (m_simulation.UpdateBson(startUid++, m_playerList[j].m_role)) { succ++; } i++; } } } catch (Exception e) { EasyLog.WriteErr(e.Message); EasyLog.WriteErr(e.StackTrace); } m_watch.Stop(); m_updteMs = m_watch.ElapsedMilliseconds; EasyLog.WriteInfo($"{m_index} update count {count}, succ {succ} time {m_watch.ElapsedMilliseconds} ms"); } public void TestDelete() { //EasyLog.WriteInfo($"{m_index} test delete data"); m_watch.Reset(); m_watch.Start(); int succ = 0; int count = m_testCount; try { long startUid = m_startUid; for (int i = 0; i < count; i++) { m_simulation.Delete(startUid++); succ++; } } catch (Exception e) { EasyLog.WriteErr(e.Message); EasyLog.WriteErr(e.StackTrace); } m_watch.Stop(); m_deleteMs = m_watch.ElapsedMilliseconds; EasyLog.WriteInfo($"{m_index} delete count {count}, succ {succ} time {m_watch.ElapsedMilliseconds} ms"); } } }