Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            const int TotalRun = 10*1000;

            var customerController = new CustomerController();
            var orderController = new OrderController();
            var customer = customerController.Get(1);

            var orders = new List<Order>();
            foreach (var orderId in customer.OrderIds)
            {
            orders.Add(orderController.Get(1, orderId));
            }

            var fullCustomer = new FullCustomer(customer)
            {
            Orders = orders
            };

            var s = JsonConvert.SerializeObject(fullCustomer);
            var bytes = Encoding.UTF8.GetBytes(s);
            var stream = new MemoryStream(bytes);
            var content = new StreamContent(stream);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var stopwatch = Stopwatch.StartNew();
            for (int i = 1; i < TotalRun+1; i++)
            {
            var a = content.ReadAsAsync<FullCustomer>().Result;
            if(i % 100 == 0)
            Console.Write("\r" + i);
            }
            Console.WriteLine();
            Console.WriteLine(stopwatch.Elapsed);

            stopwatch.Restart();
            for (int i = 1; i < TotalRun+1; i++)
            {
            var sa = content.ReadAsStringAsync().Result;
            var a = JsonConvert.DeserializeObject<FullCustomer>(sa);
            if (i % 100 == 0)
            Console.Write("\r" + i);
            }
            Console.WriteLine();
            Console.WriteLine(stopwatch.Elapsed);

            Console.Read();
        }
 public async Task<FullCustomer> GetASync(int id)
 {
     var webClient = new WebClient();
     var customerString = await webClient.DownloadStringTaskAsync(BuildUrl(id));
     var customer = JsonConvert.DeserializeObject<Customer>(customerString);
     var fullCustomer = new FullCustomer(customer);
     var orders = new List<Order>();
     foreach (var orderId in customer.OrderIds)
     {
         var orderString = await webClient.DownloadStringTaskAsync(BuildUrl(id, orderId));
         var order = JsonConvert.DeserializeObject<Order>(orderString);
         orders.Add(order);
     }
     fullCustomer.Orders = orders;
     return fullCustomer;
 }
 public async Task<FullCustomer> GetASyncWebApi(int id)
 {
     var httpClient = new HttpClient();
     httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); 
     var responseMessage = await httpClient.GetAsync(BuildUrl(id));
     var customer = await responseMessage.Content.ReadAsAsync<Customer>();
     var fullCustomer = new FullCustomer(customer);
     var orders = new List<Order>();
     foreach (var orderId in customer.OrderIds)
     {
         responseMessage = await httpClient.GetAsync(BuildUrl(id, orderId));
         var order = await responseMessage.Content.ReadAsAsync<Order>();
         orders.Add(order);
     }
     fullCustomer.Orders = orders;
     return fullCustomer;
 }