using System;
using System.Net;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static int _count = 0;

        static void Main(string[] args)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 40000;

            int loadCount = (int)(Environment.ProcessorCount * 1.5);

            // CreateTest(loadCount, restThreadFunc);

            CreateTest(loadCount, webclientThreadFunc);
            
            ThreadPool.QueueUserWorkItem(Printthroughput);

            Console.ReadLine();
        }

        private static void Printthroughput(object obj)
        {
            int old = 0;

            while (true)
            {
                Thread.Sleep(1000);

                int current = _count;
                int reqPerSecond = current - old;
                Console.WriteLine("Req / Second: " + reqPerSecond);

                old = current;
            }
        }

        private static void CreateTest(int threadCount, ThreadStart action)
        {
            for (int i = 0; i < threadCount; i ++)
            {
                Thread t = new Thread(action);
                t.IsBackground = true;
                t.Start();
            }
        }

        private static void webclientThreadFunc()
        {
            while (true)
            {
                string url = "http://192.168.0.6:8077/testing/say/hello";
                WebClient wc = new WebClient();
                string result = wc.DownloadString(url);
                Interlocked.Increment(ref _count);
            }
        }

        private static void restThreadFunc()
        {
            while (true)
            {
                RestSharp.RestClient client = new RestSharp.RestClient("http://192.168.0.6:8077");
                RestSharp.RestRequest request = new RestSharp.RestRequest("/testing/say/hello", RestSharp.Method.GET);

                string result = client.Execute(request).Content;
                Interlocked.Increment(ref _count);
            }
        }
    }
}
