Example #1
0
        protected void ParseAndVerifyProject(
            string projFileName,
            IProjectStaticPredictor predictor,
            IReadOnlyCollection <BuildInput> expectedInputs,
            IReadOnlyCollection <BuildOutputDirectory> expectedOutputs)
        {
            var projectCollection = new ProjectCollection();
            var project           = new Project(
                Path.Combine(TestsDirectoryPath, projFileName),  // TestsData files are marked to CopyToOutput and are available next to the executing assembly
                new Dictionary <string, string>
            {
                { "Platform", "amd64" },
                { "Configuration", "debug" },
            },
                null,
                projectCollection);
            ProjectInstance projectInstance = project.CreateProjectInstance(ProjectInstanceSettings.ImmutableWithFastItemLookup);

            bool success = predictor.TryPredictInputsAndOutputs(
                project,
                projectInstance,
                TestsDirectoryPath,
                out StaticPredictions predictions);


            IReadOnlyCollection <BuildInput> absolutePathInputs = expectedInputs.Select(i =>
                                                                                        new BuildInput(Path.Combine(project.DirectoryPath, i.Path), i.IsDirectory))
                                                                  .ToList();

            Assert.True(success, "Prediction returned false (no predictions)");
            predictions.AssertPredictions(absolutePathInputs, expectedOutputs);
        }
Example #2
0
        public void DistinctInputsAndOutputsAreAggregated()
        {
            var predictors = new IProjectStaticPredictor[]
            {
                new MockPredictor(new StaticPredictions(
                                      new[] { new BuildInput(@"foo\bar1", false) },
                                      new[] { new BuildOutputDirectory(@"blah\boo1") })),
                new MockPredictor2(new StaticPredictions(
                                       new[] { new BuildInput(@"foo\bar2", false) },
                                       new[] { new BuildOutputDirectory(@"blah\boo2") })),
            };

            var executor = new ProjectStaticPredictionExecutor(@"c:\repo", predictors);

            var project = TestHelpers.CreateProjectFromRootElement(ProjectRootElement.Create());

            StaticPredictions predictions = executor.PredictInputsAndOutputs(project);

            BuildInput[] expectedInputs =
            {
                new BuildInput(@"foo\bar1", false, "MockPredictor"),
                new BuildInput(@"foo\bar2", false, "MockPredictor2"),
            };

            BuildOutputDirectory[] expectedBuildOutputDirectories =
            {
                new BuildOutputDirectory(@"blah\boo1", "MockPredictor"),
                new BuildOutputDirectory(@"blah\boo2", "MockPredictor2"),
            };

            predictions.AssertPredictions(expectedInputs, expectedBuildOutputDirectories);
        }
