2016-04-20 17:53:38 -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 Microsoft.DotNet.Cli.Utils ;
2016-04-22 12:23:26 -07:00
using Microsoft.DotNet.TestFramework ;
2016-04-20 17:53:38 -07:00
using Microsoft.DotNet.Tools.Test.Utilities ;
using Microsoft.Extensions.PlatformAbstractions ;
using System ;
using System.IO ;
using System.Runtime.InteropServices ;
using Xunit ;
using Xunit.Abstractions ;
using FluentAssertions ;
namespace Microsoft.DotNet.Tests
{
public class GivenThatIWantToManageMulticoreJIT : TestBase
{
ITestOutputHelper _output ;
2016-04-22 12:23:26 -07:00
private const string OptimizationProfileFileName = "dotnet" ;
2016-04-20 17:53:38 -07:00
public GivenThatIWantToManageMulticoreJIT ( ITestOutputHelper output )
{
_output = output ;
}
[Fact]
2016-04-22 12:23:26 -07:00
public void When_invoked_then_dotnet_writes_optimization_data_to_the_profile_root ( )
2016-04-20 17:53:38 -07:00
{
2016-04-22 12:23:26 -07:00
var testDirectory = TestAssetsManager . CreateTestDirectory ( ) ;
var testStartTime = GetTruncatedDateTime ( ) ;
2016-04-20 17:53:38 -07:00
new TestCommand ( "dotnet" )
2016-04-22 12:23:26 -07:00
. WithUserProfileRoot ( testDirectory . Path )
. ExecuteWithCapturedOutput ( "--help" ) ;
var optimizationProfileFilePath = GetOptimizationProfileFilePath ( testDirectory . Path ) ;
2016-04-20 17:53:38 -07:00
2016-04-22 12:23:26 -07:00
File . Exists ( optimizationProfileFilePath )
2016-04-20 17:53:38 -07:00
. Should ( ) . BeTrue ( "Because dotnet CLI creates it after each run" ) ;
2016-04-22 12:23:26 -07:00
File . GetLastWriteTimeUtc ( optimizationProfileFilePath )
. Should ( ) . BeOnOrAfter ( testStartTime , "Because dotnet CLI was executed after that time" ) ;
2016-04-20 17:53:38 -07:00
}
2016-04-22 12:23:26 -07:00
[Fact]
public void When_invoked_with_MulticoreJit_disabled_then_dotnet_does_not_writes_optimization_data_to_the_profile_root ( )
2016-04-20 17:53:38 -07:00
{
2016-04-22 12:23:26 -07:00
var testDirectory = TestAssetsManager . CreateTestDirectory ( ) ;
var testStartTime = GetTruncatedDateTime ( ) ;
new TestCommand ( "dotnet" )
. WithUserProfileRoot ( testDirectory . Path )
. WithEnvironmentVariable ( "DOTNET_DISABLE_MULTICOREJIT" , "1" )
. ExecuteWithCapturedOutput ( "--help" ) ;
var optimizationProfileFilePath = GetOptimizationProfileFilePath ( testDirectory . Path ) ;
File . Exists ( optimizationProfileFilePath )
. Should ( ) . BeFalse ( "Because multicore JIT is disabled" ) ;
2016-04-20 17:53:38 -07:00
}
2016-04-22 12:23:26 -07:00
[Fact]
public void When_the_profile_root_is_undefined_then_dotnet_does_not_crash ( )
2016-04-20 17:53:38 -07:00
{
2016-04-22 12:23:26 -07:00
var testDirectory = TestAssetsManager . CreateTestDirectory ( ) ;
var testStartTime = GetTruncatedDateTime ( ) ;
var optimizationProfileFilePath = GetOptimizationProfileFilePath ( testDirectory . Path ) ;
new TestCommand ( "dotnet" )
. WithUserProfileRoot ( "" )
. ExecuteWithCapturedOutput ( "--help" )
. Should ( ) . Pass ( ) ;
2016-04-20 17:53:38 -07:00
}
2016-04-22 12:23:26 -07:00
[Fact]
public void When_cli_repo_builds_then_dotnet_writes_optimization_data_to_the_default_profile_root ( )
2016-04-20 17:53:38 -07:00
{
2016-04-22 12:23:26 -07:00
var optimizationProfileFilePath = GetOptimizationProfileFilePath ( ) ;
File . Exists ( optimizationProfileFilePath )
. Should ( ) . BeTrue ( "Because the dotnet building dotnet writes to the default root" ) ;
2016-04-20 17:53:38 -07:00
}
2016-04-22 12:23:26 -07:00
private static string GetOptimizationProfileFilePath ( string userHomePath = null )
2016-04-20 17:53:38 -07:00
{
2016-04-22 12:23:26 -07:00
return Path . Combine (
GetUserProfileRoot ( userHomePath ) ,
GetOptimizationRootPath ( GetDotnetVersion ( ) ) ,
OptimizationProfileFileName ) ;
}
private static string GetUserProfileRoot ( string overrideUserProfileRoot = null )
{
if ( overrideUserProfileRoot ! = null )
{
return overrideUserProfileRoot ;
}
return RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )
? Environment . GetEnvironmentVariable ( "LocalAppData" )
: Environment . GetEnvironmentVariable ( "HOME" ) ;
}
private static string GetOptimizationRootPath ( string version )
{
var rid = PlatformServices . Default . Runtime . GetRuntimeIdentifier ( ) ;
return RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )
2016-05-25 20:41:10 -07:00
? $@"Microsoft\dotnet\optimizationdata\{version}\{rid}"
: $@".dotnet/optimizationdata/{version}/{rid}" ;
2016-04-20 17:53:38 -07:00
}
private static string GetDotnetVersion ( )
{
return Command . Create ( "dotnet" , new [ ] { "--version" } )
. CaptureStdOut ( )
. Execute ( )
. StdOut
. Trim ( ) ;
}
2016-04-22 12:23:26 -07:00
private static DateTime GetTruncatedDateTime ( )
{
var dt = DateTime . UtcNow ;
return new DateTime ( dt . Year , dt . Month , dt . Day , dt . Hour , dt . Minute , dt . Second , 0 , dt . Kind ) ;
}
2016-04-20 17:53:38 -07:00
}
}