示例#1
0
        public void BufferedChannelsSelectReceiveInGoroutine()
        {
            var c = new Chan<int>(10);

            var list = new List<int>();
            var wg = new WaitGroup();

            wg.Add(1);

            GoFunc.Run(() =>
            {
                bool doLoop = true;
                while (doLoop)
                {
                    Select
                        .CaseReceiveOk(c, (i, ok) =>
                        {
                            if (ok)
                                list.Add(i);
                            else
                                doLoop = false;
                        })
                        .NoDefault();
                }

                wg.Done();
            }, "bufferChannelsTest:receiveLoop");

            for (int i = 0; i < 10; i++)
            {
                Select
                    .CaseSend(c, i)
                    .NoDefault();
            }

            c.Close();

            wg.Wait();

            Assert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, list.ToArray());
        }
示例#2
0
        public void TwoSelectsSendAndReceiveCanTalk()
        {
            var c = new Chan<int>();

            int actual = 0;

            var wg = new WaitGroup();
            wg.Add(2);

            GoFunc.Run(() =>
            {
                Select
                    .CaseSend(c, 7)
                    .NoDefault();

                wg.Done();
            });

            GoFunc.Run(() =>
            {
                Select
                    .CaseReceive(c, o => actual = o)
                    .NoDefault();

                wg.Done();
            });

            wg.Wait();

            Assert.AreEqual(7, actual);
        }
        private void BenchmarkTcp(int parallel)
        {
            string topicName = "test_benchmark_" + DateTime.Now.UnixNano();

            try
            {
                const int benchmarkNum = 30000;

                byte[] body = new byte[512];

                var p = new Producer("127.0.0.1:4150");
                p.Connect();

                var startCh = new Chan<bool>();
                var wg = new WaitGroup();

                for (int j = 0; j < parallel; j++)
                {
                    wg.Add(1);
                    //int localj = j;
                    GoFunc.Run(() =>
                    {
                        startCh.Receive();
                        for (int i = 0; i < benchmarkNum / parallel; i++)
                        {
                            //if (i%10 == 0)
                            //{
                            //    Debug.WriteLine(string.Format("{0}: {1}/{2}", localj, i, benchmarkNum/parallel));
                            //}
                            p.Publish(topicName, body);
                        }
                        wg.Done();
                    }, "ProducerBenchmarkTcpTest: sendLoop");
                }

                var stopwatch = Stopwatch.StartNew();
                startCh.Close();

                var done = new Chan<bool>();
                GoFunc.Run(() => { wg.Wait(); done.Send(true); });

                bool finished = false;
                Select
                    .CaseReceive(done, b => finished = b)
                    .CaseReceive(Time.After(TimeSpan.FromSeconds(10)), b => finished = false)
                    .NoDefault();

                stopwatch.Stop();

                if (!finished)
                {
                    Assert.Fail("timeout");
                }

                Console.WriteLine(string.Format("{0:#,0} sent in {1:mm\\:ss\\.fff}; Avg: {2:#,0} msgs/s; Threads: {3}",
                    benchmarkNum, stopwatch.Elapsed, benchmarkNum / stopwatch.Elapsed.TotalSeconds, parallel));
            }
            finally
            {
                _nsqdHttpClient.DeleteTopic(topicName);
                _nsqLookupdHttpClient.DeleteTopic(topicName);
            }
        }
示例#4
0
        public void BufferedChannelsSelectSendInGoroutine()
        {
            var c = new Chan<int>(10);

            var list = new List<int>();
            var wg = new WaitGroup();

            wg.Add(1);

            GoFunc.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    Select
                        .CaseSend(c, i)
                        .NoDefault();
                }

                c.Close();
                wg.Done();
            }, "bufferedChannelsTest:sendLoop");

            wg.Wait();

            bool doLoop = true;
            // ReSharper disable once LoopVariableIsNeverChangedInsideLoop
            while (doLoop)
            {
                Select
                    .CaseReceiveOk(c, (i, ok) =>
                    {
                        if (ok)
                            list.Add(i);
                        else
                            doLoop = false;
                    })
                    .NoDefault();
            }

            Assert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, list.ToArray());
        }
        private void BenchmarkHttp(int parallel)
        {
            string topicName = "test_benchmark_" + DateTime.Now.UnixNano();

            try
            {
                const int benchmarkNum = 30000;

                byte[] body = new byte[512];

                var startCh = new Chan<bool>();
                var wg = new WaitGroup();

                for (int j = 0; j < parallel; j++)
                {
                    wg.Add(1);
                    GoFunc.Run(() =>
                    {
                        startCh.Receive();
                        for (int i = 0; i < benchmarkNum / parallel; i++)
                        {
                            _nsqdHttpClient.Publish(topicName, body);
                        }
                        wg.Done();
                    }, "ProducerBenchmarkHttpTest: sendLoop");
                }

                var stopwatch = Stopwatch.StartNew();
                startCh.Close();
                wg.Wait();
                stopwatch.Stop();

                Console.WriteLine(string.Format("{0:#,0} sent in {1:mm\\:ss\\.fff}; Avg: {2:#,0} msgs/s; Threads: {3}",
                    benchmarkNum, stopwatch.Elapsed, benchmarkNum / stopwatch.Elapsed.TotalSeconds, parallel));
            }
            finally
            {
                _nsqdHttpClient.DeleteTopic(topicName);
                _nsqLookupdHttpClient.DeleteTopic(topicName);
            }
        }