MeasureTrace.Database.tests.ps1

<#
  Written and shared by Microsoft employee Matthew Reynolds in the spirit of "Small OSS libraries, tool, and sample code" OSS policy
  MIT license https://github.com/MatthewMWR/MeasureTrace/blob/master/LICENSE
#>

Describe "MeasureTrace.Database core API" {
    $sqlServerConnectionString = "server=(localdb)\MsSqlLocalDb;database=foo;MultipleActiveResultSets=True"
    It "LoadsDll" {
        "foo"
        $dll1 = Get-ChildItem -Path $PSScriptRoot -Filter MeasureTrace.Database.dll -Recurse | Select-Object -First 1
        $dll2 = Get-ChildItem -Path $PSScriptRoot -Filter MeasureTrace.dll -Recurse | Select-Object -First 1
        $dll2,$dll1 | %{ [Reflection.Assembly]::LoadFrom($_.FullName) }
        New-Object -TypeName MeasureTrace.TraceModel.Trace | Should Not Be Null
    }
    It "CreatesSqlServerDatabase" {
        $db = [MeasureTrace.Database]::Connect($sqlServerConnectionString)
        $db.Database.EnsureCreated()
        $db.Database.EnsureDeleted()
        {$db.Database.EnsureCreated()} | Should Not Throw
        $db.Dispose()
    }
    It "Can save a trace with measurements" {
        $trace = New-Object -TypeName 'MeasureTrace.TraceModel.Trace'
        $trace.PackageFileName = "filename"
        $measurement = New-Object -TypeName 'MeasureTrace.TraceModel.BootPhase'
        $measurement.DurationMSec = 100
        $measurement2 = New-Object -TypeName 'MeasureTrace.TraceModel.DiskIo'
        $trace.AddMeasurement($measurement)
        $trace.AddMeasurement($measurement2)
        $db = [MeasureTrace.Database]::Connect($sqlServerConnectionString)
        $db.AddTraceWithMeasurements($trace)
        $db.SaveChanges() | Should Be 3
        $db.Dispose()
    }

    It "Can retrieve trace with measurements" {
        $db = [MeasureTrace.Database]::Connect($sqlServerConnectionString)
        $traceX = $db.Traces | Select -First 1
        $traceX | Should Not Be Null
        $db.HydrateTrace($traceX)
        $traceX.GetMeasurementsAll().Count | Should Be 2
        $db.Dispose()
    }
}

Describe "MeasureTrace.Database powershell module" {
    $sqlServerConnectionString = "server=(localdb)\MsSqlLocalDb;database=bar;MultipleActiveResultSets=True"
    It "Module loads" {
        Import-Module -Name (Join-Path $env:temp "MeasureTrace.Database") -ErrorAction Stop
    }

    It "Can save an arbitrary trace to db and retrieve it again" {
        Import-Module -Name (Join-Path $env:temp "MeasureTrace.Database")
        $db = Connect-MtDbRepository -ConnectionString $sqlServerConnectionString
        $db.Database.EnsureCreated()
        $db.Database.EnsureDeleted()
        $db.Database.EnsureCreated()
        $db.Dispose()
        $trace = New-Object -TypeName 'MeasureTrace.TraceModel.Trace'
        $trace.PackageFileName = "ArbitraryTraceFileName"
        $measurement = New-Object -TypeName 'MeasureTrace.TraceModel.BootPhase'
        $measurement.DurationMSec = 100
        $measurement2 = New-Object -TypeName 'MeasureTrace.TraceModel.DiskIo'
        $trace.AddMeasurement($measurement)
        $trace.AddMeasurement($measurement2)
        Add-MtDbTrace -ConnectionString $sqlServerConnectionString -MeasuredTrace $trace
        $db = Connect-MtDbRepository -ConnectionString $sqlServerConnectionString
        $t = $db.GetTrace("ArbitraryTraceFileName")
        $t | Should Not Be Null
        $db.HydrateTrace($t)
        $t.Measurements['BootPhase'].Count | Should Be 1
    }

}