프로그래밍/C#

[C#] CPU, DISK I/O 스트레스 테스트

Victory_HA 2024. 10. 18. 10:43
  • CPU, DISK I/O 사용률을 최대로 사용한 상태에서 APP 테스트가 필요하여 사용하게 된 코드입니다.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPU_Disk_PerformanceTEST
{
    public class CPU_DiskPerformanceTEST
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(" 1 : Use Cpu 100% \n 2 : Usage DIsk IO");
            string select = Console.ReadLine();
            if (select == "1")
                ParallelFunc();
            else if (select == "2")
                DiskIOFunc();
            else
            {
                Console.WriteLine("Select 1 or 2");
                return;
            }
        }

        public static void DiskIOFunc()
        {
            try
            {
                DriveInfo[] allDrives = DriveInfo.GetDrives();
                int driveNo = 0;

                Console.WriteLine("Drive List : ");
                foreach (var drive in allDrives)
                {
                    driveNo++;
                    Console.WriteLine($"{driveNo}:{drive.Name}");
                }

                Console.Write("Select Drive No.");
                int SelectDriveNo = int.Parse(Console.ReadLine());
                SelectDriveNo = SelectDriveNo -1;
                string source = $"{allDrives[SelectDriveNo].Name}" + "copy";
                string target = $"{allDrives[SelectDriveNo].Name}" + "copyfile";
                Console.WriteLine($"Source Path: {source}");
                Console.WriteLine($"Target Path: {target}");

                Console.WriteLine("Copy, Paste File Count :");
                int fileCount = int.Parse(Console.ReadLine());

                ExecuteFileIO(fileCount, target);
            }
            catch (Exception e)
            {
                Console.WriteLine($"{e.ToString()}");
                Console.ReadKey();
            }
        }

        public static void ExecuteFileIO(int fileCount, string targetPath)
        {
            var projectPath = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName;
            var copyFilePath = Path.Combine(projectPath, "copy");

            List<string> copyFiles = new List<string>();
            for (int i = 0; i < fileCount; i++)
            {
                copyFiles.Add(copyFilePath + $"{i}");
            }

            if (!Directory.Exists(targetPath))
                Directory.CreateDirectory(targetPath);

            Parallel.ForEach(copyFiles, copyFile => {
                while (true)
                {
                    string fileName = Path.GetFileName(copyFile);
                    string targetFile = targetPath + $"\\{fileName}";
                    File.Copy(copyFilePath, targetFile, true);
                    if (File.Exists(targetFile))
                        File.Delete(targetFile);
                    else
                        continue;
                }
            });
        }

        public static void ParallelFunc()
        {
            string inputString = "";
            Console.WriteLine("Set Thread Count :");

            inputString = Console.ReadLine();
            int threadCount = Convert.ToInt32(inputString);
            Console.WriteLine($"Parallel.For Running Thread Count : {threadCount}");

            Parallel.For(0, threadCount, x =>
            {
                while (true) { }
            });
        }
    }
}

'프로그래밍 > C#' 카테고리의 다른 글

[WPF] GroupBox 안에 2개 이상의 요소 넣기  (0) 2024.10.15
[C#] WPF Func<T>  (0) 2024.09.27
[C#] Path 클래스 활용하기  (0) 2024.06.28
[C#] Linq - GroupBy  (0) 2024.06.28
[C#] JsonSerialize Enum -> String  (0) 2024.06.27