示例#1
0
        // use explicitly created tasks
        public static void Main(string[] args)
        {
            bool      executeOnce = false;
            const int partial     = REQ_BATCH_COUNT / 4;

            // set the minimum thread pool's worker threads to theh MAX_DEGREE_OF_PARALLELISM
            int worker, iocp;

            ThreadPool.GetMinThreads(out worker, out iocp);
            ThreadPool.SetMinThreads(worker < MAX_DEGREE_OF_PARALLELISM ? MAX_DEGREE_OF_PARALLELISM : worker, iocp);

            string text = (args.Length > 0) ? args[0] : "original paths are hard ok";

            Task[]    tasksCreate   = new Task[partial];
            Task[]    tasksPut      = new Task[partial];
            Task[]    tasksTake     = new Task[partial];
            Task[]    tasksTransfer = new Task[partial];
            Stopwatch sw            = Stopwatch.StartNew();

            //do
            //{
            for (int i = 0; i < partial; i++)
            {
                tasksCreate[i] = SendRequestAndReceiveResponseAsync("localhost",
                                                                    new RequestPayload {
                    Number = i, Text = text + i, Method = "CREATE"
                });
            }
            Task.WaitAll(tasksCreate);
            for (int i = 0; i < partial; i++)
            {
                int id = i + partial;
                tasksPut[i] = SendRequestAndReceiveResponseAsync("localhost",
                                                                 new RequestPayload {
                    Number = id, Text = text + i, Method = "PUT"
                });
            }
            for (int i = 0; i < partial; i++)
            {
                int id = i + partial * 2;
                tasksTransfer[i] = SendRequestAndReceiveResponseAsync("localhost",
                                                                      new RequestPayload {
                    Number = id, Text = text + i, Method = "TRANSFER"
                });
            }
            for (int i = 0; i < partial; i++)
            {
                int id = i + partial * 3;
                tasksTake[i] = SendRequestAndReceiveResponseAsync("localhost",
                                                                  new RequestPayload {
                    Number = id, Text = text + i, Method = "TAKE"
                });
            }
            List <Task> tasks = new List <Task>();

            tasks.AddRange(tasksPut);
            tasks.AddRange(tasksTransfer);
            tasks.AddRange(tasksTake);
            //there will be races between put and transfer so timeouts are expected
            Task.WaitAll(tasks.ToArray());
            SendRequestAndReceiveResponseAsync("localhost", new RequestPayload {
                Number = -3, Text = "should have status 405 operation non existent", Method = "IDKSTFU"
            }).Wait();
            SendRequestAndReceiveResponseAsync("localhost", new RequestPayload {
                Number = -1, Text = "shutdown msg", Method = "SHUTDOWN"
            }).Wait();
            //} while (!(executeOnce || Console.KeyAvailable));
            Console.WriteLine("--completed requests: {0} / {1} ms", Volatile.Read(ref requestCount), sw.ElapsedMilliseconds);
            Console.ReadKey();
        }