public void TestDataBufferPool() { try { DateTime d = default(DateTime); DateTime d1 = JinRiDBFacade.Instance.GetDateTimeNow(); } catch { } int count = 0; IDataBufferPool pool = new DataBufferPool((x) => { List <object> arr = ((DataBuffer)x).GetList <object>(); Interlocked.Add(ref count, arr.Count); Debug.WriteLine(count); }, false); for (int i = 0; i < 1000000; i++) { pool.Write(i); //if (i % 100 == 0) //System.out.println(i); } pool.Flush(); Thread.Sleep(15000); Assert.Equals(1000000, count); }
public void TestWrite() { DataBufferPool pool = new DataBufferPool(dataBufferSize, dataBufferCount, false); pool.OnDataHandle += WriteFile; //1.写据数 var task1 = new TaskFactory().StartNew(() => Write(pool, dataBufferSize * dataBufferCount + 1)); Thread.Sleep(50); //2.查看bufferpool状态 var task2 = new TaskFactory().StartNew(() => Show(pool)); Task.WaitAll(task1, task2); Debug.WriteLine("结束 ,BufferCount=" + pool.BufferCount + " ,BufferCurrentDataCount=" + pool.BufferCurrentDataCount); //3.断言 pool.BufferCount.ShouldBe(dataBufferSize); //4.消费数据 var task3 = pool.TimerFlushAsync(); //刷新数据 //Debug.WriteLine("刷新数据..."); //pool.Flush(); //测试是否重复写入池中 new TaskFactory().StartNew(() => Empty(pool)); Thread.Sleep(1000 * 60 * 60 * 1); }
/// <summary> /// 写数据 /// </summary> /// <param name="pool"></param> /// <param name="count"></param> private void Write(DataBufferPool pool, int count) { for (int i = 0; i < count; i++) { pool.Write(new DataObj { Index = i, Des = "测试" + i, CreateTime = DateTime.Now }); } }
/// <summary> /// 客户端初始化 /// </summary> /// <param name="poolCount"></param> /// <param name="bufferSize"></param> public static void Init(int poolCount, int bufferSize) { _varListPool = new VarListPool(poolCount); _sendBufferPool = new DataBufferPool(poolCount, bufferSize); _sendBigBufferPool = new DataBufferPool(100, bufferSize * 10); _packageHeadPool = new PackageHeadPool(poolCount); //_recvBufferPool = new DataBufferPool(poolCount, bufferSize); _poolOfRecSendEventArgs = new SocketAsyncEventArgsPool(); }
//static SocketHelper() //{ // _varListPool = new VarListPool(CfgMgr.BasicCfg.MaxNumberOfConnections); //} /// <summary> /// 服务端初始化 /// </summary> public static void Init() { _sendBufferPool = new DataBufferPool(CfgMgr.BasicCfg.MaxNumberOfConnections + CfgMgr.BasicCfg.ExcessSaeaObjectsInPool, CfgMgr.BasicCfg.BufferSize); _sendBigBufferPool = new DataBufferPool(CfgMgr.BasicCfg.BigBufferCount, CfgMgr.BasicCfg.BufferSize * 10); _packageHeadPool = new PackageHeadPool(CfgMgr.BasicCfg.MaxNumberOfConnections); //_recvBufferPool = new DataBufferPool(CfgMgr.BasicCfg.MaxNumberOfConnections + CfgMgr.BasicCfg.ExcessSaeaObjectsInPool,CfgMgr.BasicCfg.BufferSize); _poolOfRecSendEventArgs = new SocketAsyncEventArgsPool(); _poolOfAcceptEventArgs = new SocketAsyncEventArgsPool(); }
/// <summary> /// 生产数据 /// </summary> private Task ProduceAsync() { DataBufferPool pool = new DataBufferPool(AppSetting.DataBufferPoolSize, AppSetting.DataBufferSize); pool.OnDataHandle += OnWrite; pool.TimerFlushAsync(); //1.写据数 //var task1 = new TaskFactory().StartNew(() => Write(pool, AppSetting.DataBufferPoolSize * AppSetting.DataBufferSize + 1)); var task1 = new TaskFactory().StartNew(() => Write(pool, DataBufferPoolSize * DataBufferSize + 1)); return(task1); }
private void Show(DataBufferPool pool) { int i = 1; while (true) { Debug.WriteLine("第" + i + "轮 ,BufferCount=" + pool.BufferCount + " ,BufferCurrentDataCount=" + pool.BufferCurrentDataCount); Thread.Sleep(5); i++; if (pool.BufferCount == dataBufferSize) { break; } } Debug.WriteLine("第" + i + "轮 ,BufferCount=" + pool.BufferCount + " ,BufferCurrentDataCount=" + pool.BufferCurrentDataCount); }
public bool Init() { usingStateLock = new object(); if (usingStateLock == null) { return(false); } rcvAsyncEventArgs = new SocketAsyncEventArgs(); if (rcvAsyncEventArgs == null) { return(false); } else { rcvAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(NetModuleMgr.GetInstance().NetIOComplete); rcvAsyncEventArgs.UserToken = this; } sendAsyncEventArgs = new SocketAsyncEventArgs(); if (sendAsyncEventArgs == null) { return(false); } else { sendAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(NetModuleMgr.GetInstance().NetIOComplete); sendAsyncEventArgs.UserToken = this; } rcvDataBuffer = DataBufferPool.GetInstance().MallocRcvBuffer(); if (rcvDataBuffer == null) { return(false); } sendDataBufferMgr = DataBufferPool.GetInstance().MallocSendBufferMgr(); if (sendDataBufferMgr == null) { DataBufferPool.GetInstance().FreeRcvBuffer(rcvDataBuffer); rcvDataBuffer = null; return(false); } return(true); }
// 关闭对象 public void Shutdown() { CloseAllSession(); NetSessionPool.GetInstance().Release(); DataBufferPool.GetInstance().Release(); MsgBufferMgr.GetInstance().Release(); MsgBufferPool.GetInstance().Release(); if (netSessionIDList != null) { netSessionIDList.Clear(); netSessionIDList = null; } if (addOrDelEventList != null) { addOrDelEventList.ReleaseWriteReadList(); addOrDelEventList = null; } }
public void Release() { CloseSocket(); usingStateLock = null; rcvAsyncEventArgs = null; sendAsyncEventArgs = null; if (rcvDataBuffer != null) { DataBufferPool.GetInstance().FreeRcvBuffer(rcvDataBuffer); rcvDataBuffer = null; } if (sendDataBufferMgr != null) { DataBufferPool.GetInstance().FreeSendBufferMgr(sendDataBufferMgr); sendDataBufferMgr = null; } }
public void TestWrite2() { DataBufferPool pool = new DataBufferPool(dataBufferSize, dataBufferCount); pool.OnDataHandle += WriteFile; pool.TimerFlushAsync(); //1.写据数 var task1 = new TaskFactory().StartNew(() => Write(pool, dataBufferSize * dataBufferCount + 1)); Task.WaitAll(task1); Debug.WriteLine("结束 ,BufferCount=" + pool.BufferCount + " ,BufferCurrentDataCount=" + pool.BufferCurrentDataCount); //测试是否重复写入池中 new TaskFactory().StartNew(() => Empty(pool)); Thread.Sleep(1000 * 60 * 60 * 1); }
/// <summary> /// 判断数据是否重复写到池中 /// </summary> /// <param name="pool"></param> private void Empty(DataBufferPool pool) { while (true) { if (pool.IsPoolEmpty) { break; } } Task.Delay(1000 * 5).Wait(); //断言 tmpList.Count.ShouldBe(dataBufferSize * dataBufferCount + 1); new TaskFactory().StartNew(() => { File.AppendAllText(CreateFile(), JsonConvert.SerializeObject( tmpList.ToLookup(x => x.Index) .Select(x => new { Index = x.Key, Count = x.Count() }) .OrderByDescending(x => x.Count).OrderBy(x => x.Index).ToList())); }); }
static void Main(string[] args) { Derived d = new Derived(); Console.Read(); for (int i = 0; i < 100000; i++) { T2 t2 = new T2 { ID = i, T2Name = "fasdfasd" + i }; t2.Insert(); LocalDBTest lt = new LocalDBTest { Age = 10, ID = 1, Name = "zhoulin" + i, T2S = new List <T2> { t2 } }; lt.Insert(); Thread.Sleep(100); } //List<LocalDBTest> lts = LocalDB.Select<LocalDBTest>(); //if (lts == null || !lts.Any()) //{ // T2 t2 = new T2 { ID = 23, T2Name = "fasdfasd" }; // t2.Insert(); // LocalDBTest lt = new LocalDBTest { Age = 10, ID = 1, Name = "zhoulin", T2S = new List<T2> { t2 } }; // lt.Insert(); //} //else //{ // LocalDBTest lt = lts.First(); // if (lt.remark == null) // { // lt.remark = new List<string>(); // } // if (lt.T2S == null) // { // lt.T2S = new List<T2> { new T2 { ID = 23, T2Name = "fasdfasd" } }; // } // lt.remark.Add(new Random().Next().ToString()); // lt.Update(); // lt = new LocalDBTest { Age = 10, ID = 1, Name = "zhoulin_" + new Random().Next() }; // lt.Insert(); //} Person pson = new Person { id = "1", Name = "zl" }; Thread.Sleep(10000 * 323); string rrr = SerializerHelper.JsonSerializer(pson); pson = SerializerHelper.JsonDeserialize <Person>("{\"id\":\"1\",\"Int_id\":3,\"Name\":\"zl\"}"); DataBufferPool <int> dp = new DataBufferPool <int>(tls => { Console.WriteLine(tls.Count + "|" + string.Join(",", tls)); }, 2000); Random r = new Random(); for (int i = 0; i < 10000000; i++) { dp.AddItem(i % 10); Thread.Sleep(r.Next(10, 300)); } Console.ReadLine(); Parallel.Invoke( () => ExecCrawler(30, 16), () => ExecCrawler(31, 16), () => ExecCrawler(32, 16), () => ExecCrawler(33, 16), () => ExecCrawler(30, 4), () => ExecCrawler(31, 4), () => ExecCrawler(32, 4), () => ExecCrawler(33, 4) ); Console.WriteLine("done"); //DataBufferPool<int> dp = new DataBufferPool<int>(Console.WriteLine); //int iu = 0; //while (iu < 20) //{ // iu++; // dp.AddItem(iu); //} //Console.ReadLine(); #region 引用类型测试 RTest rt = new RTest(); InnerC ic = new InnerC(); ic.T(rt); Console.WriteLine(rt.i); ic.T(ref rt); Console.WriteLine(rt.i); Console.ReadLine(); #endregion #region 动态类型测试 dynamic dynamicobj = new LocalCacheContainer(); dynamicobj.Name = "Learning Hard"; dynamicobj.Age = "24"; var res = dynamicobj.Age; Console.WriteLine("fsadf"); DynamicObjectTest dot = new DynamicObjectTest(); dot.LocalCache.Person = new Person { id = "1", Name = "zl" }; dot.LocalCache.OtherInfo = "234"; Person p1 = dot.LocalCache.Person1 as Person; Console.WriteLine(dot.LocalCache.Person.Name); Console.WriteLine(dot.LocalCache.OtherInfo); Console.ReadLine(); #endregion #region 反射输出对象属性测试 Group gp = new Group { GroupID = 1, GroupName = "TestGroupName", PS = new List <Person> { new Person { id = "1", Name = "n1" }, new Person { id = "2", Name = "n2" }, new Person { id = "3", Name = "n3" } }, Mark = "这是一个测试组" }; foreach (string str in gp.GetReflectPropsValue().Split('|')) { Console.WriteLine(str); } Console.ReadLine(); #endregion #region 随机数测试 TestRandomNum(); #endregion #region 反射对象属性测试 Person p = new Person(); TestReflectProps(p); Console.ReadLine(); #endregion #region 数据缓存仓库测试 //key const string key = "GetCurrDateKey"; const string key_Dt = "GetDatatableKey"; //初始化仓库 DataWarehouse <string> .InitDataItem(key, GetCurrDate, 1); DataWarehouse <DataTable> .InitDataItem(key_Dt, GetDataTable, 1); //根据key获取值 Console.WriteLine(DataWarehouse <string> .GetData(key)); //Console.WriteLine(DataWarehouse<string>.GetData(key)); ////休眠 等待过期 //Thread.Sleep(1000 * 61); ////再次根据key获取值 //Console.WriteLine(DataWarehouse<string>.GetData(key)); //Console.ReadLine(); #endregion #region DataTable测试 DataTable dt = DataWarehouse <DataTable> .GetData(key_Dt); List <Person> ps = dt.GetEntityListByTable <Person>(); Stopwatch s = new Stopwatch(); s.Start(); foreach (DataRow dr in dt.Rows) { } s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); Console.ReadLine(); #endregion }
// 初始化对象 public bool Init(ref NetModuleInit netModuleInit, HandleNetMessage netMsgHandle) { if (netMsgHandle == null) { Trace.Assert(false, "netMsgHandle is null"); return(false); } Trace.Assert(netModuleInit.netSessionClosedCallbackFunc != null, "netModuleInit.netSessionClosedCallbackFunc is null"); Trace.Assert(netModuleInit.netSessionConnectedCallbackFunc != null, "netModuleInit.netSessionConnectedCallbackFunc is null"); if (netModuleInit.netSessionClosedCallbackFunc == null || netModuleInit.netSessionConnectedCallbackFunc == null) { return(false); } netSessionClosedCallbackFunc = netModuleInit.netSessionClosedCallbackFunc; netSessionConnectedCallbackFunc = netModuleInit.netSessionConnectedCallbackFunc; netSessionIDList = new List <int>(); if (netSessionIDList == null) { return(false); } netSessionDic = new Dictionary <int, NetSessionImpl>(); if (netSessionDic == null) { Trace.Assert(false, "netSessionDic is null"); return(false); } addOrDelEventList = new WriteReadList <AddOrDelSessionEvent>(); if (addOrDelEventList == null || !addOrDelEventList.InitWriteReadList(HandleAddOrDelEvent, FreeEvent)) { Trace.Assert(false, "addOrDelEventList is error"); return(false); } // 初始化网络消息池 if (!MsgBufferPool.GetInstance().Init(netModuleInit.msgBufferCounts, netModuleInit.msgBufferSize)) { Trace.Assert(false, "MsgBufferPool init false"); return(false); } // 初始化消息管理器 if (!MsgBufferMgr.GetInstance().Init(netMsgHandle)) { Trace.Assert(false, "MsgBufferMgr init false"); return(false); } // 初始化数据缓冲区 if (!DataBufferPool.GetInstance().Init(netModuleInit.bufferReserves, netModuleInit.sendBufSize, netModuleInit.sendBufExtend, netModuleInit.rcvBufSize, netModuleInit.rcvBufExtend)) { Trace.Assert(false, "DataBufferPool init false"); return(false); } // 初始化网络会话池 if (!NetSessionPool.GetInstance().Init(netModuleInit.sessionInitCount, netModuleInit.sessionExtendCount)) { Trace.Assert(false, "NetSessionPool init false"); return(false); } return(true); }