2016-04-26 17:52:34 -07:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System ;
using System.IO ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using Microsoft.Xunit.Performance ;
namespace Microsoft.DotNet.Tests.Performance
{
public class HelloWorld : TestBase
{
private static readonly string s_testdirName = "helloworldtestroot" ;
2016-10-14 00:06:35 -07:00
private static readonly string s_testProject = $"{s_testdirName}.csproj" ;
2016-04-26 17:52:34 -07:00
private static readonly string s_outputdirName = "test space/bin" ;
private static string AssetsRoot { get ; set ; }
private static string RestoredTestProjectDirectory { get ; set ; }
2016-10-14 00:06:35 -07:00
private string ProjectPath { get ; set ; }
2016-04-26 17:52:34 -07:00
private string TestDirectory { get ; set ; }
private string OutputDirectory { get ; set ; }
static HelloWorld ( )
{
HelloWorld . SetupStaticTestProject ( ) ;
}
public HelloWorld ( )
{
}
[Benchmark]
public void MeasureDotNetBuild ( )
{
foreach ( var iter in Benchmark . Iterations )
{
// Setup a new instance of the test project.
TestInstanceSetup ( ) ;
// Setup the build command.
2016-10-27 18:46:43 -07:00
var buildCommand = new BuildCommand ( ) ;
2016-04-26 17:52:34 -07:00
using ( iter . StartMeasurement ( ) )
{
// Execute the build command.
2016-10-14 00:06:35 -07:00
buildCommand . Execute ( $"{ProjectPath} --output \" { OutputDirectory } \ " --framework {DefaultFramework}" ) ;
2016-04-26 17:52:34 -07:00
}
}
}
private void TestInstanceSetup ( )
{
2016-10-14 00:06:35 -07:00
var root = Temp . CreateDirectory ( ) ;
2016-04-26 17:52:34 -07:00
var testInstanceDir = root . CopyDirectory ( RestoredTestProjectDirectory ) ;
TestDirectory = testInstanceDir . Path ;
OutputDirectory = Path . Combine ( TestDirectory , s_outputdirName ) ;
2016-10-14 00:06:35 -07:00
ProjectPath = Path . Combine ( TestDirectory , s_testProject ) ;
2016-04-26 17:52:34 -07:00
}
private static void SetupStaticTestProject ( )
{
AssetsRoot = Path . Combine ( AppContext . BaseDirectory , "bin" ) ;
RestoredTestProjectDirectory = Path . Combine ( AssetsRoot , s_testdirName ) ;
// Ignore Delete Failure
try
{
Directory . Delete ( RestoredTestProjectDirectory , true ) ;
}
catch ( Exception ) { }
Directory . CreateDirectory ( RestoredTestProjectDirectory ) ;
// Todo: this is a hack until corefx is on nuget.org remove this After RC 2 Release
NuGetConfig . Write ( RestoredTestProjectDirectory ) ;
var newCommand = new NewCommand ( ) ;
newCommand . WorkingDirectory = RestoredTestProjectDirectory ;
newCommand . Execute ( ) . Should ( ) . Pass ( ) ;
2016-10-27 18:46:43 -07:00
var restoreCommand = new RestoreCommand ( ) ;
2016-04-26 17:52:34 -07:00
restoreCommand . WorkingDirectory = RestoredTestProjectDirectory ;
2016-10-14 00:06:35 -07:00
restoreCommand . Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
2016-04-26 17:52:34 -07:00
}
}
}