using System; using System.Collections.Generic; using System.Diagnostics; using System.Management; using System.Net.Http; using System.Text.Json.Serialization; using System.Text; using System.Threading; using System.Threading.Tasks; using AipGateway.AIP; using Serilog; using Newtonsoft.Json; using System.IO; namespace AipGateway.Test { class Program { private static string _email = "seungho@ms365.hanteinfo.com"; private static string _apiKey = "24826125-188B-4875-A841-F9146DD267F0"; private static string _aipGuid = "1098bdb0-f2bc-43d3-b9c7-e610431fc1a4"; private static AipFileManager _aipFileManager; private static string[] dispFileNames = { "sample.pptx", "sample-1.pptx", "sample-2.pptx", "sample-3.pptx", "sample-4.pptx", "sample-5.pptx", "sample-6.pptx", "sample-7.pptx", "sample-8.pptx", "sample-9.pptx", "sample-10.pptx", "sample-11.pptx", "sample-12.pptx", "sample-13.pptx", "sample-14.pptx", "sample-15.pptx", "sample-16.pptx", "sample-17.pptx", "sample-18.pptx", "sample-19.pptx", "sample-20.pptx", "sample-21.pptx", "sample-22.pptx", "sample-23.pptx", "sample-24.pptx", "sample-25.pptx", "sample-26.pptx", "sample-27.pptx", "sample-28.pptx", "sample-29.pptx", "sample-30.pptx", }; private static string[] realFileNames = dispFileNames; public static long GetElapsedMilliseconds(long start) { return (long)((Stopwatch.GetTimestamp() - start) * 1000 / (double)Stopwatch.Frequency); } public static void SetSystemConfig() { ThreadPool.GetAvailableThreads(out int workerThreads1, out int completionPortThreads1); ThreadPool.GetMaxThreads(out int workerThreads2, out int completionPortThreads2); ThreadPool.GetMinThreads(out int workerThreads3, out int completionPortThreads3); Log.Information(" AvailableThreads: worker({0}), port({1})", workerThreads1, completionPortThreads1); Log.Information(" MaxThreads: worker({0}), port({1})", workerThreads2, completionPortThreads2); Log.Information(" MinThreads: worker({0}), port({1})", workerThreads3, completionPortThreads3); Log.Information(" UseNagleAlgorithm: {0}", System.Net.ServicePointManager.UseNagleAlgorithm); System.Net.ServicePointManager.UseNagleAlgorithm = false; Log.Information(" UseNagleAlgorithm: {0}, Application Set.", System.Net.ServicePointManager.UseNagleAlgorithm); Log.Information(" Expect100Continue: {0}", System.Net.ServicePointManager.Expect100Continue); System.Net.ServicePointManager.Expect100Continue = false; Log.Information(" Expect100Continue: {0}, Application Set.", System.Net.ServicePointManager.Expect100Continue); Log.Information(" CheckCertificateRevocationList: {0}", System.Net.ServicePointManager.CheckCertificateRevocationList); System.Net.ServicePointManager.CheckCertificateRevocationList = true; Log.Information(" CheckCertificateRevocationList: {0}, Application Set.", System.Net.ServicePointManager.CheckCertificateRevocationList); Log.Information("DefaultPersistentConnectionLimit: {0}", System.Net.ServicePointManager.DefaultPersistentConnectionLimit); Log.Information(" DefaultConnectionLimit: {0}", System.Net.ServicePointManager.DefaultConnectionLimit); System.Net.ServicePointManager.DefaultConnectionLimit = 2048; Log.Information(" DefaultConnectionLimit: {0}, Application Set.", System.Net.ServicePointManager.DefaultConnectionLimit); // Bump up the min threads reserved for this app to ramp connections faster - minWorkerThreads defaults to 4, minIOCP defaults to 4 ThreadPool.SetMinThreads(100, 100); // Change max connections from .NET to a remote service default: 2 //System.Net.ServicePointManager.DefaultConnectionLimit = 65000; // Turn off the Expect 100 to continue message - 'true' will cause the caller to wait until it round-trip confirms a connection to the server //System.Net.ServicePointManager.Expect100Continue = false; // Can decrease overall transmission overhead but can cause delay in data packet arrival //System.Net.ServicePointManager.UseNagleAlgorithm = false; } static void Main(string[] args) { AipConfig aipConfig = new AipConfig { AppName = "AIP.Gateway", // 어플리케이션 이름 AppVersion = "1.0.0", // 어플리케이션 버전 ClientId = "0e225915-3be3-419c-aa04-284d7de5e16b", // 어플리케이션 ID(클라이언트 ID) TenantId = "2e58414a-c6ae-43ff-aaf5-45ab8b78a404", // 디렉토리(테넌트) ID MipData = "c:\\temp\\app_data\\mip_data", // Mip Data Path Domain = "AIP.Gateway", // Domain EMail = "seungho@ms365.hanteinfo.com", // User E-Mail SecretValue = "CvW8Q~0iANtLN1Y2EXR_nVyYb_tQTDwjW-Z7Ndg3", // 인증 비밀번호 CertThumbPrint = "C:\\Data\\SSL\\mip\\mip_gateway.pfx" // 인증서 지문 }; string tempLoginType = "authLoginCert"; AipAuthLoginType loginType = (AipAuthLoginType)Enum.Parse(typeof(AipAuthLoginType), tempLoginType, true); aipConfig.LoginType = loginType; var serillogLogger = new LoggerConfiguration() .ReadFrom.AppSettings(filePath: AppDomain.CurrentDomain.BaseDirectory + @"App.config") .WriteTo.Logger(cfg => cfg.ReadFrom.AppSettings(filePath: AppDomain.CurrentDomain.BaseDirectory + @"serilog.config")) .MinimumLevel.Information() .Enrich.FromLogContext() //.WriteTo.Console() .CreateLogger(); Log.Logger = serillogLogger.ForContext(); SetSystemConfig(); //Log.Error("{0}", args); if (args.Length > 0) { int.TryParse(args[0], out int idx); if (idx < 0 || idx > dispFileNames.Length) { Log.Error("Argument index error: {0}", idx); return; } string dispFileName = "c:\\data\\source\\" + dispFileNames[idx]; string realFileName = "c:\\data\\source\\" + dispFileNames[idx]; string actualFileName = "c:\\data\\target\\" + dispFileNames[idx]; string fmtIdx = $"mip_data_{idx}"; AipConfig aipConfigChild = new AipConfig { AppName = "AIP.Gateway", // 어플리케이션 이름 AppVersion = "1.0.0", // 어플리케이션 버전 ClientId = "0e225915-3be3-419c-aa04-284d7de5e16b", // 어플리케이션 ID(클라이언트 ID) TenantId = "2e58414a-c6ae-43ff-aaf5-45ab8b78a404", // 디렉토리(테넌트) ID MipData = "c:\\temp\\app_data\\" + fmtIdx, // Mip Data Path Domain = "AIP.Gateway", // Domain EMail = "seungho@ms365.hanteinfo.com", // User E-Mail SecretValue = "CvW8Q~0iANtLN1Y2EXR_nVyYb_tQTDwjW-Z7Ndg3", // 인증 비밀번호 CertThumbPrint = "C:\\Data\\SSL\\mip\\mip_gateway.pfx" // 인증서 지문 }; _aipFileManager = new AipFileManager(serillogLogger, aipConfigChild); _aipFileManager.Initialize(); _aipFileManager.CreateProfile(); _aipFileManager.CreateEngine(); SetLabel(dispFileName, realFileName, actualFileName); Thread.Sleep(30000); return; } Log.Information("Worker Thread Total {0} EA.", dispFileNames.Length); for (int ii = 0; ii < dispFileNames.Length; ii++) { //StartChildProcess(ii); } _aipFileManager = new AipFileManager(serillogLogger, aipConfig); _aipFileManager.Initialize(); _aipFileManager.CreateProfile(); _aipFileManager.CreateEngine(); Log.Information("SensitivityLabels======================================================================="); List aipLabels = _aipFileManager.SensitivityLabels(); foreach (var label in aipLabels) { Log.Information("ID: {0}, NAME: {1}", label.Id, label.Name); if (label.Children.Count > 0) { foreach (AipLabel child in label.Children) { Log.Information("\tID: {0}, NAME: {1}", child.Id, child.Name); } } } Log.Information("ListSensitivityLabels======================================================================="); List labels2 = _aipFileManager.ListSensitivityLabels(); foreach (var v in labels2) { Log.Information("Label List: {0}, {1}, {2}", v.Id, v.Name, v.IsActive); if (v.Children.Count > 0) { foreach (AipLabel child in v.Children) { Log.Information("\tID: {0}, NAME: {1}", child.Id, child.Name); } } } Log.Information("GetTemplates======================================================================="); List templates = _aipFileManager.GetTemplates(); foreach (var v in templates) { Log.Information("Template: {0}, {1}, {2}", v.Id, v.Name, v.Description); } Log.Information("======================================================================="); ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystemProduct"); foreach (ManagementObject obj in searcher.Get()) { string uniqueId = obj["UUID"].ToString(); Log.Information("유일한 식별자 (UUID): {0}", uniqueId); } Log.Information("======================================================================="); Log.Information("=======================================================================\r\n\r\n"); int qryMode = 2; if (args.Length > 0) { int.TryParse(args[0], out qryMode); } if (qryMode == 0) { SetLabelsAll(); } else if (qryMode == 1) { Test1(); //SetLabelsAllAsync(99); } else if (qryMode == 2) { SetLabelsApiCallAllAsync(); } #if false Log.Information("Waiting Child Process ======================================================================="); var currentProcessPath = Process.GetCurrentProcess().MainModule.FileName; Process[] precesses = Process.GetProcessesByName(currentProcessPath); while (precesses != null && precesses.Length != 0 && precesses.Length != 1) { foreach (var process in precesses) { process.EnableRaisingEvents = true; process.Exited += (sender, eventArgs) => { Log.Information("Child process {0} has exited.", process.Id); }; } precesses = Process.GetProcessesByName(currentProcessPath); } #endif Log.Information("Press a key to continue..."); Console.ReadKey(); Log.CloseAndFlush(); } public static void SetLabelsAll() { var startTotal = Stopwatch.GetTimestamp(); Log.Information("SetLabel Tatal {0} EA. Start.", dispFileNames.Length); for (int ii = 0; ii < dispFileNames.Length; ii++) { string dispFileName = "c:\\data\\source\\" + dispFileNames[ii]; string realFileName = "c:\\data\\source\\" + dispFileNames[ii]; string actualFileName = "c:\\data\\target\\" + dispFileNames[ii]; var start = Stopwatch.GetTimestamp(); //Log.Information("SetLabel Start: {0}", dispFileName); _aipFileManager.SetLabel(realFileName, actualFileName, _email, _aipGuid, "", "string"); Log.Information("SetLabel ..End: {0} ms. {1}", GetElapsedMilliseconds(start), dispFileName); } Log.Information("SetLabel Tatal {0} EA. ..Stop. {1} ms.", dispFileNames.Length, GetElapsedMilliseconds(startTotal)); } public static void SetLabel(string dispFileName, string realFileName, string actualFileName) { var start = Stopwatch.GetTimestamp(); Log.Information("SetLabel Start: {0}", dispFileName); _aipFileManager.SetLabel(realFileName, actualFileName, _email, _aipGuid, "", "string"); Log.Information("SetLabel ..End: {0} ms. {1}", GetElapsedMilliseconds(start), dispFileName); } public static async void Test1() { var startTotal = Stopwatch.GetTimestamp(); int reqIdx = 0; await SetLabelsAllAsync(reqIdx++); await SetLabelsAllAsync(reqIdx++); await SetLabelsAllAsync(reqIdx++); await SetLabelsAllAsync(reqIdx++); await SetLabelsAllAsync(reqIdx++); Log.Error("&&&&&&&&&&&&&&&&&&&&&&&&&&&&& SetLabel Tatal {0} EA. ..Stop. {1} ms.", dispFileNames.Length, GetElapsedMilliseconds(startTotal)); } public static async Task SetLabelsAllAsync(int reqIdx) { var startTotal = Stopwatch.GetTimestamp(); Log.Error("SetLabelsAllAsync Tatal {0} EA. Start.{1}.", dispFileNames.Length, reqIdx); List result = new List(); var tasks = new List>(); for (int ii = 0; ii < dispFileNames.Length; ii++) { string dispFileName = "c:\\data\\source\\" + dispFileNames[ii]; string realFileName = "c:\\data\\source\\" + dispFileNames[ii]; string actualFileName = "c:\\data\\target\\" + dispFileNames[ii]; //SetFileInfo result = _aipFileManager.SetLabel(realFileName, actualFileName, aipConfig.EMail, _aipGuid, "", "string"); tasks.Add( Task.Run(() => { //var start = Stopwatch.GetTimestamp(); //Log.Information("SetLabel Start: {0}", dispFileName); Thread.Sleep(100); SetFileInfo fileInfo = _aipFileManager.SetLabel(realFileName, actualFileName, _email, _aipGuid, "", "string"); //Log.Information("SetLabel ..End: {0} ms. {1}", GetElapsedMilliseconds(start), dispFileName); return fileInfo; })); } await Task.WhenAll(tasks); Log.Error("====================== SetLabelsAllAsync Tatal {0} EA. ..Stop. {1}, {2} ms.", dispFileNames.Length, reqIdx, GetElapsedMilliseconds(startTotal)); return true; } static async void SetLabelsApiCallAllAsync() { var startTotal = Stopwatch.GetTimestamp(); //Log.Information("SetLabel Tatal {0} EA. Start.", dispFileNames.Length); int reqIdx = 0; await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); await SetLabelsApiCallAsync(reqIdx++); Log.Error("&&&&&&&&&&&&&&&&&&&&&&&&&&&&& SetLabel Tatal {0} EA. ..Stop. {1} ms.", dispFileNames.Length, GetElapsedMilliseconds(startTotal)); } static async Task SetLabelsApiCallAsync(int reqIdx) { var startTotal = Stopwatch.GetTimestamp(); Log.Error("SetLabelsApiCallAsync Tatal {0} EA. Start. {1}.", dispFileNames.Length, reqIdx); HttpClient _httpClient = new HttpClient(new HttpClientHandler { MaxConnectionsPerServer = int.MaxValue }); _httpClient.DefaultRequestHeaders.ConnectionClose = false; _httpClient.Timeout = TimeSpan.FromSeconds(20); int Port = 7000; int seq = 0; List result = new List(); var tasks = new List>>(); for (int ii = 0; ii < dispFileNames.Length; ii++) { string dispFileName = dispFileNames[ii]; string realFileName = dispFileNames[ii]; string actualFileName = dispFileNames[ii]; RequestFileSet reqFileSet = new RequestFileSet { apiKey = _apiKey, email = _email, aipGuid = _aipGuid, comment = "comment", file = new RequestFile { dispFileName = dispFileName, realFileName = realFileName, } }; var json = JsonConvert.SerializeObject(reqFileSet); var content = new StringContent(json, Encoding.UTF8, "application/json"); string reqUri = $"http://localhost:{Port+ seq++}/api/v1/file/set-label"; if (seq >= 31) seq = 0; //Log.Information($"{reqUri}"); //var start0 = Stopwatch.GetTimestamp(); //SetFileInfo tempInfo = SendRequestsAsync(_httpClient, reqUri, reqFileSet).Result; //Log.Information("SetLabel Start: {0}", DateTime.Now); //var response = await _httpClient.PostAsync(reqUri, content);//.ConfigureAwait(false); //Log.Information("SetLabel ..End: {0} ms. {1}", GetElapsedMilliseconds(start0), reqUri); tasks.Add(Task.Run(() => { var start = Stopwatch.GetTimestamp(); ApiResponseModel fileInfo = SendRequestsAsync(_httpClient, reqUri, reqFileSet).Result; //Log.Information("SetLabel ..End: {0} ms. {1}", GetElapsedMilliseconds(start), JsonConvert.SerializeObject(fileInfo)); return fileInfo; })); } await Task.WhenAll(tasks); Log.Error("**************** SetLabelsApiCallAsync Tatal {0} EA. ..Stop. {1}, {2} ms.", dispFileNames.Length, reqIdx, GetElapsedMilliseconds(startTotal)); return true; } private static async Task> SendRequestsAsync(HttpClient httpClient, string reqUri, RequestFileSet reqFileSet) { var content = new StringContent(JsonConvert.SerializeObject(reqFileSet), Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(reqUri, content);//.ConfigureAwait(false); if (response.IsSuccessStatusCode) { Log.Information($"Request successful for: {reqFileSet.file.dispFileName}"); } else { Log.Error($"Request failed for: {reqFileSet.file.dispFileName}. Status code: {response.StatusCode}"); } //Log.Error("{0}", JsonConvert.SerializeObject(response.Content.ToString())); //string stringContents = await response.Content.ReadAsStringAsync(); //Log.Error($"Contents: {stringContents}"); //return JsonConvert.DeserializeObject>(stringContents); return JsonConvert.DeserializeObject>(await response.Content.ReadAsStringAsync()); } static void StartChildProcess(int idx) { var currentProcessPath = Process.GetCurrentProcess().MainModule.FileName; var startInfo = new ProcessStartInfo { FileName = currentProcessPath, Arguments = $"{idx}", CreateNoWindow = true, UseShellExecute = false }; Process prcs = Process.Start(startInfo); Log.Information("Worker Thread Start {0} ==> {1}, {2}, {3}.", idx, prcs.Id, prcs.SessionId, prcs.ProcessName); } } }