Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux GCS tests and benchmarks #1352

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions scripts/Test-LCOW-UVM.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#ex: .\scripts\Test-LCOW-UVM.ps1 -vb -Action Bench -BootFilesPath C:\ContainerPlat\LinuxBootFiles\ -MountGCSTest -Count 2 -Benchtime '3s'
# benchstat via `go install golang.org/x/perf/cmd/benchstat@latest`

[CmdletBinding()]
param (
[ValidateSet('Test', 'Bench', 'List', 'Shell')]
[alias('a')]
[string]
$Action = 'Bench',

[string]
$Note = '',

# test parameters
[int]
$Count = 1,

[string]
$BenchTime = '5s',

[string]
$Timeout = '10m',

[alias('tv')]
[switch]
$TestVerbose,

[string]
$Run = '',

[string]
$CodePath = '.',

[string]
$OutDirectory = '.\test\results',

# uvm parameters

[string]
$BootFilesPath = 'C:\ContainerPlat\LinuxBootFiles',

[ValidateSet('vhd', 'initrd')]
[string]
$BootFSType = 'vhd',

[switch]
$DisableTimeSync,

# gcs test/container options

[string]
$ContainerRootFSMount = '/run/rootfs',

[string]
$ContainerRootFSPath = (Join-Path $BootFilesPath 'rootfs.vhd'),

[string]
$GCSTestMount = '/run/bin',

[string]
$GCSTestPath = '.\bin\test\gcs.test',

[switch]
$MountGCSTest,

[string]
$Feature = ''
)

Import-Module ( Join-Path $PSScriptRoot Testing.psm1 ) -Force

$CodePath = Resolve-Path $CodePath
$OutDirectory = Resolve-Path $OutDirectory
$BootFilesPath = Resolve-Path $BootFilesPath
$ContainerRootFSPath = Resolve-Path $ContainerRootFSPath
$GCSTestPath = Resolve-Path $GCSTestPath

$shell = ( $Action -eq 'Shell' )

if ( $shell ) {
$cmd = 'ash'
} else {
$date = Get-Date
$waitfiles = "$ContainerRootFSMount"
$gcspath = 'gcs.test'
if ( $MountGCSTest ) {
$waitfiles += ",$GCSTestMount"
$gcspath = "$GCSTestMount/gcs.test"
}

$pre = "wait-paths -p $waitfiles -t 5 ; " + `
helsaawy marked this conversation as resolved.
Show resolved Hide resolved
helsaawy marked this conversation as resolved.
Show resolved Hide resolved
'echo nproc: `$(nproc) ; ' + `
'echo kernel: `$(uname -a) ; ' + `
'echo gcs.commit: `$(cat /info/gcs.commit 2>/dev/null) ; ' + `
'echo gcs.branch: `$(cat /info/gcs.branch 2>/dev/null) ; ' + `
'echo tar.date: `$(cat /info/tar.date 2>/dev/null) ; ' + `
'echo image.name: `$(cat /info/image.name 2>/dev/null) ; ' + `
'echo build.date: `$(cat /info/build.date 2>/dev/null) ; '

$testcmd, $out = New-TestCommand `
-Action $Action `
-Path $gcspath `
-Name gcstest `
-OutDirectory $OutDirectory `
-Date $date `
-Note $Note `
-TestVerbose:$TestVerbose `
-Count $Count `
-BenchTime $BenchTime `
-Timeout $Timeout `
-Run $Run `
-Feature $Feature `
-Verbose:$Verbose

$testcmd += " `'-rootfs-path=$ContainerRootFSMount`' "
$cmd = $pre + $testcmd
}

$boot = '.\bin\tool\uvmboot.exe -gcs lcow ' + `
'-fwd-stdout -fwd-stderr -output-handling stdout ' + `
"-boot-files-path $BootFilesPath " + `
"-root-fs-type $BootFSType " + `
'-kernel-file vmlinux ' + `
helsaawy marked this conversation as resolved.
Show resolved Hide resolved
"-mount-scsi `"$ContainerRootFSPath,$ContainerRootFSMount`" "

if ( $MountGCSTest ) {
$boot += "-share `"$GCSTestPath,$GCSTestMount`" "
}

if ( $DisableTimeSync ) {
$boot += ' -disable-time-sync '
}

if ( $shell ) {
$boot += ' -t '
}

$boot += " -exec `"$cmd`" "

Invoke-TestCommand `
-TestCmd $boot `
-TestCmdPreamble $testcmd `
Comment on lines +141 to +142
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't $boot include $cmd which has $testcmd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but -TestCmdPreamble just writes out the string argument before execing the tests.
its a bit convoluted, but this way, before the test output runs, we get headers with the test info, including the command that was run, so we can just save the output on its own, if need be, and still have all the data we need:

date: 20220412T1701329409
note: 46-117
test.command: .\bin\test\functional.exe -feature="LCOW"  "-test.shuffle=on" "-test.run=^#" "-test.bench=."  "-test.benchmem" "-test.benchtime=5s" "-test.count=5" 
repo.commit: 5631c613afeabbac116133f06a4b2ca17fe83d8e
-test.shuffle 1649797293380880100
goos: windows
goarch: amd64
pkg: github.com/Microsoft/hcsshim/test/functional
cpu: Intel(R) Xeon(R) W-2235 CPU @ 3.80GHz
BenchmarkLCOWStart-12                            	       7	 806345986 ns/op	   16400 B/op	     157 allocs/op
# ...

If we printed $boot, it would have all the boot and mount and a bunch of other redundant info

-OutputFile (&{ if ( $Action -ne 'Shell' ) { $out } }) `
-OutputCmd (&{ if ( $Action -eq 'Bench' ) { 'benchstat' } }) `
-Preamble `
-Date $Date `
-Note $Note `
-Verbose:$Verbose
226 changes: 226 additions & 0 deletions test/gcs/container_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
//go:build linux

package gcs

import (
"context"
"testing"

"github.com/Microsoft/hcsshim/internal/guest/prot"
"github.com/Microsoft/hcsshim/internal/guest/runtime/hcsv2"
"github.com/Microsoft/hcsshim/internal/guest/stdio"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
cri_util "github.com/containerd/containerd/pkg/cri/util"

testoci "github.com/Microsoft/hcsshim/test/internal/oci"
)

func BenchmarkContainerCreate(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := namespaces.WithNamespace(context.Background(), testoci.DefaultNamespace)
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id := b.Name() + cri_util.GenerateID()
scratch, rootfs := mountRootfs(ctx, b, host, id)

s := testoci.CreateLinuxSpec(ctx, b, id,
testoci.DefaultLinuxSpecOpts(id,
oci.WithRootFSPath(rootfs),
oci.WithProcessArgs("/bin/sh", "-c", tailNull),
)...,
)
r := &prot.VMHostedContainerSettingsV2{
OCIBundlePath: scratch,
OCISpecification: s,
}

b.StartTimer()
c := createContainer(ctx, b, host, id, r)
b.StopTimer()

// create launches background go-routines
// so kill container to end those and avoid future perf hits
killContainer(ctx, b, c)
deleteContainer(ctx, b, c)
removeContainer(ctx, b, host, id)
unmountRootfs(ctx, b, scratch)
}
}