Example #3
0
        public void PredictorSparsenessStressTest()
        {
            int[] numPredictorCases     = { 40 }; // Set to 1000 or more for reduced average noise when using tickResults for measurements.
            int[] sparsenessPercentages = { 0, 25, 50, 75, 100 };
            var   tickResults           = new long[numPredictorCases.Length][];

            var proj = TestHelpers.CreateProjectFromRootElement(ProjectRootElement.Create());

            // Run through twice and keep the second round only - first round affected by JIT overhead.
            for (int iter = 0; iter < 2; iter++)
            {
                for (int p = 0; p < numPredictorCases.Length; p++)
                {
                    int numPredictors = numPredictorCases[p];
                    tickResults[p] = new long[sparsenessPercentages.Length];
                    var predictors  = new IProjectStaticPredictor[numPredictors];
                    int sparseIndex = 0;

                    for (int s = 0; s < sparsenessPercentages.Length; s++)
                    {
                        int sparsenessPercentage = sparsenessPercentages[s];
                        for (int i = 0; i < numPredictors; i++)
                        {
                            if (sparseIndex < sparsenessPercentage)
                            {
                                predictors[i] = new MockPredictor(null);
                            }
                            else
                            {
                                predictors[i] = new MockPredictor(new StaticPredictions(null, null));
                            }

                            sparseIndex++;
                            if (sparseIndex > sparsenessPercentage)
                            {
                                sparseIndex = 0;
                            }
                        }

                        var       executor = new ProjectStaticPredictionExecutor(@"c:\repo", predictors);
                        Stopwatch sw       = Stopwatch.StartNew();
                        executor.PredictInputsAndOutputs(proj);
                        sw.Stop();
                        tickResults[p][s] = sw.ElapsedTicks;
                        Console.WriteLine($"{numPredictors} @ {sparsenessPercentage}%: {sw.ElapsedTicks} ticks");
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Creates a collection of all instances of <see cref="IProjectStaticPredictor"/> contained in the Microsoft.Build.Prediction assembly.
        /// </summary>
        /// <param name="msBuildHintPath">
        /// When not null, this path is used to load MSBuild Microsoft.Build.* assemblies if
        /// they have not already been loaded into the appdomain. When null, the fallback
        /// is to look for an installed copy of Visual Studio on Windows.
        /// </param>
        /// <returns>A collection of <see cref="IProjectStaticPredictor"/>.</returns>
        public static IReadOnlyCollection <IProjectStaticPredictor> CreateStandardPredictors(string msBuildHintPath)
        {
            // Ensure the user has MSBuild loaded into the appdomain.
            MsBuildEnvironment.Setup(msBuildHintPath);

            Type[] types = Assembly.GetExecutingAssembly().GetTypes()
                           .Where(t => !t.IsAbstract && !t.IsInterface && t.GetInterfaces().Contains(typeof(IProjectStaticPredictor)))
                           .ToArray();
            var predictors = new IProjectStaticPredictor[types.Length];

            // Use default constructor.
            Parallel.For(0, types.Length, i =>
                         predictors[i] = (IProjectStaticPredictor)Activator.CreateInstance(types[i]));

            return(predictors);
        }
Example #5
0
        public void EmptyPredictionsResultInEmptyAggregateResult()
        {
            var predictors = new IProjectStaticPredictor[]
            {
                new MockPredictor(new StaticPredictions(null, null)),
                new MockPredictor(new StaticPredictions(null, null)),
            };

            var executor = new ProjectStaticPredictionExecutor(@"c:\repo", predictors);

            var project = TestHelpers.CreateProjectFromRootElement(ProjectRootElement.Create());
            StaticPredictions predictions = executor.PredictInputsAndOutputs(project);

            Assert.NotNull(predictions);
            Assert.Equal(0, predictions.BuildInputs.Count);
            Assert.Equal(0, predictions.BuildOutputDirectories.Count);
        }
Example #6
0
        public void DuplicateInputsAndOutputsMergePredictedBys()
        {
            var predictors = new IProjectStaticPredictor[]
            {
                new MockPredictor(new StaticPredictions(
                                      new[] { new BuildInput(@"foo\bar", false) },
                                      new[] { new BuildOutputDirectory(@"blah\boo") })),
                new MockPredictor2(new StaticPredictions(
                                       new[] { new BuildInput(@"foo\bar", false) },
                                       new[] { new BuildOutputDirectory(@"blah\boo") })),
            };

            var executor = new ProjectStaticPredictionExecutor(@"c:\repo", predictors);

            var project = TestHelpers.CreateProjectFromRootElement(ProjectRootElement.Create());

            StaticPredictions predictions = executor.PredictInputsAndOutputs(project);

            predictions.AssertPredictions(
                new[] { new BuildInput(@"foo\bar", false, "MockPredictor", "MockPredictor2") },
                new[] { new BuildOutputDirectory(@"blah\boo", "MockPredictor", "MockPredictor2") });
        }
 public PredictorAndName(IProjectStaticPredictor predictor)
 {
     Predictor = predictor;
     TypeName  = predictor.GetType().Name;
 }