コード例 #1
0
ファイル: Program.cs プロジェクト: dnobori/DNT-Jikken
        static void BenchStreamBuffer()
        {
            FastStreamBuffer <byte> new_test_buf(int num = 1)
            {
                FastStreamBuffer <byte> ret = new FastStreamBuffer <byte>();

                for (int i = 0; i < num; i++)
                {
                    ret.Enqueue(new byte[] { 1, 2, 3, });
                    ret.Enqueue(new byte[] { 4, 5, 6, 7 });
                    ret.Enqueue(new byte[] { 8, 9, 10, 11, 12 });
                    ret.Enqueue(new byte[] { 13, 14, 15, 16, 17, 18 });
                }
                return(ret);
            }

            MicroBenchmarkQueue q = new MicroBenchmarkQueue();

            int num_test_data = 10000;

            object[] test_data_array = new object[num_test_data];
            for (int i = 0; i < num_test_data; i++)
            {
                test_data_array[i] = new object();
            }

            q.Add(new MicroBenchmark <int>("Queue insert test: .NET Queue", 10000,
                                           (x, iterations) =>
            {
                Queue <object> queue = new Queue <object>();

                for (int i = 0; i < iterations; i++)
                {
                    foreach (object o in test_data_array)
                    {
                        queue.Enqueue(o);
                    }
                }
            },
                                           () => 0), true, 0);


            q.Add(new MicroBenchmark <int>("Queue insert test: Fifo<T>", 10000,
                                           (x, iterations) =>
            {
                Fifo <object> fifo = new Fifo <object>();
                var span           = test_data_array.AsSpan();

                for (int i = 0; i < iterations; i++)
                {
                    fifo.Write(span);
                }
            },
                                           () => 0), true, 0);


            q.Add(new MicroBenchmark <int>("InsertFirst", 1000,
                                           (x, iterations) =>
            {
                List <int> rands = new List <int>();
                for (int i = 0; i < 256; i++)
                {
                    rands.Add(WebSocketHelper.RandSInt31());
                }
                Memory <byte> add_data = new byte[] { 1, 2, 3, 4, 5, };

                FastStreamBuffer <byte> buf = new_test_buf();

                for (int i = 0; i < iterations; i++)
                {
                    //buf.Insert(buf.PinHead + 0, add_data);
                    buf.InsertHead(add_data);
                }
            },
                                           () => 0), true, 0);

            q.Add(new MicroBenchmark <int>("InsertAndRemoveRandom", 10000,
                                           (x, iterations) =>
            {
                List <int> rands = new List <int>();
                for (int i = 0; i < 256; i++)
                {
                    rands.Add(WebSocketHelper.RandSInt31());
                }
                Memory <byte> add_data = new byte[] { 1, 2, 3, 4, 5, };

                FastStreamBuffer <byte> buf = new_test_buf();

                for (int i = 0; i < iterations; i++)
                {
                    long pin = buf.PinHead + rands[i % rands.Count] % (buf.Length - add_data.Length);
                    buf.Insert(pin, add_data);
                    buf.Remove(pin, add_data.Length);
                }
            },
                                           () => 0), true, 0);

            q.Add(new MicroBenchmark <int>("EnqueueAndDequeue", 10000,
                                           (x, iterations) =>
            {
                List <int> rands = new List <int>();
                for (int i = 0; i < 256; i++)
                {
                    rands.Add(WebSocketHelper.RandSInt31());
                }
                Memory <byte> add_data = new byte[] { 1, 2, 3, 4, 5, };

                FastStreamBuffer <byte> buf = new_test_buf();

                for (int i = 0; i < iterations; i++)
                {
                    buf.Enqueue(add_data);
                    var ret = buf.Dequeue(long.MaxValue, out _, false);
                }
            },
                                           () => 0), true, 10);

            FastStreamBuffer <byte> bufx1 = new_test_buf(10000);
            FastStreamBuffer <byte> bufx2 = new_test_buf(10000);

            q.Add(new MicroBenchmark <int>("MoveToOtherEmpty", 100000,
                                           (x, iterations) =>
            {
                for (int i = 0; i < iterations; i++)
                {
                    bufx1.DequeueAllAndEnqueueToOther(bufx2);
                    bufx2.DequeueAllAndEnqueueToOther(bufx1);
                }
            },
                                           () => 0), true, 20);

            System.GC.Collect();


            q.Add(new MicroBenchmark <int>("MoveToOtherNonEmpty", 1,
                                           (x, iterations) =>
            {
                FastStreamBuffer <byte> buf1 = new_test_buf(10000);
                FastStreamBuffer <byte> buf2 = new_test_buf(10000);

                for (int i = 0; i < iterations; i++)
                {
                    buf1.DequeueAllAndEnqueueToOther(buf2);
                }
            },
                                           () => 0), true, 20);


            bool call_deque = true;
            long deque_max  = 1;

            q.Add(new MicroBenchmark <int>("Datagram1_single_add", 10000,
                                           (x, iterations) =>
            {
                FastDatagramBuffer <int> dg = new FastDatagramBuffer <int>();

                for (int i = 0; i < iterations; i++)
                {
                    dg.Enqueue(i);
                }

                if (call_deque)
                {
                    for (int i = 0; i < iterations; i++)
                    {
                        var r = dg.Dequeue(deque_max, out long _);
                    }
                }
            },
                                           () => 0), true, 30);



            q.Add(new MicroBenchmark <int>("Datagram1_bulk_add", 10000,
                                           (x, iterations) =>
            {
                FastDatagramBuffer <int> dg = new FastDatagramBuffer <int>();
                int[] tmp = new int[100];

                for (int i = 0; i < iterations; i++)
                {
                    dg.EnqueueAll(tmp);
                }

                if (call_deque)
                {
                    for (int i = 0; i < iterations; i++)
                    {
                        var r = dg.Dequeue(deque_max, out long _);
                    }
                }
            },
                                           () => 0), true, 30);



            q.Run();
        }