Ejemplo n.º 1
0
        static void RunLoop(bool log)
        {
            var loop = new UVLoop();

            var listener  = new TcpListener(s_ipAddress, s_port, loop);
            var formatter = new ArrayFormatter(512, EncodingData.InvariantUtf8);

            listener.ConnectionAccepted += (Tcp connection) =>
            {
                if (log)
                {
                    Console.WriteLine("connection accepted");
                }

                connection.ReadCompleted += (data) =>
                {
                    if (log)
                    {
                        unsafe
                        {
                            var requestString = new Utf8String(data);
                            Console.WriteLine("*REQUEST:\n {0}", requestString.ToString());
                        }
                    }

                    formatter.Clear();
                    formatter.Append("HTTP/1.1 200 OK");
                    formatter.Append("\r\n\r\n");
                    formatter.Append("Hello World!");
                    if (log)
                    {
                        formatter.Format(" @ {0:O}", DateTime.UtcNow);
                    }

                    var segment = formatter.Formatted;
                    unsafe
                    {
                        fixed(byte *p = segment.Array)
                        {
                            var response = new UnsafeMemory <byte>(segment.Array, segment.Offset, segment.Count, pointer: p);

                            connection.TryWrite(response);
                        }
                    }

                    connection.Dispose();
                };

                connection.ReadStart();
            };

            listener.Listen();
            loop.Run();
        }
Ejemplo n.º 2
0
        public void FormatXUtf8()
        {
            var x = StandardFormat.Parse("x");
            var X = StandardFormat.Parse("X");

            var sb = new ArrayFormatter(256, SymbolTable.InvariantUtf8);

            sb.Append((ulong)255, x);
            sb.Append((uint)255, X);

            Assert.Equal("ffFF", new Utf8Span(sb.Formatted.AsSpan()).ToString());

            sb.Clear();
            sb.Append((int)-1, X);
            Assert.Equal("FFFFFFFF", new Utf8Span(sb.Formatted.AsSpan()).ToString());

            sb.Clear();
            sb.Append((int)-2, X);
            Assert.Equal("FFFFFFFE", new Utf8Span(sb.Formatted.AsSpan()).ToString());
        }
Ejemplo n.º 3
0
        static void RunLoop(bool log)
        {
            var loop = new UVLoop();

            var listener = new TcpListener(s_ipAddress, s_port, loop);

            listener.ConnectionAccepted += (Tcp connection) =>
            {
                if (log)
                {
                    Console.WriteLine("connection accepted");
                }

                connection.ReadCompleted += (data) =>
                {
                    if (log)
                    {
                        unsafe
                        {
                            var requestString = new Utf8Span(data.Span);
                            Console.WriteLine("*REQUEST:\n {0}", requestString.ToString());
                        }
                    }

                    var formatter = new ArrayFormatter(512, SymbolTable.InvariantUtf8);
                    formatter.Clear();
                    formatter.Append("HTTP/1.1 200 OK");
                    formatter.Append("\r\n\r\n");
                    formatter.Append("Hello World!");
                    if (log)
                    {
                        formatter.Format(" @ {0:O}", DateTime.UtcNow);
                    }

                    var segment = formatter.Formatted;
                    using (var memory = new OwnedPinnedBuffer <byte>(segment.Array))
                    {
                        connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count));
                        connection.Dispose();
                    }
                };

                connection.ReadStart();
            };

            listener.Listen();
            loop.Run();
        }
        public void FormatDateTimeRUtf8()
        {
            var time = DateTime.UtcNow;

            var expected = time.ToString("R");

            var sb = new ArrayFormatter(100, TextEncoder.Utf8);

            sb.Append(time, 'R');
            var result       = sb.Formatted.AsSpan().ToArray();
            var resultString = Encoding.UTF8.GetString(result);

            Assert.Equal(expected, resultString);

            sb.Clear();
        }
