Beispiel #1
0
        public void GenerateSuite(int seed, string suiteName, string outputPath, string[] testPaths, string[] searchPatterns, string[] hintPaths, LoadSuiteConfig config, string cachePath = null, bool legacyProject = false, string globalPackageConfig = null)
        {
            Random rand = new Random(seed);

            int suiteTestCount = 0;

            _unitTestSelector = new UnitTestSelector();

            _unitTestSelector.Initialize(seed, testPaths, searchPatterns, hintPaths, cachePath);

            for (int iConfig = 0; iConfig < config.LoadTestConfigs.Count; iConfig++)
            {
                var loadTestConfig = config.LoadTestConfigs[iConfig];

                for (int i = 0; i < loadTestConfig.TestCount; i++)
                {
                    var loadTestInfo = new LoadTestInfo(globalPackageConfig)
                    {
                        TestName             = suiteName + "_" + suiteTestCount.ToString("X4"),
                        Duration             = loadTestConfig.Duration,
                        LoadPatternType      = typeof(StaticLoadPattern),                     //Type.GetType(loadTestConfig.LoadPattern),
                        TestPatternType      = typeof(RandomTestPattern),                     //Type.GetType(loadTestConfig.TestPattern),
                        WorkerStrategyType   = typeof(DedicatedThreadWorkerStrategy),         //Type.GetType(loadTestConfig.WorkerStrategy),
                        WorkerCount          = loadTestConfig.NumWorkers,
                        SelfDestruct         = loadTestConfig.SelfDestruct,
                        EnvironmentVariables = loadTestConfig.EnvironmentVariables,
                        SuiteConfig          = config,
                        Seed = rand.Next(),
                    };

                    loadTestInfo.SourceDirectory = Path.Combine(outputPath, iConfig.ToString("00") + "_" + loadTestInfo.Duration.TotalHours.ToString("00.##") + "hr", loadTestInfo.TestName);
                    loadTestInfo.UnitTests       = _unitTestSelector.NextUnitTests(loadTestConfig.NumTests).ToArray();

                    //build a list of all the sources files to generate for the load test
                    var generators = new List <ISourceFileGenerator>()
                    {
                        new LoadTestSourceFileGenerator(),
                        new ProgramSourceFileGenerator(),
                        new ExecutionFileGeneratorWindows(),
                        new ExecutionFileGeneratorLinux(),
                    };

                    //if we want to generate a legacy project file (i.e. ToF project file) use HelixToFProjectFileGenerator otherwise use LoadTestProjectFileGenerator
                    var projectFileGenerator = legacyProject ? (ISourceFileGenerator) new HelixToFProjectFileGenerator() : (ISourceFileGenerator) new LoadTestProjectFileGenerator();

                    if (!legacyProject)
                    {
                        generators.Add(new LoadTestProjectJsonFileGenerator());
                    }

                    //I believe the project file generator must be last, becuase it depends on discovering all the other source files
                    //however the ordering beyond that should not matter
                    generators.Add(projectFileGenerator);

                    this.GenerateTestSources(loadTestInfo, generators);
                    CodeGenOutput.Info($"Generated Load Test: {loadTestInfo.TestName}");
                    suiteTestCount++;
                }
            }
        }
