<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- Perf Runner NuGet package paths -->
  <PropertyGroup>
    <XunitPerfRunnerPackageId>Microsoft.DotNet.xunit.performance.runner.Windows</XunitPerfRunnerPackageId>
    <XunitPerfRunnerPackageVersion Condition="'$(XunitPerfRunnerPackageVersion)' == ''">1.0.0-alpha-build0029</XunitPerfRunnerPackageVersion>
    <XunitPerfRunnerPackageDir>$(PackagesDir)$(XunitPerfRunnerPackageId)/$(XunitPerfRunnerPackageVersion)</XunitPerfRunnerPackageDir>
    <XunitPerfRunnerPath>$(TestPath)$(DebugTestFrameworkFolder)/xunit.performance.run.exe</XunitPerfRunnerPath>
  </PropertyGroup>

  <!-- Perf Analysis NuGet package paths -->
  <PropertyGroup>
    <XunitPerfAnalysisPackageId>Microsoft.DotNet.xunit.performance.analysis</XunitPerfAnalysisPackageId>
    <XunitPerfAnalysisPackageVersion Condition="'$(XunitPerfAnalysisPackageVersion)' == ''">1.0.0-alpha-build0029</XunitPerfAnalysisPackageVersion>
    <XunitPerfAnalysisPath>$(PackagesDir)$(XunitPerfAnalysisPackageId)/$(XunitPerfAnalysisPackageVersion)/tools/xunit.performance.analysis.exe</XunitPerfAnalysisPath>
  </PropertyGroup>

  <!-- Copy over the performance runners to the test directory-->
  <Target Name="CopyXunitExecutionDesktop" AfterTargets="CopyTestToTestDirectory" Condition="'$(Performance)' == 'true'">
    <ItemGroup>
      <PerfRunners Include="$(XunitPerfRunnerPackageDir)/tools/xunit.performance.run.exe" />
      <PerfRunners Include="$(XunitPerfRunnerPackageDir)/tools/xunit.performance.metrics.dll" />
      <PerfRunners Include="$(XunitPerfRunnerPackageDir)/tools/xunit.performance.logger.exe" />
      <PerfRunners Include="$(XunitPerfRunnerPackageDir)/tools/xunit.performance.core.dll" />
      <PerfRunners Include="$(XunitPerfRunnerPackageDir)/tools/xunit.runner.utility.desktop.dll" />
      <PerfRunners Include="$(XunitPerfRunnerPackageDir)/tools/ProcDomain.dll" />
      <PerfRunners Include="$(XunitPerfRunnerPackageDir)/tools/Microsoft.Diagnostics.Tracing.TraceEvent.dll" />
      <PerfRunners Include="$(XunitPerfRunnerPackageDir)/tools/*/*.*" />
    </ItemGroup>
    <Copy SourceFiles="@(PerfRunners)"
          DestinationFiles="@(PerfRunners->'$(TestPath)$(DebugTestFrameworkFolder)/%(RecursiveDir)%(Filename)%(Extension)')"
          SkipUnchangedFiles="true" />
  </Target>

  <!-- Executable properties -->
  <PropertyGroup>
    <PerfRunId Condition="'$(PerfRunId)' == ''">$(TargetFileName)-WindowsCore</PerfRunId>
    <AnalysisReportFileName>$(PerfRunId)-analysis.html</AnalysisReportFileName>
    <XunitRunnerArgs>$(TargetFileName) -trait Benchmark=true -runnerhost $(TestHost) -runner $(XunitExecutable) -runid $(PerfRunId) -verbose</XunitRunnerArgs>
    <PerfTestCommandLine>$(XunitPerfRunnerPath) $(XunitRunnerArgs) $(AdditionalPerfTestArgs)</PerfTestCommandLine>

    <PerfRunOutputFile>$(TestPath)$(DebugTestFrameworkFolder)/$(PerfRunId).xml</PerfRunOutputFile>
    <XunitAnalysisArgs>$(PerfRunOutputFile) -html $(AnalysisReportFileName) $(AdditionalXunitAnalysisArgs)</XunitAnalysisArgs>
    <PerfAnalysisCommandLine>$(XunitPerfAnalysisPath) $(XunitAnalysisArgs)</PerfAnalysisCommandLine>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Performance)' == 'true'">
    <AnalyzePerfResults Condition="'$(AnalyzePerfResults)' != 'false'">true</AnalyzePerfResults>
    <TestCommandLine>$(PerfTestCommandLine)</TestCommandLine>
  </PropertyGroup>

    <!-- Optimizations to configure Xunit for performance -->
  <ItemGroup Condition="'$(IncludePerformanceTests)' == 'true'">
    <AssemblyInfoUsings Include="using Microsoft.Xunit.Performance%3B" />
    <AssemblyInfoLines Include="[assembly:OptimizeForBenchmarks]" />
  </ItemGroup>

  <Target Name="AnalyzePerfResults"
          AfterTargets="RunTestsForProject"
          Condition="'$(AnalyzePerfResults)' == 'true' and Exists('$(PerfRunOutputFile)')">
    <Exec Command="$(PerfAnalysisCommandLine)"
          WorkingDirectory="$(TestPath)%(TestTargetFramework.Folder)"
          ContinueOnError="false">
      <Output PropertyName="PerfTestRunExitCode" TaskParameter="ExitCode" />
    </Exec>
    <Message Text="Performance test analysis report is available at $(TestPath)$(DebugTestFrameworkFolder)/$(AnalysisReportFileName)" />
  </Target>

  <Target Name="WarnForDebugPerfConfiguration"
          BeforeTargets="RunTestsForProject"
          Condition="'$(Performance)' == 'true' and !$(ConfigurationGroup.ToLower().Contains('release'))">
    <Warning Text="You are running performance tests in a configuration other than Release. Your results may be unreliable." />
  </Target>
</Project>