Ejemplo n.º 5
0
        public void WriterSystemTextJsonBasic(bool formatted, EncoderTarget encoderTarget)
        {
            var encoder = GetTargetEncoder(encoderTarget);
            var f       = new ArrayFormatter(BufferSize, encoder);

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        f.Clear();
                        TestWriterSystemTextJsonBasic(formatted, f);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void EncodeStringToUtf8()
        {
            string         text           = "Hello World!";
            int            stringsToWrite = 2000;
            int            size           = stringsToWrite * text.Length + stringsToWrite;
            ArrayFormatter formatter      = new ArrayFormatter(size, SymbolTable.InvariantUtf8, pool);

            timer.Restart();
            for (int itteration = 0; itteration < itterationsInvariant; itteration++)
            {
                formatter.Clear();
                for (int i = 0; i < stringsToWrite; i++)
                {
                    formatter.Append(text);
                    formatter.Append(1);
                }
                Assert.Equal(size, formatter.CommitedByteCount);
            }
            PrintTime();
        }
Ejemplo n.º 7
0
        public void WriteJsonUtf16()
        {
            var formatter = new ArrayFormatter(1024, SymbolTable.InvariantUtf16);
            var json      = new JsonWriterUtf16(formatter, prettyPrint: false);

            Write(ref json);

            var formatted = formatter.Formatted;
            var str       = Encoding.Unicode.GetString(formatted.Array, formatted.Offset, formatted.Count);

            Assert.Equal(expected, str.Replace(" ", ""));

            formatter.Clear();
            json = new JsonWriterUtf16(formatter, prettyPrint: true);
            Write(ref json);

            formatted = formatter.Formatted;
            str       = Encoding.Unicode.GetString(formatted.Array, formatted.Offset, formatted.Count);
            Assert.Equal(expected, str.Replace("\r\n", "").Replace("\n", "").Replace(" ", ""));
        }
Ejemplo n.º 8
0
        public void DataArray()
        {
            // Verify that we can deserialize arrays of data items.

            var converter = new JsonDataConverter();
            var formatter = new ArrayFormatter();

            byte[]   contents;
            object[] items;

            // Empty array.

            contents = null;
            items    = converter.FromDataArray(contents, Array.Empty <Type>());

            Assert.Empty(items);

            // Single item.

            formatter.Clear();
            formatter.Append("\"foo\"");

            contents = formatter.ToBytes();
            items    = converter.FromDataArray(contents, new Type[] { typeof(string) });

            Assert.Equal(new string[] { "foo" }, items);

            // Multiple object items

            formatter.Clear();
            formatter.Append("\"foo\"");
            formatter.Append("1234");
            formatter.Append("{Hello: \"World!\"}");
            formatter.Append("null");

            contents = formatter.ToBytes();
            items    = converter.FromDataArray(contents, new Type[] { typeof(string), typeof(int), typeof(TestData), typeof(TestData) });

            Assert.Equal(4, items.Length);
            Assert.Equal("foo", items[0]);
            Assert.Equal(1234, items[1]);
            Assert.Equal("World!", ((TestData)items[2]).Hello);
            Assert.Null(items[3]);

            // Roundtrip objects

            var bob = new Person()
            {
                Name   = "Bob",
                Age    = 27,
                Data   = new byte[] { 0, 1, 2, 3, 4 },
                Gender = Gender.Male
            };

            bob.__O.Add("extra", "data");

            formatter.Clear();
            formatter.Clear();
            formatter.Append("\"foo\"");
            formatter.Append("1234");
            formatter.Append(bob.ToString(indented: false));
            formatter.Append("null");

            contents = formatter.ToBytes();
            items    = converter.FromDataArray(contents, new Type[] { typeof(string), typeof(int), typeof(Person), typeof(Person) });

            Assert.Equal(4, items.Length);
            Assert.Equal("foo", items[0]);
            Assert.Equal(1234, items[1]);
            Assert.Equal(bob, (Person)items[2]);
            Assert.Equal("data", ((Person)items[2]).__O["extra"].ToString());
            Assert.Null(items[3]);

            // Arrays of other types.

            formatter.Clear();

            var guid = Guid.NewGuid();

            formatter.Append("10");
            formatter.Append("123.4");
            formatter.Append("\"Hello World!\"");
            formatter.Append("null");
            formatter.Append("\"female\"");
            formatter.Append("true");
            formatter.Append(NeonHelper.JsonSerialize(new DateTime(2019, 7, 17, 12, 0, 0)));
            formatter.Append(NeonHelper.JsonSerialize(TimeSpan.FromSeconds(1.5)));
            formatter.Append($"\"{guid}\"");

            contents = formatter.ToBytes();
            items    = converter.FromDataArray(contents, new Type[] { typeof(int), typeof(double), typeof(string), typeof(string), typeof(Gender), typeof(bool), typeof(DateTime), typeof(TimeSpan), typeof(Guid) });

            Assert.Equal(9, items.Length);
            Assert.Equal(10, (int)items[0]);
            Assert.Equal(123.4, (double)items[1]);
            Assert.Equal("Hello World!", (string)items[2]);
            Assert.Null((string)items[3]);
            Assert.Equal(Gender.Female, (Gender)items[4]);
            Assert.True((bool)items[5]);
            Assert.Equal(new DateTime(2019, 7, 17, 12, 0, 0), (DateTime)items[6]);
            Assert.Equal(TimeSpan.FromSeconds(1.5), (TimeSpan)items[7]);
            Assert.Equal(guid, (Guid)items[8]);
        }
Ejemplo n.º 9
0
 public void WriterSlowSystemTextJsonBasic(ArrayFormatter formatter)
 {
     formatter.Clear();
     WriterSystemTextJsonBasic(Formatted, formatter);
 }
Ejemplo n.º 10
0
 public void WriterSlowSystemTextJsonHelloWorld(ArrayFormatter formatter)
 {
     formatter.Clear();
     WriterSystemTextJsonHelloWorld(Formatted, formatter);
 }
Ejemplo n.º 11
0
 public void WriterSystemTextJsonBasic()
 {
     _arrayFormatter.Clear();
     WriterSystemTextJsonBasic(Formatted, _arrayFormatter);
 }
        public void FormatXUtf8()
        {
            var x = TextFormat.Parse("x");
            var X = TextFormat.Parse("X");

            var sb = new ArrayFormatter(256, EncodingData.InvariantUtf8);
            sb.Append((ulong)255, x);
            sb.Append((uint)255, X);

            Assert.Equal("ffFF", new Utf8String(sb.Formatted.Slice()).ToString());

            sb.Clear();
            sb.Append((int)-1, X);
            Assert.Equal("FFFFFFFF", new Utf8String(sb.Formatted.Slice()).ToString());

            sb.Clear();
            sb.Append((int)-2, X);
            Assert.Equal("FFFFFFFE", new Utf8String(sb.Formatted.Slice()).ToString());
        }
Ejemplo n.º 13
0
        private void EncodeStringToUtf8()
        {
            string text = "Hello World!";
            int stringsToWrite = 2000;
            int size = stringsToWrite * text.Length + stringsToWrite;
            ArrayFormatter formatter = new ArrayFormatter(size, EncodingData.InvariantUtf8, pool);

            timer.Restart();
            for (int itteration = 0; itteration < itterationsInvariant; itteration++)
            {
                formatter.Clear();
                for (int i = 0; i < stringsToWrite; i++)
                {
                    formatter.Append(text);
                    formatter.Append(1);
                }
                Assert.Equal(size, formatter.CommitedByteCount);
            }
            PrintTime();
        }