Ejemplo n.º 1
0
            public static async Task <Arm11LocalSystemCapabilities> Load(IReadOnlyBinaryDataAccessor data)
            {
                var capabilities = new Arm11LocalSystemCapabilities
                {
                    ProgramId   = await data.ReadInt64Async(0),
                    CoreVersion = await data.ReadInt32Async(0x8),
                    Flag1       = await data.ReadByteAsync(0xC),
                    Flag2       = await data.ReadByteAsync(0xD),
                    Flag0       = await data.ReadByteAsync(0xE),
                    Priority    = await data.ReadByteAsync(0xF),
                    ResourceLimitDescriptors = await data.ReadArrayAsync(0x10, 0x20),
                    StorageInformation       = await StorageInfo.Load(data.Slice(0x30, 0x20)),
                };

                var accessControl = new long[32];
                await AsyncFor.For(0, 32 - 1, async i =>
                {
                    accessControl[i] = await data.ReadInt64Async(0x50 + 8 * i);
                });

                capabilities.ServiceAccessControl         = accessControl;
                capabilities.ExtendedServiceAccessControl = new long[] { await data.ReadInt64Async(0x150), await data.ReadInt64Async(0x158) };
                capabilities.Reserved = await data.ReadArrayAsync(0x160, 0xF);

                capabilities.ResourceLimitCategory = await data.ReadByteAsync(0x16F);

                return(capabilities);
            }
Ejemplo n.º 2
0
        private async Task RampUpAsync(TestRunInfo testRunInfo)
        {
            // Handle ramp up if defined
            if (testRunInfo.RampUpTimeSeconds > 4 && !_terminate)
            {
                Tracer.TraceInfo($"Ramping up starts.");

                DateTime startTime        = DateTime.Now;
                DateTime endTime          = startTime + TimeSpan.FromSeconds(testRunInfo.RampUpTimeSeconds);
                int      numberIntervals  = Math.Min(testRunInfo.RampUpTimeSeconds / 5, 6);
                TimeSpan intervalLength   = (endTime - startTime) / numberIntervals;
                double   intervalRpsDelta = ((double)testRunInfo.TargetRPS) / ((double)numberIntervals);
                for (int i = 0; i < numberIntervals && !_terminate; i++)
                {
                    var apiInfo = _mixInfo.ApiMix[i % _mixInfo.ApiMix.Count];

                    long intervalRps = (long)Math.Round((i + 1) * intervalRpsDelta);
                    Tracer.TraceInfo($"Ramping up. RPS = {intervalRps}");

                    AsyncFor myRampUpFor = new AsyncFor(intervalRps, GetResourceDescription(apiInfo, _mixInfo), GetTestDescription(apiInfo), testRunInfo.MeasureServerSideTime);
                    myRampUpFor.PerSecondMetricsAvailable += new ConsoleMetricsHandler().MetricsAvailableHandler;
                    _asyncForInstances.Add(myRampUpFor);

                    await myRampUpFor.For(intervalLength, testRunInfo.SimultaneousConnections, new MaaServiceApiCaller(apiInfo, _mixInfo.ProviderMix, testRunInfo.EnclaveInfoFile, testRunInfo.ForceReconnects).CallApi);
                }
                Tracer.TraceInfo($"Ramping up complete.");
            }
        }
 public async Task ThrowsOnSimultaneousInstanceUsage()
 {
     var sum        = 0;
     var lockObject = new object();
     var f          = new AsyncFor();
     var first      = f.RunFor(0, 10, async i =>
     {
         lock (lockObject)
         {
             sum += i;
         }
         await Task.Delay(100);
     });
     await Assert.ThrowsAsync <InvalidOperationException>(async() =>
     {
         await f.RunFor(0, 10, async i =>
         {
             lock (lockObject)
             {
                 sum += i;
             }
             await Task.Delay(100);
         });
     });
 }