func BenchmarkContainerStart(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := context.Background()
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, r, cleanup := standaloneContainerRequest(ctx, b, host)

c := createContainer(ctx, b, host, id, r)

b.StartTimer()
p := startContainer(ctx, b, c, stdio.ConnectionSettings{})
b.StopTimer()

killContainer(ctx, b, c)
waitContainer(ctx, b, c, p, true)
cleanupContainer(ctx, b, host, c)
cleanup()
}
}

func BenchmarkContainerKill(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := context.Background()
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, r, cleanup := standaloneContainerRequest(ctx, b, host)
c := createContainer(ctx, b, host, id, r)
p := startContainer(ctx, b, c, stdio.ConnectionSettings{})

b.StartTimer()
killContainer(ctx, b, c)
_, n := waitContainerRaw(c, p)
b.StopTimer()

switch n {
case prot.NtForcedExit:
default:
b.Fatalf("container exit was %s", n)
}

cleanupContainer(ctx, b, host, c)
cleanup()
}
}

// benchmark container create through wait until exit.
func BenchmarkContainerCompleteExit(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := context.Background()
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, r, cleanup := standaloneContainerRequest(ctx, b, host, oci.WithProcessArgs("/bin/sh", "-c", "true"))

b.StartTimer()
c := createContainer(ctx, b, host, id, r)
p := startContainer(ctx, b, c, stdio.ConnectionSettings{})
e, n := waitContainerRaw(c, p)
b.StopTimer()

switch n {
case prot.NtGracefulExit, prot.NtUnexpectedExit:
default:
b.Fatalf("container exit was %s", n)
}

if e != 0 {
b.Fatalf("container exit code was %d", e)
}

killContainer(ctx, b, c)
c.Wait()
cleanupContainer(ctx, b, host, c)
cleanup()
}
}

func BenchmarkContainerCompleteKill(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := context.Background()
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, r, cleanup := standaloneContainerRequest(ctx, b, host)

b.StartTimer()
c := createContainer(ctx, b, host, id, r)
p := startContainer(ctx, b, c, stdio.ConnectionSettings{})
killContainer(ctx, b, c)
_, n := waitContainerRaw(c, p)
b.StopTimer()

switch n {
case prot.NtForcedExit:
default:
b.Fatalf("container exit was %s", n)
}

cleanupContainer(ctx, b, host, c)
cleanup()
}
}

func BenchmarkContainerExec(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := namespaces.WithNamespace(context.Background(), testoci.DefaultNamespace)
host, _ := getTestState(ctx, b)

id := b.Name()
c := createStandaloneContainer(ctx, b, host, id)
ip := startContainer(ctx, b, c, stdio.ConnectionSettings{})

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ps := testoci.CreateLinuxSpec(ctx, b, id,
oci.WithDefaultPathEnv,
oci.WithProcessArgs("/bin/sh", "-c", "true"),
).Process

b.StartTimer()
p := execProcess(ctx, b, c, ps, stdio.ConnectionSettings{})
exch, dch := p.Wait()
if e := <-exch; e != 0 {
b.Errorf("process exited with error code %d", e)
}
b.StopTimer()

dch <- true
close(dch)
}

killContainer(ctx, b, c)
waitContainer(ctx, b, c, ip, true)
cleanupContainer(ctx, b, host, c)
}

func standaloneContainerRequest(
ctx context.Context,
t testing.TB,
host *hcsv2.Host,
extra ...oci.SpecOpts,
) (string, *prot.VMHostedContainerSettingsV2, func()) {
ctx = namespaces.WithNamespace(ctx, testoci.DefaultNamespace)
id := t.Name() + cri_util.GenerateID()
scratch, rootfs := mountRootfs(ctx, t, host, id)

opts := testoci.DefaultLinuxSpecOpts(id,
oci.WithRootFSPath(rootfs),
oci.WithProcessArgs("/bin/sh", "-c", tailNull),
)
opts = append(opts, extra...)
s := testoci.CreateLinuxSpec(ctx, t, id, opts...)
r := &prot.VMHostedContainerSettingsV2{
OCIBundlePath: scratch,
OCISpecification: s,
}
f := func() {
unmountRootfs(ctx, t, scratch)
}

return id, r, f
}
Loading