public void FreePoolChunk() { const int ChunkSize = 16 * 1024 * 1024; var allocator = new PooledByteBufferAllocator(true, 1, 0, 8192, 11, 0, 0, 0); IByteBuffer buffer = allocator.HeapBuffer(ChunkSize); var arenas = allocator.Metric.HeapArenas(); Assert.Equal(1, arenas.Count); var lists = arenas[0].ChunkLists; Assert.Equal(6, lists.Count); Assert.False(lists[0].GetEnumerator().MoveNext()); Assert.False(lists[1].GetEnumerator().MoveNext()); Assert.False(lists[2].GetEnumerator().MoveNext()); Assert.False(lists[3].GetEnumerator().MoveNext()); Assert.False(lists[4].GetEnumerator().MoveNext()); // Must end up in the 6th PoolChunkList Assert.True(lists[5].GetEnumerator().MoveNext()); Assert.True(buffer.Release()); // Should be completely removed and so all PoolChunkLists must be empty Assert.False(lists[0].GetEnumerator().MoveNext()); Assert.False(lists[1].GetEnumerator().MoveNext()); Assert.False(lists[2].GetEnumerator().MoveNext()); Assert.False(lists[3].GetEnumerator().MoveNext()); Assert.False(lists[4].GetEnumerator().MoveNext()); Assert.False(lists[5].GetEnumerator().MoveNext()); }
static void AllocNotNull0(PooledByteBufferAllocator allocator, int capacity) { IByteBuffer buffer = allocator.HeapBuffer(capacity); Assert.NotNull(buffer.Allocator); Assert.True(buffer.Release()); Assert.NotNull(buffer.Allocator); }
public void GlobalSetup() { PooledByteBufferAllocator allocator = PooledByteBufferAllocator.Default; // Use buffer sizes that will also allow to write UTF-8 without grow the buffer this.buffer = allocator.DirectBuffer(512); this.wrapped = Unpooled.UnreleasableBuffer(allocator.DirectBuffer(512)); var asciiSequence = new StringBuilder(128); for (int i = 0; i < 128; i++) { asciiSequence.Append('a'); } this.ascii = asciiSequence.ToString(); // Generate some mixed UTF-8 String for benchmark var utf8Sequence = new StringBuilder(128); char[] chars = "Some UTF-8 like äÄ∏ŒŒ".ToCharArray(); for (int i = 0; i < 128; i++) { utf8Sequence.Append(chars[i % chars.Length]); } this.utf8 = utf8Sequence.ToString(); byte[] bytes = Encoding.ASCII.GetBytes(this.ascii); this.asciiLength = bytes.Length; this.asciiBuffer = allocator.DirectBuffer(this.asciiLength); this.asciiBuffer.WriteBytes(bytes); bytes = Encoding.UTF8.GetBytes(this.utf8); this.utf8Length = bytes.Length; this.utf8Buffer = allocator.DirectBuffer(bytes.Length); this.utf8Buffer.WriteBytes(bytes); }
public void CommonTest() { PooledByteBufferAllocator pbba = new PooledByteBufferAllocator(); IByteBuffer bf = pbba.CompositeBuffer(); bf.WriteUnsignedShort(20); bf.WriteBoolean(true); bf.WriteInt(32); bf.WriteInt(1); bf.WriteInt(2); bf.WriteInt(3); bf.WriteInt(4); bf.WriteInt(5); bf.WriteInt(6); bf.WriteByte(0xff); SimplDataType sdt = ByteStreamToObjectConverter.Deserialize <SimplDataType>(bf); Assert.AreEqual(20, sdt.head); Assert.AreEqual(true, sdt.result); Assert.AreEqual(32, sdt.length); Assert.AreEqual(0xff, sdt.XOR); //System.Console.ReadLine(); }
public void Test_Package_simple_Type() { PooledByteBufferAllocator pbba = new PooledByteBufferAllocator(); IByteBuffer bf = pbba.CompositeBuffer(); bf.WriteByte(0xef); bf.WriteByte(0xef); // 超始标识 bf.SetUnsignedInt(bf.WriterIndex, 2000); // 数据包ID bf.SetWriterIndex(bf.WriterIndex + sizeof(UInt32)); bf.WriteUnsignedShort((ushort)CommandIdentifier.Hello); // 命令标识 bf.WriteInt(6); // 数据长度 bf.WriteUnsignedShort(2); // 内容标识 bf.WriteInt(0); // 内容长度 bf.WriteUnsignedShort(20); TRPackage ar = ByteStreamToObjectConverter.Deserialize <TRPackage>(bf); Assert.AreEqual(ar.delimiter, 0xefef); Assert.AreEqual(ar.packageid, 2000); Assert.AreEqual(ar.identifier, (ushort)CommandIdentifier.Hello); Assert.AreEqual(ar.data_len, 6); Assert.AreEqual(ar.content_identifier, 2); Assert.AreEqual(ar.content_len, 0); Assert.AreEqual(ar.content_obj, null); //System.Console.ReadLine(); }
internal Udp(LoopContext loop, PooledByteBufferAllocator allocator) : base(loop, uv_handle_type.UV_UDP) { Contract.Requires(allocator != null); this.allocator = allocator; this.pendingRead = new PendingRead(); }
static void ArenaMetrics0(PooledByteBufferAllocator allocator, int num, int expectedActive, int expectedAlloc, int expectedDealloc) { for (int i = 0; i < num; i++) { Assert.True(allocator.HeapBuffer().Release()); } AssertArenaMetrics(allocator.Metric.HeapArenas(), expectedActive, expectedAlloc, expectedDealloc); }
async Task EnsureServerInitializedAsync() { if (this.ServerAddress != null) { return; } int threadCount = Environment.ProcessorCount; var executorGroup = new MultithreadEventLoopGroup(threadCount); var bufAllocator = new PooledByteBufferAllocator(16 * 1024, 10 * 1024 * 1024 / threadCount); // reserve 10 MB for 64 KB buffers BlobSessionStatePersistenceProvider sessionStateProvider = await BlobSessionStatePersistenceProvider.CreateAsync( this.settingsProvider.GetSetting("BlobSessionStatePersistenceProvider.StorageConnectionString"), this.settingsProvider.GetSetting("BlobSessionStatePersistenceProvider.StorageContainerName")); TableQos2StatePersistenceProvider qos2StateProvider = await TableQos2StatePersistenceProvider.CreateAsync( this.settingsProvider.GetSetting("TableQos2StatePersistenceProvider.StorageConnectionString"), this.settingsProvider.GetSetting("TableQos2StatePersistenceProvider.StorageTableName")); var settings = new Settings(this.settingsProvider); var authProvider = new SasTokenDeviceIdentityProvider(); var topicNameRouter = new ConfigurableMessageRouter(); IotHubClientFactoryFunc iotHubClientFactoryMethod = IotHubClient.PreparePoolFactory(settings.IotHubConnectionString + ";DeviceId=stub", "a", 1); var iotHubFactory = new IotHubCommunicationFactory(iotHubClientFactoryMethod); ServerBootstrap server = new ServerBootstrap() .Group(executorGroup) .Channel <TcpServerSocketChannel>() .ChildOption(ChannelOption.Allocator, bufAllocator) .ChildOption(ChannelOption.AutoRead, false) .ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(TlsHandler.Server(this.tlsCertificate)); ch.Pipeline.AddLast( MqttEncoder.Instance, new MqttDecoder(true, 256 * 1024), new LoggingHandler(), new MqttIotHubAdapter( settings, sessionStateProvider, authProvider, qos2StateProvider, iotHubFactory, topicNameRouter), new XUnitLoggingHandler(this.output)); })); IChannel serverChannel = await server.BindAsync(IPAddress.Any, this.ProtocolGatewayPort); this.ScheduleCleanup(async() => { await serverChannel.CloseAsync(); await executorGroup.ShutdownGracefullyAsync(); }); this.ServerAddress = IPAddress.Loopback; }
internal Pipeline(StreamHandle streamHandle, PooledByteBufferAllocator allocator) { Contract.Requires(streamHandle != null); Contract.Requires(allocator != null); this.streamHandle = streamHandle; this.allocator = allocator; this.receiveBufferSizeEstimate = new ReceiveBufferSizeEstimate(); this.pendingRead = new PendingRead(); }
internal Pipeline(StreamHandle streamHandle, PooledByteBufferAllocator allocator) { Debug.Assert(streamHandle is object); Debug.Assert(allocator is object); _streamHandle = streamHandle; _allocator = allocator; _receiveBufferSizeEstimate = new ReceiveBufferSizeEstimate(); _pendingRead = new PendingRead(); }
internal Udp(LoopContext loop, PooledByteBufferAllocator allocator) : base(loop, uv_handle_type.UV_UDP) { if (allocator is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.allocator); } _allocator = allocator; _pendingRead = new PendingRead(); }
public void AllocNotNull() { var allocator = new PooledByteBufferAllocator(true, 1, 1, 8192, 11, 0, 0, 0); // Huge allocation AllocNotNull0(allocator, allocator.Metric.ChunkSize + 1); // Normal allocation AllocNotNull0(allocator, 1024); // Small allocation AllocNotNull0(allocator, 512); // Tiny allocation AllocNotNull0(allocator, 1); }
public void PooledBufferGrowTest(int bufferSize, int startSize, int[] writeSizes) { var alloc = new PooledByteBufferAllocator(); IByteBuffer buffer = alloc.Buffer(startSize); int wrote = 0; foreach (int size in writeSizes) { buffer.WriteBytes(Unpooled.WrappedBuffer(new byte[size])); wrote += size; } Assert.Equal(wrote, buffer.ReadableBytes); }
public void PooledBufferGrowTest(int bufferSize, int startSize, int[] writeSizes) { var alloc = new PooledByteBufferAllocator(bufferSize, int.MaxValue); IByteBuffer buffer = alloc.Buffer(startSize); int wrote = 0; foreach (int size in writeSizes) { buffer.WriteBytes(Unpooled.WrappedBuffer(new byte[size])); wrote += size; } Assert.Equal(wrote, buffer.ReadableBytes); }
public void TinySubpageMetric() { var allocator = new PooledByteBufferAllocator(true, 1, 1, 8192, 11, 0, 0, 0); IByteBuffer buffer = allocator.HeapBuffer(1); try { IPoolArenaMetric metric = allocator.Metric.HeapArenas()[0]; IPoolSubpageMetric subpageMetric = metric.TinySubpages[0]; Assert.Equal(1, subpageMetric.MaxNumElements - subpageMetric.NumAvailable); } finally { buffer.Release(); } }
public void ResampleNoLeak() { ResourceLeakDetector.DetectionLevel preservedLevel = ResourceLeakDetector.Level; try { ResourceLeakDetector.Level = ResourceLeakDetector.DetectionLevel.Paranoid; var bufPool = new PooledByteBufferAllocator(100, 1000); IByteBuffer buffer = bufPool.Buffer(10); buffer.Release(); buffer = bufPool.Buffer(10); buffer.Release(); } finally { ResourceLeakDetector.Level = preservedLevel; } }
/// <summary> /// Serialize目前只支持TR, 因为时间问题,不再过多研究 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static IByteBuffer Serialize <T>(object obj) { PooledByteBufferAllocator pbba = new PooledByteBufferAllocator(); IByteBuffer bf = pbba.CompositeBuffer(); try { SerializeInner(obj, bf); } catch (Exception ex) { bf.Release(); return(null); } return(bf); }
public void AllocationCounter() { var allocator = new PooledByteBufferAllocator( true, // preferDirect 0, // nHeapArena 1, // nDirectArena 8192, // pageSize 11, // maxOrder 0, // tinyCacheSize 0, // smallCacheSize 0 // normalCacheSize ); // create tiny buffer IByteBuffer b1 = allocator.Buffer(24); // create small buffer IByteBuffer b2 = allocator.Buffer(800); // create normal buffer IByteBuffer b3 = allocator.Buffer(8192 * 2); Assert.NotNull(b1); Assert.NotNull(b2); Assert.NotNull(b3); // then release buffer to deallocated memory while threadlocal cache has been disabled // allocations counter value must equals deallocations counter value Assert.True(b1.Release()); Assert.True(b2.Release()); Assert.True(b3.Release()); Assert.True(allocator.DirectArenas().Count >= 1); IPoolArenaMetric metric = allocator.DirectArenas()[0]; Assert.Equal(3, metric.NumDeallocations); Assert.Equal(3, metric.NumAllocations); Assert.Equal(1, metric.NumTinyDeallocations); Assert.Equal(1, metric.NumTinyAllocations); Assert.Equal(1, metric.NumSmallDeallocations); Assert.Equal(1, metric.NumSmallAllocations); Assert.Equal(1, metric.NumNormalDeallocations); Assert.Equal(1, metric.NumNormalAllocations); }
public void CommonTest() { PooledByteBufferAllocator pbba = new PooledByteBufferAllocator(); IByteBuffer bf = pbba.CompositeBuffer(); bf.WriteUnsignedShort(20); bf.WriteInt(21); bf.WriteBoolean(false); bf.WriteByte(0xff); SimplDataType sdt = ByteStreamToObjectConverter.Deserialize <SimplDataType>(bf); Assert.AreEqual(20, sdt.type); Assert.AreEqual(21, sdt.id); Assert.AreEqual(false, sdt.gender); Assert.AreEqual(0xff, sdt.hash); //System.Console.ReadLine(); }
public void Leak() { var eventListener = new ObservableEventListener(); Mock<IObserver<EventEntry>> logListener = this.mockRepo.Create<IObserver<EventEntry>>(); var eventTextFormatter = new EventTextFormatter(); Func<EventEntry, bool> leakPredicate = y => y.TryFormatAsString(eventTextFormatter).Contains("LEAK"); logListener.Setup(x => x.OnNext(It.Is<EventEntry>(y => leakPredicate(y)))).Verifiable(); logListener.Setup(x => x.OnNext(It.Is<EventEntry>(y => !leakPredicate(y)))); eventListener.Subscribe(logListener.Object); eventListener.EnableEvents(DefaultEventSource.Log, EventLevel.Verbose); var bufPool = new PooledByteBufferAllocator(100, 1000); IByteBuffer buffer = bufPool.Buffer(10); buffer = null; GC.Collect(); GC.WaitForPendingFinalizers(); this.mockRepo.Verify(); }
public void Leak() { var eventListener = new ObservableEventListener(); Mock <IObserver <EventEntry> > logListener = this.mockRepo.Create <IObserver <EventEntry> >(); var eventTextFormatter = new EventTextFormatter(); Func <EventEntry, bool> leakPredicate = y => y.TryFormatAsString(eventTextFormatter).Contains("LEAK"); logListener.Setup(x => x.OnNext(It.Is <EventEntry>(y => leakPredicate(y)))).Verifiable(); logListener.Setup(x => x.OnNext(It.Is <EventEntry>(y => !leakPredicate(y)))); eventListener.Subscribe(logListener.Object); eventListener.EnableEvents(DefaultEventSource.Log, EventLevel.Verbose); var bufPool = new PooledByteBufferAllocator(100, 1000); IByteBuffer buffer = bufPool.Buffer(10); buffer = null; GC.Collect(); GC.WaitForPendingFinalizers(); this.mockRepo.Verify(); }
public void CommonTest() { PooledByteBufferAllocator pbba = new PooledByteBufferAllocator(); IByteBuffer bf = pbba.CompositeBuffer(); bf.WriteUnsignedShort(20); bf.WriteBoolean(true); bf.WriteUnsignedShort(2); bf.WriteInt(21); bf.WriteInt(22); bf.WriteInt(23); bf.WriteInt(24); bf.WriteUnsignedShort(10); bf.WriteInt(25); bf.WriteInt(28); bf.WriteInt(25); bf.WriteInt(26); bf.WriteInt(29); bf.WriteInt(26); bf.WriteInt(27); bf.WriteInt(30); bf.WriteInt(27); bf.WriteInt(28); bf.WriteInt(25); bf.WriteInt(28); bf.WriteInt(29); bf.WriteInt(26); bf.WriteInt(29); bf.WriteInt(30); bf.WriteInt(27); bf.WriteInt(30); bf.WriteInt(25); bf.WriteInt(28); bf.WriteInt(25); bf.WriteInt(26); bf.WriteInt(29); bf.WriteInt(26); bf.WriteInt(27); bf.WriteInt(30); bf.WriteInt(27); bf.WriteInt(28); bf.WriteInt(29); bf.WriteInt(30); bf.WriteByte(0xff); SimplDataType sdt = ByteStreamToObjectConverter.Deserialize <SimplDataType>(bf); Assert.AreEqual(20, sdt.type); Assert.AreEqual(true, sdt.three_D); Assert.AreEqual(0xff, sdt.hash); //System.Console.ReadLine(); }
public void Test_content_maintaince_string() { PooledByteBufferAllocator pbba = new PooledByteBufferAllocator(); IByteBuffer bf = pbba.CompositeBuffer(); bf.WriteByte(0xef); // 1 bf.WriteByte(0xef); // 超始标识 1 bf.SetUnsignedInt(bf.WriterIndex, 2000); // 数据包ID 4 bf.SetWriterIndex(bf.WriterIndex + sizeof(UInt32)); bf.WriteUnsignedShort((ushort)CommandIdentifier.Report); // 命令标识 2 bf.WriteInt(6); // 数据长度 4 bf.WriteUnsignedShort((ushort)PackageDataIdnetifier.Maintenance); // 内容标识 2 bf.WriteInt(100); // 内容长度 4 ////////////////写入内容///////////////////// bf.WriteByte(0x01); // 1 MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms, encoding: Encoding.ASCII); string bstring; bstring = "codes"; bw.Write(bstring); ms.Seek(0, SeekOrigin.Begin); // Read the string as raw bytes using FileStream... // The first series of bytes is the UTF7 encoded length of the // string. In this case, however, it is just the first two bytes. int len = ms.ReadByte() & 0x7f; len += ms.ReadByte() & 0x80; byte[] code_str = new byte[len]; ms.Read(code_str, 0, len); ms.Seek(0, SeekOrigin.Begin); bf.WriteByte(ms.ReadByte()); bf.WriteByte(ms.ReadByte()); string convertred = Encoding.ASCII.GetString(code_str); bf.WriteBytes(code_str); ms.Seek(0, SeekOrigin.Begin); bf.WriteByte(ms.ReadByte()); bf.WriteByte(ms.ReadByte()); bf.WriteBytes(code_str); ms.Close(); ////////////////写入内容///////////////////// bf.WriteUnsignedShort(20); //2 TRPackage ar = ByteStreamToObjectConverter.Deserialize <TRPackage>(bf); Assert.AreEqual(ar.delimiter, 0xefef); Assert.AreEqual(ar.packageid, 2000); Assert.AreEqual(ar.identifier, (ushort)CommandIdentifier.Report); Assert.AreEqual(ar.data_len, 6); Assert.AreEqual(ar.content_identifier, (ushort)PackageDataIdnetifier.Maintenance); Assert.AreEqual(ar.content_len, 100); Assert.AreEqual(ar.content_obj.GetType(), typeof(TRCloudMaintenance)); Assert.AreEqual(20, ar.xor_check_code); //System.Console.ReadLine(); }
/// <summary> /// /// </summary> static BufferManager() { pooledByteBufAllocator = new PooledByteBufferAllocator(true); }
static ClientChannelHandler() { pooledByteBufAllocator = new PooledByteBufferAllocator(true); }
public void Test_content_analysis_result_list() { PooledByteBufferAllocator pbba = new PooledByteBufferAllocator(); IByteBuffer bf = pbba.CompositeBuffer(); bf.WriteByte(0xef); // 1 bf.WriteByte(0xef); // 超始标识 1 bf.SetUnsignedInt(bf.WriterIndex, 2000); // 数据包ID 4 bf.SetWriterIndex(bf.WriterIndex + sizeof(UInt32)); bf.WriteUnsignedShort((ushort)CommandIdentifier.Report); // 命令标识 2 bf.WriteInt(6); // 数据长度 4 bf.WriteUnsignedShort((ushort)PackageDataIdnetifier.AnalyseResults); // 内容标识 2 bf.WriteInt(100); // 内容长度 4 ////////////////写入内容///////////////////// bf.WriteInt(23); // recordid bf.WriteInt(24); // type bf.WriteLong(25); // time bf.WriteInt(26); // analysiscount bf.WriteInt(27); // alarmcount bf.WriteBoolean(true); // isalarm MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms, encoding: Encoding.ASCII); string bstring; bstring = "codes"; bw.Write(bstring); ms.Seek(0, SeekOrigin.Begin); // Read the string as raw bytes using FileStream... // The first series of bytes is the UTF7 encoded length of the // string. In this case, however, it is just the first two bytes. int len = ms.ReadByte() & 0x7f; len += ms.ReadByte() & 0x80; byte[] code_str = new byte[len]; ms.Read(code_str, 0, len); ms.Seek(0, SeekOrigin.Begin); bf.WriteByte(ms.ReadByte()); bf.WriteByte(ms.ReadByte()); bf.WriteBytes(code_str); // softversion ms.Seek(0, SeekOrigin.Begin); bf.WriteByte(ms.ReadByte()); bf.WriteByte(ms.ReadByte()); bf.WriteBytes(code_str);// algversion ms.Seek(0, SeekOrigin.Begin); bf.WriteByte(ms.ReadByte()); bf.WriteByte(ms.ReadByte()); bf.WriteBytes(code_str);// libversion ms.Seek(0, SeekOrigin.Begin); bf.WriteByte(ms.ReadByte()); bf.WriteByte(ms.ReadByte()); bf.WriteBytes(code_str);// username // alarm result bf.WriteInt(1); // count // alarm info bf.WriteInt(28);// masstype ms.Seek(0, SeekOrigin.Begin); bf.WriteByte(ms.ReadByte()); bf.WriteByte(ms.ReadByte()); bf.WriteBytes(code_str); // massname bf.WriteFloat(29); // identensity bf.WriteInt(30); bf.WriteFloat(31); bf.WriteFloat(32); bf.WriteFloat(33); bf.WriteFloat(34); bf.WriteFloat(35); bf.WriteFloat(36); bf.WriteFloat(37); bf.WriteFloat(38); bf.WriteFloat(39); bf.WriteFloat(40); ms.Close(); ////////////////写入内容///////////////////// bf.WriteUnsignedShort(20); //2 TRPackage ar = ByteStreamToObjectConverter.Deserialize <TRPackage>(bf); Assert.AreEqual(ar.delimiter, 0xefef); Assert.AreEqual(ar.packageid, 2000); Assert.AreEqual(ar.identifier, (ushort)CommandIdentifier.Report); Assert.AreEqual(ar.data_len, 6); Assert.AreEqual(ar.content_identifier, (ushort)PackageDataIdnetifier.AnalyseResults); Assert.AreEqual(ar.content_len, 100); Assert.AreEqual(ar.content_obj.GetType(), typeof(TRCloudAnalyseResult)); Assert.AreEqual(20, ar.xor_check_code); //System.Console.ReadLine(); }