Ejemplo n.º 4
0
        public async Task ExtractFiles(string directoryName, IFileSystem fileSystem, ExtractionProgressedToken progressReportToken = null)
        {
            if (progressReportToken != null)
            {
                progressReportToken.TotalFileCount = Headers.Count(h => h != null && !string.IsNullOrEmpty(h.Filename));
            }

            if (!fileSystem.DirectoryExists(directoryName))
            {
                fileSystem.CreateDirectory(directoryName);
            }

            var a = new AsyncFor();
            await a.RunForEach(Headers, async (header) =>
            {
                if (string.IsNullOrEmpty(header.Filename))
                {
                    return;
                }

                fileSystem.WriteAllBytes(Path.Combine(directoryName, header.Filename), await ExeFsData.ReadArrayAsync(0x200 + header.Offset, header.FileSize));

                if (progressReportToken != null)
                {
                    progressReportToken.IncrementExtractedFileCount();
                }
            });
        }
Ejemplo n.º 5
0
        public async Task RunAsync()
        {
            // Complete all HTTP conversations before exiting application
            Console.CancelKeyPress += HandleControlC;

            // Retrieve all run configuration settings
            _mixInfo = _options.GetMixInfo();
            _mixInfo.Trace();

            using (var uberCsvAggregator = new CsvAggregatingMetricsHandler())
            {
                for (int i = 0; i < _mixInfo.TestRuns.Count && !_terminate; i++)
                {
                    var testRunInfo  = _mixInfo.TestRuns[i];
                    var asyncRunners = new List <Task>();

                    uberCsvAggregator.SetRpsAndConnections(testRunInfo.TargetRPS, testRunInfo.SimultaneousConnections);

                    Tracer.TraceInfo("");
                    Tracer.TraceInfo($"Starting test run #{i}   RPS: {testRunInfo.TargetRPS}  Connections: {testRunInfo.SimultaneousConnections}");

                    // Handle ramp up if needed
                    await RampUpAsync(testRunInfo);

                    // Initiate separate asyncfor for each API in the mix
                    if (!_terminate)
                    {
                        foreach (var apiInfo in _mixInfo.ApiMix)
                        {
                            var myFor = new AsyncFor(testRunInfo.TargetRPS * apiInfo.Percentage, GetResourceDescription(apiInfo, _mixInfo), GetTestDescription(apiInfo), testRunInfo.MeasureServerSideTime);
                            if (_mixInfo.ApiMix.Count > 1)
                            {
                                myFor.PerSecondMetricsAvailable += new ConsoleAggregatingMetricsHandler(_mixInfo.ApiMix.Count, 60).MetricsAvailableHandler;
                            }
                            else
                            {
                                myFor.PerSecondMetricsAvailable += new ConsoleMetricsHandler().MetricsAvailableHandler;
                                myFor.PerSecondMetricsAvailable += new CsvFileMetricsHandler().MetricsAvailableHandler;
                            }

                            if (testRunInfo.TestTimeSeconds != int.MaxValue)
                            {
                                myFor.PerSecondMetricsAvailable += uberCsvAggregator.MetricsAvailableHandler;
                            }

                            _asyncForInstances.Add(myFor);
                            asyncRunners.Add(myFor.For(TimeSpan.FromSeconds(testRunInfo.TestTimeSeconds), testRunInfo.SimultaneousConnections, new MaaServiceApiCaller(apiInfo, _mixInfo.ProviderMix, testRunInfo.EnclaveInfoFile, testRunInfo.ForceReconnects).CallApi));
                        }
                    }

                    // Wait for all to be complete (happens when crtl-c is hit)
                    await Task.WhenAll(asyncRunners.ToArray());

                    Tracer.TraceInfo("");
                    Tracer.TraceInfo($"Completed test run #{i}   RPS: {testRunInfo.TargetRPS}  Connections: {testRunInfo.SimultaneousConnections}");
                }
            }
            Tracer.TraceInfo($"Organized shutdown complete.");
        }
