첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ Program.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// HTTP 클라이언트
        /// </summary>
        private static HttpClient _httpClient = new HttpClient
        {
            MaxResponseContentBufferSize = 1_000_000
        };

        /// <summary>
        /// URL 리스트
        /// </summary>
        private static List<string> _urlList = new List<string>
        {
            "https://docs.microsoft.com",
            "https://docs.microsoft.com/aspnet/core",
            "https://docs.microsoft.com/azure",
            "https://docs.microsoft.com/azure/devops",
            "https://docs.microsoft.com/dotnet",
            "https://docs.microsoft.com/dynamics365",
            "https://docs.microsoft.com/education",
            "https://docs.microsoft.com/enterprise-mobility-security",
            "https://docs.microsoft.com/gaming",
            "https://docs.microsoft.com/graph",
            "https://docs.microsoft.com/microsoft-365",
            "https://docs.microsoft.com/office",
            "https://docs.microsoft.com/powershell",
            "https://docs.microsoft.com/sql",
            "https://docs.microsoft.com/surface",
            "https://docs.microsoft.com/system-center",
            "https://docs.microsoft.com/visualstudio",
            "https://docs.microsoft.com/windows",
            "https://docs.microsoft.com/xamarin"
        };

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 컨텐트 길이 구하기 (비동기) - GetContentLengthAsync(httpClient, url)

        /// <summary>
        /// 컨텐트 길이 구하기 (비동기)
        /// </summary>
        /// <param name="httpClient">HTTP 클라이언트</param>
        /// <param name="url">URL</param>
        /// <returns>컨텐트 길이 태스크</returns>
        private static async Task<int> GetContentLengthAsync(HttpClient httpClient, string url)
        {
            HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(url);

            byte[] contentByteArray = await httpResponseMessage.Content.ReadAsByteArrayAsync();

            Console.WriteLine($"{url, -60} {contentByteArray.Length, 10:#,#}");

            return contentByteArray.Length;
        }

        #endregion
        #region 전체 컨텐트 길이 출력하기 (비동기) - PrintTotalContentLengthAsync()

        /// <summary>
        /// 전체 컨텐트 길이 출력하기 (비동기)
        /// </summary>
        /// <returns>태스크</returns>
        private static async Task PrintTotalContentLengthAsync()
        {
            Stopwatch watch = Stopwatch.StartNew();

            IEnumerable<Task<int>> taskEnumerable = from url in _urlList
                                                    select GetContentLengthAsync(_httpClient, url);

            List<Task<int>> taskList = taskEnumerable.ToList();

            int totalLength = 0;

            while(taskList.Any())
            {
                Task<int> taskCompleted = await Task.WhenAny(taskList);

                taskList.Remove(taskCompleted);

                totalLength += await taskCompleted;
            }

            watch.Stop();

            Console.WriteLine($"전체 바이트 : {totalLength:#,#}");
            Console.WriteLine($"경과 시간   : {watch.Elapsed}");
        }

        #endregion
        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        /// <returns>태스크</returns>
        private static async Task Main() => await PrintTotalContentLengthAsync();

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요