예제 #1
0
        public static async Task Main(string[] args)
        {
            using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder
                                                                      .AddConsole());

            ILogger logger = loggerFactory.CreateLogger <Program>();

            try
            {
                CTLConfig config = CTLConfig.From(args);

                SetEnvironmentVariables(config);

                if (config.OutputEventTraces)
                {
                    EnableTraceSourcesToConsole();
                }

                using CosmosClient client = config.CreateCosmosClient();

                string loggingContextIdentifier = $"{config.WorkloadType}{config.LogginContext}";
                using (logger.BeginScope(loggingContextIdentifier))
                {
                    IMetricsRoot metrics = ConfigureReporting(config, logger);

                    ICTLScenario scenario = CreateScenario(config.WorkloadType);

                    using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

                    await scenario.InitializeAsync(
                        config : config,
                        cosmosClient : client,
                        logger : logger);

                    logger.LogInformation("Initialization completed.");

                    if (client.ClientOptions.EnableClientTelemetry.GetValueOrDefault())
                    {
                        logger.LogInformation("Telemetry is enabled for CTL.");
                    }
                    else
                    {
                        logger.LogInformation("Telemetry is disabled for CTL.");
                    }

                    List <Task> tasks = new List <Task>
                    {
                        scenario.RunAsync(
                            config: config,
                            cosmosClient: client,
                            logger: logger,
                            metrics: metrics,
                            loggingContextIdentifier: loggingContextIdentifier,
                            cancellationToken: cancellationTokenSource.Token),

                        Task.Run(async() =>
                        {
                            // Measure CPU/memory
                            Process process = Process.GetCurrentProcess();

                            GaugeOptions processPhysicalMemoryGauge = new GaugeOptions
                            {
                                Name            = "Process Working Set",
                                MeasurementUnit = Unit.Bytes,
                                Context         = loggingContextIdentifier
                            };

                            GaugeOptions totalCpuGauge = new GaugeOptions
                            {
                                Name            = "Total CPU",
                                MeasurementUnit = Unit.Percent,
                                Context         = loggingContextIdentifier
                            };

                            GaugeOptions priviledgedCpuGauge = new GaugeOptions
                            {
                                Name            = "Priviledged CPU",
                                MeasurementUnit = Unit.Percent,
                                Context         = loggingContextIdentifier
                            };

                            GaugeOptions userCpuGauge = new GaugeOptions
                            {
                                Name            = "User CPU",
                                MeasurementUnit = Unit.Percent,
                                Context         = loggingContextIdentifier
                            };

                            DateTime lastTimeStamp               = process.StartTime;
                            TimeSpan lastTotalProcessorTime      = TimeSpan.Zero;
                            TimeSpan lastUserProcessorTime       = TimeSpan.Zero;
                            TimeSpan lastPrivilegedProcessorTime = TimeSpan.Zero;

                            while (!cancellationTokenSource.Token.IsCancellationRequested)
                            {
                                await Task.Delay(TimeSpan.FromSeconds(config.ReportingIntervalInSeconds));
                                process.Refresh();

                                double totalCpuTimeUsed      = process.TotalProcessorTime.TotalMilliseconds - lastTotalProcessorTime.TotalMilliseconds;
                                double privilegedCpuTimeUsed = process.PrivilegedProcessorTime.TotalMilliseconds - lastPrivilegedProcessorTime.TotalMilliseconds;
                                double userCpuTimeUsed       = process.UserProcessorTime.TotalMilliseconds - lastUserProcessorTime.TotalMilliseconds;

                                lastTotalProcessorTime      = process.TotalProcessorTime;
                                lastPrivilegedProcessorTime = process.PrivilegedProcessorTime;
                                lastUserProcessorTime       = process.UserProcessorTime;

                                double cpuTimeElapsed = (DateTime.UtcNow - lastTimeStamp).TotalMilliseconds * Environment.ProcessorCount;
                                lastTimeStamp         = DateTime.UtcNow;

                                metrics.Measure.Gauge.SetValue(totalCpuGauge, totalCpuTimeUsed * 100 / cpuTimeElapsed);
                                metrics.Measure.Gauge.SetValue(priviledgedCpuGauge, privilegedCpuTimeUsed * 100 / cpuTimeElapsed);
                                metrics.Measure.Gauge.SetValue(userCpuGauge, userCpuTimeUsed * 100 / cpuTimeElapsed);
                                metrics.Measure.Gauge.SetValue(processPhysicalMemoryGauge, process.WorkingSet64);

                                await Task.WhenAll(metrics.ReportRunner.RunAllAsync());
                            }
                        })
                    };

                    await Task.WhenAny(tasks);

                    cancellationTokenSource.Cancel();
                    // Final report
                    await Task.WhenAll(metrics.ReportRunner.RunAllAsync());

                    logger.LogInformation($"{nameof(CosmosCTL)} completed successfully.");
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Unhandled exception during execution");
            }
        }
        public static async Task Main(string[] args)
        {
            using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder
                                                                      .AddConsole());

            ILogger logger = loggerFactory.CreateLogger <Program>();

            try
            {
                CTLConfig config = CTLConfig.From(args);
                if (config.OutputEventTraces)
                {
                    EnableTraceSourcesToConsole();
                }

                using CosmosClient client = config.CreateCosmosClient();

                using (logger.BeginScope(config.WorkloadType))
                {
                    IMetricsRoot metrics = ConfigureReporting(config, logger);

                    ICTLScenario scenario = CreateScenario(config.WorkloadType);

                    using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

                    await scenario.InitializeAsync(
                        config : config,
                        cosmosClient : client,
                        logger : logger);

                    List <Task> tasks = new List <Task>
                    {
                        scenario.RunAsync(
                            config: config,
                            cosmosClient: client,
                            logger: logger,
                            metrics: metrics,
                            cancellationToken: cancellationTokenSource.Token),

                        Task.Run(async() =>
                        {
                            while (!cancellationTokenSource.Token.IsCancellationRequested)
                            {
                                await Task.Delay(TimeSpan.FromSeconds(config.ReportingIntervalInSeconds));
                                await Task.WhenAll(metrics.ReportRunner.RunAllAsync());
                            }
                        })
                    };

                    await Task.WhenAny(tasks);

                    cancellationTokenSource.Cancel();
                    // Final report
                    await Task.WhenAll(metrics.ReportRunner.RunAllAsync());

                    logger.LogInformation($"{nameof(CosmosCTL)} completed successfully.");
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Unhandled exception during execution");
            }
        }