Ejemplo n.º 6
0
        public async Task RunAsync()
        {
            await Authentication.Authentication.AcquireAccessTokenAsync("microsoft.com", false);

            AsyncFor myFor = new AsyncFor(_options.TargetRPS, "MAA SGX Attest");

            myFor.PerSecondMetricsAvailable += new ConsoleMetricsHandler().MetricsAvailableHandler;
            myFor.PerSecondMetricsAvailable += new CsvFileMetricsHandler().MetricsAvailableHandler;
            await myFor.For(TimeSpan.MaxValue, _options.SimultaneousConnections, CallAttestSgx);
        }
        public async Task RunsForEveryItemInGenericEnumerableWithSynchronousDelegate_InstanceMethod()
        {
            var sampleData = Enumerable.Repeat(new TestClass(), 5);

            var f = new AsyncFor();
            await f.RunForEach(sampleData, data =>
            {
                data.Success = true;
            });

            Assert.All(sampleData, data => Assert.True(data.Success));
        }
        public async Task RunsBackwardsWithNegativeStepCount()
        {
            var sum        = 0;
            var lockObject = new object();
            await AsyncFor.For(10, 0, i =>
            {
                lock (lockObject)
                {
                    sum += i;
                }
            }, stepCount : -2);

            Assert.Equal(30, sum);
        }
Ejemplo n.º 9
0
            public static async Task <Arm11KernelCapabilities> Load(IReadOnlyBinaryDataAccessor data)
            {
                var capabilities = new Arm11KernelCapabilities();
                var descriptors  = new int[28];
                await AsyncFor.For(0, 28 - 1, async i =>
                {
                    descriptors[i] = await data.ReadInt32Async(i * 4);
                });

                capabilities.Descriptors = descriptors;
                capabilities.Reserved    = await data.ReadArrayAsync(0x70, 0x10);

                return(capabilities);
            }
        public async Task RunsForEveryNumberWithSynchronousDelegate_StaticMethod()
        {
            var sum        = 0;
            var lockObject = new object();
            await AsyncFor.For(0, 10, i =>
            {
                lock (lockObject)
                {
                    sum += i;
                }
            });

            Assert.Equal(55, sum);
        }
        public async Task DoesNothingWhenEndComesBeforeStart()
        {
            var sum        = 0;
            var lockObject = new object();
            await AsyncFor.For(0, -10, i =>
            {
                lock (lockObject)
                {
                    sum += i;
                }
            });

            Assert.Equal(0, sum);
        }
        public async Task UsesStepCount()
        {
            var sum        = 0;
            var lockObject = new object();
            await AsyncFor.For(0, 10, i =>
            {
                lock (lockObject)
                {
                    sum += i;
                }
            }, stepCount : 2);

            Assert.Equal(30, sum);
        }
        public async Task ReportsProgressThroughInstanceProperties()
        {
            var f = new AsyncFor();
            await f.RunFor(0, 6, i =>
            {
                Assert.False(f.IsCompleted);
                Assert.False(f.IsIndeterminate);
                Assert.True(f.Progress < 1);
            });

            Assert.True(f.IsCompleted);
            Assert.False(f.IsIndeterminate);
            Assert.Equal(1, f.Progress, precision: 1);
        }
 public async Task ThrowsOnZeroStepCount()
 {
     var sum        = 0;
     var lockObject = new object();
     await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
     {
         await AsyncFor.For(0, 10, i =>
         {
             lock (lockObject)
             {
                 sum += i;
             }
         }, stepCount: 0);
     });
 }
        public async Task ReportsProgressThroughToken()
        {
            var progressToken = new ProgressReportToken();

            await AsyncFor.For(1, 10, i =>
            {
                Assert.False(progressToken.IsCompleted);
                Assert.False(progressToken.IsIndeterminate);
                Assert.True(progressToken.Progress < 1);
            }, progressReportToken : progressToken, batchSize : 2);

            Assert.True(progressToken.IsCompleted);
            Assert.False(progressToken.IsIndeterminate);
            Assert.Equal(1, progressToken.Progress, precision: 1);
        }
        public async Task UnwrapsSingleExceptionAggregateExceptions()
        {
            var ex = await Assert.ThrowsAsync <AggregateException>(async() =>
            {
                await AsyncFor.For(0, 6, i =>
                {
                    if (i % 2 == 1)
                    {
                        throw new TestException();
                    }
                });
            });

            Assert.Equal(3, ex.InnerExceptions.Count);
            Assert.All(ex.InnerExceptions, e => Assert.IsType <TestException>(e));
        }
        public async Task NoConcurrencyWithSynchronousOption()
        {
            var taskAlreadyRunning = false;
            await AsyncFor.For(0, 6, async i =>
            {
                if (taskAlreadyRunning)
                {
                    throw new Exception("Task is already running, so the 'RunSynchronously' option was ignored.");
                }

                taskAlreadyRunning = true;
                // Delay to increase chances of concurrency
                await Task.Delay(100);
                taskAlreadyRunning = false;
            }, runSynchronously : true);
        }
        public async Task RunsForEveryNumberWithAsynchronousDelegate_InstanceMethod()
        {
            var sum        = 0;
            var lockObject = new object();
            var f          = new AsyncFor();
            await f.RunFor(0, 10, async i =>
            {
                lock (lockObject)
                {
                    sum += i;
                }
                await Task.CompletedTask;
            });

            Assert.Equal(55, sum);
        }