Beispiel #2
0
        public void DuplicateFileName()
        {
            var    files  = new[] { new CodeGenFile("a.txt", ""), new CodeGenFile("A.txt", "") };
            Action action = () => _ = new CodeGenOutput(files, null);

            action.Should().Throw <ArgumentException>();
        }
        public override bool Execute()
        {
            if (DebugWaitForInput)
            {
                this.Log.LogMessageFromText($"PID:{Process.GetCurrentProcess().Id} Attach debugger now.", MessageImportance.High);

                while (DebugWaitForInput)
                {
                    ;
                }
            }

            try
            {
                CodeGenOutput.Redirect(new TaskLogOutputWriter(this.Log));

                LoadSuiteGenerator suiteGen = new LoadSuiteGenerator();

                suiteGen.GenerateSuite(this.ParseSeed(), this.SuiteName, this.SuitePath, this.ParseTestPaths(), this.ParseSearchStrings(), this.ParseFrameworkPaths(), this.GetSuiteConfig(), this.DiscoveryCachePath);

                return(true);
                //return this.Log.HasLoggedErrors;
            }
            catch (Exception e)
            {
                this.Log.LogErrorFromException(e);

                return(false);
            }
        }
        public void GenerateSuite(int seed, string suiteName, string outputPath, string[] testPaths, string[] searchPatterns, string[] hintPaths, LoadSuiteConfig config, string cachePath = null)
        {
            int suiteTestCount = 0;

            _unitTestSelector = new UnitTestSelector();

            _unitTestSelector.Initialize(seed, testPaths, searchPatterns, hintPaths, cachePath);

            for (int iConfig = 0; iConfig < config.LoadTestConfigs.Count; iConfig++)
            {
                var loadTestConfig = config.LoadTestConfigs[iConfig];

                for (int i = 0; i < loadTestConfig.TestCount; i++)
                {
                    var loadTestInfo = new LoadTestInfo()
                    {
                        TestName             = suiteName + "_" + suiteTestCount.ToString("X4"),
                        Duration             = loadTestConfig.Duration,
                        LoadPatternType      = typeof(StaticLoadPattern),                     //Type.GetType(loadTestConfig.LoadPattern),
                        TestPatternType      = typeof(RandomTestPattern),                     //Type.GetType(loadTestConfig.TestPattern),
                        WorkerStrategyType   = typeof(DedicatedThreadWorkerStrategy),         //Type.GetType(loadTestConfig.WorkerStrategy),
                        WorkerCount          = loadTestConfig.NumWorkers,
                        EnvironmentVariables = loadTestConfig.EnvironmentVariables,
                        SuiteConfig          = config,
                    };

                    loadTestInfo.SourceDirectory = Path.Combine(outputPath, iConfig.ToString("00") + "_" + loadTestInfo.Duration.TotalHours.ToString("00.##") + "hr", loadTestInfo.TestName);
                    loadTestInfo.UnitTests       = _unitTestSelector.NextUnitTests(loadTestConfig.NumTests).ToArray();

                    this.GenerateTestSources(loadTestInfo);
                    CodeGenOutput.Info($"Generated Load Test: {loadTestInfo.TestName}");
                    suiteTestCount++;
                }
            }
        }
Beispiel #5
0
 public void WriteCacheToFile(string path)
 {
     try
     {
         File.WriteAllText(path, JsonConvert.SerializeObject(_candidates));
     }
     catch (Exception e)
     {
         CodeGenOutput.Warning($"Unable to write test discovery cache file: {path}.\n{e.ToString()}");
     }
 }
Beispiel #6
0
        private UnitTestInfo[] GetTests(string path, string[] hintPaths)
        {
            List <UnitTestInfo> cachedTests;

            if (_candidateCache != null && _candidateCache.TryGetValue(path, out cachedTests))
            {
                CodeGenOutput.Info($"{path}: {cachedTests.Count} tests discovered from cache");
                return(cachedTests.ToArray());
            }
            else
            {
                return(JsonConvert.DeserializeObject <UnitTestInfo[]>(FindTests(path, hintPaths)));
            }
        }
Beispiel #7
0
        private Dictionary <string, List <UnitTestInfo> > LoadCacheFromFile(string path)
        {
            Dictionary <string, List <UnitTestInfo> > cache = null;

            if (File.Exists(path))
            {
                UnitTestInfo[] cachedTests = null;
                try
                {
                    cachedTests = ReadCacheFromFile(path);

                    Dictionary <string, DateTime> assmTimestamp = new Dictionary <string, DateTime>();

                    cache = new Dictionary <string, List <UnitTestInfo> >();

                    foreach (var test in cachedTests)
                    {
                        DateTime currentAssmTimestamp;

                        if (!assmTimestamp.TryGetValue(test.AssemblyPath, out currentAssmTimestamp))
                        {
                            currentAssmTimestamp = File.Exists(test.AssemblyPath) ? File.GetLastWriteTime(test.AssemblyPath) : DateTime.MaxValue;

                            assmTimestamp[test.AssemblyPath] = currentAssmTimestamp;
                        }

                        if (currentAssmTimestamp == test.AssemblyLastModified)
                        {
                            List <UnitTestInfo> assmTests;
                            if (!cache.TryGetValue(test.AssemblyPath, out assmTests))
                            {
                                assmTests = new List <UnitTestInfo>();

                                cache[test.AssemblyPath] = assmTests;
                            }

                            assmTests.Add(test);
                        }
                    }
                }
                catch (Exception e)
                {
                    CodeGenOutput.Warning($"Unable to read test discovery cache file: {path}.\n{e.ToString()}");
                }
            }

            return(cache);
        }