Ejemplo n.º 19
0
        private async Task RampUpAsync(TestRunInfo testRunInfo)
        {
            // Handle ramp up if defined
            if (testRunInfo.RampUpTimeSeconds > 4 && !_cancellationTokenSource.IsCancellationRequested)
            {
                Tracer.TraceInfo($"Ramping up starts.");

                DateTime startTime        = DateTime.Now;
                DateTime endTime          = startTime + TimeSpan.FromSeconds(testRunInfo.RampUpTimeSeconds);
                int      numberIntervals  = Math.Min(testRunInfo.RampUpTimeSeconds / 5, 6);
                TimeSpan intervalLength   = (endTime - startTime) / numberIntervals;
                double   intervalRpsDelta = ((double)testRunInfo.TargetRPS) / ((double)numberIntervals);
                for (int i = 0; i < numberIntervals && !_cancellationTokenSource.IsCancellationRequested; i++)
                {
                    var apiInfo = _mixInfo.ApiMix[i % _mixInfo.ApiMix.Count];

                    long intervalRps = (long)Math.Round((i + 1) * intervalRpsDelta);
                    Tracer.TraceInfo($"Ramping up. RPS = {intervalRps}");

                    AsyncFor myRampUpFor = new AsyncFor(intervalRps, GetResourceDescription(apiInfo, _mixInfo), GetTestDescription(apiInfo), testRunInfo.MeasureServerSideTime);
                    myRampUpFor.PerSecondMetricsAvailable += new ConsoleMetricsHandler().MetricsAvailableHandler;
                    _asyncForInstances.Add(myRampUpFor);

                    try
                    {
                        await myRampUpFor.ForAsync(
                            intervalLength,
                            testRunInfo.SimultaneousConnections,
                            new MaaServiceApiCaller(apiInfo, _mixInfo.ProviderMix, testRunInfo.EnclaveInfoFile, testRunInfo.ForceReconnects).CallApi,
                            _cancellationTokenSource.Token);
                    }
                    catch (TaskCanceledException)
                    {
                        // Ignore task cancelled if we requested cancellation via ctrl-c
                        if (_cancellationTokenSource.IsCancellationRequested)
                        {
                            Tracer.TraceInfo(($"Organized shutdown in progress.  All asyncfor instances have gracefully shutdown."));
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                Tracer.TraceInfo($"Ramping up complete.");
            }
        }
        public async Task ReportsProgressThroughInstanceEvent()
        {
            var myCustomMessage   = $"Loading... ({Guid.NewGuid()})";
            var progressEventArgs = new ConcurrentBag <ProgressReportedEventArgs>();
            var completedCount    = 0;

            void OnProgressChanged(object sender, ProgressReportedEventArgs e)
            {
                progressEventArgs.Add(e);
                Assert.Equal(myCustomMessage, e.Message);
                Assert.False(e.IsIndeterminate);
                Assert.True(e.Progress <= 1);
            }

            void OnCompleted(object sender, EventArgs e)
            {
                Interlocked.Increment(ref completedCount);
            }

            var sampleData = Enumerable.Repeat(new TestClass(), 5).ToList();

            var f = new AsyncFor();

            f.Message          = myCustomMessage;
            f.ProgressChanged += OnProgressChanged;
            f.Completed       += OnCompleted;
            f.BatchSize        = 2;
            await f.RunForEach(sampleData, data =>
            {
                data.Success = true;
            });

            f.ProgressChanged -= OnProgressChanged;
            f.Completed       -= OnCompleted;


            Assert.Equal(sampleData.Count, progressEventArgs.Count);
            Assert.Equal(1, completedCount);

            var distinctProgressPercentages = progressEventArgs.Select(e => e.Progress).Distinct().ToList();

            Assert.InRange(distinctProgressPercentages.Count, 2, 5);
            Assert.All(distinctProgressPercentages, p => Assert.InRange(p, 0, 1));
            Assert.Contains(distinctProgressPercentages, p => p != 0 && p != 1);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Extracts all files to the given directory
        /// </summary>
        /// <param name="outputDirectory">Directory to which the files should be saved. This will be created if it does not exist.</param>
        /// <param name="fileSystem">File system to which to save the files</param>
        public async Task Extract(string outputDirectory, IFileSystem fileSystem, ProgressReportToken?progressReportToken = null)
        {
            if (!fileSystem.DirectoryExists(outputDirectory))
            {
                fileSystem.CreateDirectory(outputDirectory);
            }

            var filenames = GetFilenames();
            await AsyncFor.ForEach(filenames, filename =>
            {
                var fileData = GetFile(filename);
                if (fileData == null)
                {
                    return;
                }
                fileSystem.WriteAllBytes(Path.Combine(outputDirectory, filename), fileData);
            }, progressReportToken : progressReportToken).ConfigureAwait(false);
        }
Ejemplo n.º 22
0
        public async Task <IEnumerable <FileTypeDetectionResult> > DetectFileType(GenericFile file, PluginManager manager)
        {
            ConcurrentQueue <FileTypeDetectionResult> matches = new ConcurrentQueue <FileTypeDetectionResult>();
            AsyncFor f = new AsyncFor();

            f.RunSynchronously = !file.IsThreadSafe;
            await f.RunForEach(manager.GetRegisteredObjects <IDetectableFileType>(), async (x) =>
            {
                if (await x.IsOfType(file))
                {
                    matches.Enqueue(new FileTypeDetectionResult {
                        FileType = x.GetType().GetTypeInfo(), MatchChance = 0.5f
                    });
                }
            });

            return(matches);
        }
        private async Task temp()
        {
            var progressToken = new ProgressReportToken();

            progressToken.ProgressChanged += (object sender, ProgressReportedEventArgs e) =>
            {
                Console.WriteLine($"Progress: {e.Progress * 100} %");
            };
            progressToken.Completed += (object sender, EventArgs e) =>
            {
                Console.WriteLine("Completed!");
            };

            await AsyncFor.For(0, 10, i =>
            {
                Console.WriteLine(i);
            }, progressReportToken : progressToken);
        }
        public async Task RunsConcurrently()
        {
            var taskAlreadyRunning = false;
            var anyConcurrency     = false;
            await AsyncFor.For(0, 6, async i =>
            {
                if (taskAlreadyRunning)
                {
                    anyConcurrency = true;
                }

                taskAlreadyRunning = true;
                // Delay to increase chances of concurrency
                await Task.Delay(100);
                taskAlreadyRunning = false;
            }, runSynchronously : false);

            Assert.True(anyConcurrency);
        }
        public async Task CapturesAllExceptions()
        {
            var ex = await Assert.ThrowsAsync <AggregateException>(async() =>
            {
                await AsyncFor.For(0, 6, i =>
                {
                    if (i % 2 == 1)
                    {
                        throw new AggregateException(new TestException(), new TestException());
                    }
                });
            });

            Assert.Equal(3, ex.InnerExceptions.Count);
            Assert.All(ex.InnerExceptions, e =>
            {
                Assert.IsType <AggregateException>(e);
                Assert.All((e as AggregateException).InnerExceptions, inner => Assert.IsType <TestException>(inner));
            });
        }
        public async Task ThrowsOnSimultaneousInstanceUsage()
        {
            var sampleData = Enumerable.Repeat(new TestClass(), 5);

            var f         = new AsyncFor();
            var firstTask = f.RunForEach(sampleData, async data =>
            {
                data.Success = true;
                await Task.Delay(100);
            });

            await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await f.RunForEach(sampleData, async data =>
                {
                    data.Success = true;
                    await Task.Delay(100);
                });
            });
        }
        public async Task ReportsProgressThroughInstanceProperties()
        {
            var sampleData = Enumerable.Repeat(new TestClass(), 5);

            var f = new AsyncFor();
            await f.RunForEach(sampleData, data =>
            {
                Assert.False(f.IsCompleted);
                Assert.False(f.IsIndeterminate);
                Assert.True(f.Progress < 1);

                data.Success = true;
            });

            Assert.True(f.IsCompleted);
            Assert.False(f.IsIndeterminate);
            Assert.Equal(1, f.Progress, precision: 1);

            Assert.All(sampleData, data => Assert.True(data.Success));
        }
        public async Task BatchSizeLimitsConcurrency()
        {
            var runningTasks = 0;
            var batchSize    = 5;

            await AsyncFor.For(0, 20, async i =>
            {
                if (runningTasks > batchSize)
                {
                    throw new Exception("Maximum task count exceeded.");
                }

                Interlocked.Increment(ref runningTasks);

                // Delay to increase chances of concurrency problems
                await Task.Delay(100);

                Interlocked.Decrement(ref runningTasks);
            }, batchSize : batchSize);
        }
Ejemplo n.º 29
0
        public static async Task <NcchExtendedHeader> Load(IReadOnlyBinaryDataAccessor data)
        {
            var header = new NcchExtendedHeader
            {
                ApplicationTitle    = await data.ReadStringAsync(0, 8, Encoding.ASCII),
                Reserved1           = await data.ReadArrayAsync(8, 5),
                Flag                = await data.ReadByteAsync(0xD),
                RemasterVersion     = await data.ReadInt16Async(0xE),
                TextCodeSetInfo     = await CodeSetInfo.Load(data.Slice(0x10, 0xC)),
                StackSize           = await data.ReadInt32Async(0x1C),
                ReadOnlyCodeSetInfo = await CodeSetInfo.Load(data.Slice(0x20, 0xC)),
                Reserved2           = await data.ReadInt32Async(0x2C),
                DataCodeSetInfo     = await CodeSetInfo.Load(data.Slice(0x30, 0xC)),
                BssSize             = await data.ReadInt32Async(0x3C)
            };

            var moduleIds = new long[48];
            await AsyncFor.For(0, 48 - 1, async i =>
            {
                moduleIds[i] = await data.ReadInt64Async(0x40 + i * 8);
            });

            header.DependencyModuleIds = moduleIds;

            header.SystemInformation = await SystemInfo.Load(data.Slice(0x1C0, 0x40));

            header.LocalSystemCapabilities = await Arm11LocalSystemCapabilities.Load(data.Slice(0x200, 0x170));

            header.KernelCapabilities = await Arm11KernelCapabilities.Load(data.Slice(0x370, 0x80));

            header.AccessControl = await Arm9AccessControl.Load(data.Slice(0x3F0, 0x10));

            header.AccessDescSignature = await data.ReadArrayAsync(0x400, 0x100);

            header.NcchHdrPublicKey = await data.ReadArrayAsync(0x500, 0x100);

            header.Aci = await data.ReadArrayAsync(0x600, 0x200);

            return(header);
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Asynchronously copies a directory
        /// </summary>
        /// <param name="sourceDirectory">The directory to copy</param>
        /// <param name="destinationDirectory">The new destination for the source directory</param>
        public static async Task CopyDirectory(string sourceDirectory, string destinationDirectory, IFileSystem provider)
        {
            // Get the files/directories to copy
            var files = provider.GetFiles(sourceDirectory, "*", false);

            //Create all required directories
            foreach (var item in files)
            {
                var dest = item.Replace(sourceDirectory, destinationDirectory);
                if (!provider.DirectoryExists(Path.GetDirectoryName(dest)))
                {
                    provider.CreateDirectory(Path.GetDirectoryName(dest));
                }
            }

            AsyncFor f = new AsyncFor();

            f.RunSynchronously = false;
            await f.RunForEach(files, path =>
            {
                string dest = path.Replace(sourceDirectory, destinationDirectory);
                provider.CopyFile(path, dest);
            }).ConfigureAwait(false);
        }