Beispiel #8
0
        public void Initialize(int seed, string[] paths, string[] patterns, string[] hintPaths, string cachePath = null)
        {
            _rand = new Random(seed);

            if (cachePath != null)
            {
                _candidateCache = LoadCacheFromFile(cachePath);
            }

            _candidates = this.FindAllTests(paths, patterns, hintPaths).ToArray();

            if (cachePath != null)
            {
                WriteCacheToFile(cachePath);
            }

            CodeGenOutput.Info($"Discovered {_candidates.Length} unit tests, across {_candidates.Select(t => t.AssemblyPath).Distinct().Count()} assemblies.");
        }
        public void WriteCacheToFile(string path)
        {
            try
            {
                // Serialize the RunConfiguration
                JsonSerializer serializer = JsonSerializer.CreateDefault();

                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    using (StreamWriter writer = new StreamWriter(fs))
                    {
                        serializer.Serialize(writer, _candidates);
                    }
                }
            }
            catch (Exception e)
            {
                CodeGenOutput.Warning($"Unable to write test discovery cache file: {path}.\n{e.ToString()}");
            }
        }
Beispiel #10
0
        private UnitTestInfo[] FindTests(string path, string[] hintPaths)
        {
            var codeGenDllPath = Assembly.GetExecutingAssembly().Location;

            var codegenDir = Path.GetDirectoryName(codeGenDllPath);

            AppDomain loaderDomain = AppDomain.CreateDomain(path, AppDomain.CurrentDomain.Evidence, new AppDomainSetup()
            {
                ApplicationBase = codegenDir
            });

            var loader = (TestAssemblyLoader)loaderDomain.CreateInstanceFromAndUnwrap(codeGenDllPath, typeof(TestAssemblyLoader).FullName);

            loader.InitializeLifetimeService();

            HashSet <string> hints = new HashSet <string>(hintPaths)
            {
                Path.GetDirectoryName(path)
            };

            loader.Load(path, hints.ToArray());

            UnitTestInfo[] tests = loader.GetTests <XUnitTestDiscoverer>();

            //if no xunit tests were discovered and the assembly is an exe treat as a standalone exe test
            if ((tests == null || tests.Length == 0) && Path.GetExtension(path).ToLowerInvariant() == ".exe")
            {
                tests = loader.GetTests <StandAloneTestDiscoverer>();
            }

            AppDomain.Unload(loaderDomain);

            if (tests.Length > 0)
            {
                CodeGenOutput.Info($"{path}: {tests.Length} tests discovered from assembly");
            }

            return(tests);
        }
Beispiel #11
0
        public string GetTests <TDiscoverer>()
            where TDiscoverer : ITestDiscoverer, new()
        {
            try
            {
                var discoverer = new TDiscoverer();

                var tests = discoverer.GetTests(_assembly);

                if (tests.Length > 0)
                {
                    CodeGenOutput.Info($"{_assembly.Assembly.FullName}: {tests.Length} tests discovered from assembly");
                }

                return(JsonConvert.SerializeObject(tests));
            }
            catch (Exception e)
            {
                this.LoadError = (this.LoadError ?? string.Empty) + e.ToString();
            }

            return(JsonConvert.SerializeObject(new UnitTestInfo[] { }));
        }
Beispiel #12
0
        public override bool Execute()
        {
            if (DebugWaitForInput)
            {
                MessageBox.Show($"PID:{Process.GetCurrentProcess().Id} Attach debugger now.", "Debug GenerateStressSuiteTask", MessageBoxButton.OK);
            }

            try
            {
                CodeGenOutput.Redirect(new TaskLogOutputWriter(this.Log));

                LoadSuiteGenerator suiteGen = new LoadSuiteGenerator();

                suiteGen.GenerateSuite(this.ParseSeed(), this.SuiteName, this.SuitePath, this.ParseTestPaths(), this.ParseSearchStrings(), this.ParseFrameworkPaths(), this.GetSuiteConfig(), this.DiscoveryCachePath, this.UseLegacyProject, this.PackageConfigPath);

                return(true);
            }
            catch (Exception e)
            {
                this.Log.LogErrorFromException(e);

                return(false);
            }
        }