|
| 1 | +// Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/containerd/containerd" |
| 22 | + "github.com/containerd/containerd/namespaces" |
| 23 | + "github.com/containerd/containerd/pkg/ttrpcutil" |
| 24 | + "github.com/firecracker-microvm/firecracker-containerd/proto" |
| 25 | + fccontrol "github.com/firecracker-microvm/firecracker-containerd/proto/service/fccontrol/ttrpc" |
| 26 | + "github.com/gofrs/uuid" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func createAndStopVM( |
| 31 | + ctx context.Context, |
| 32 | + fcClient fccontrol.FirecrackerService, |
| 33 | + request proto.CreateVMRequest, |
| 34 | +) error { |
| 35 | + uuid, err := uuid.NewV4() |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + request.VMID = uuid.String() |
| 41 | + |
| 42 | + _, err = fcClient.CreateVM(ctx, &request) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + _, err = fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: request.VMID}) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func benchmarkCreateAndStopVM(b *testing.B, vcpuCount uint32, kernelArgs string, parallel int) { |
| 56 | + require := require.New(b) |
| 57 | + |
| 58 | + client, err := containerd.New(containerdSockPath, containerd.WithDefaultRuntime(firecrackerRuntime)) |
| 59 | + require.NoError(err, "unable to create client to containerd service at %s, is containerd running?", containerdSockPath) |
| 60 | + defer client.Close() |
| 61 | + |
| 62 | + ctx := namespaces.WithNamespace(context.Background(), "default") |
| 63 | + |
| 64 | + pluginClient, err := ttrpcutil.NewClient(containerdSockPath + ".ttrpc") |
| 65 | + require.NoError(err, "failed to create ttrpc client") |
| 66 | + |
| 67 | + fcClient := fccontrol.NewFirecrackerClient(pluginClient.Client()) |
| 68 | + request := proto.CreateVMRequest{ |
| 69 | + KernelArgs: kernelArgs, |
| 70 | + MachineCfg: &proto.FirecrackerMachineConfiguration{ |
| 71 | + VcpuCount: vcpuCount, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + ch := make(chan error, parallel) |
| 76 | + for i := 0; i < parallel; i++ { |
| 77 | + go func() { |
| 78 | + var err error |
| 79 | + defer func() { |
| 80 | + ch <- err |
| 81 | + }() |
| 82 | + |
| 83 | + for i := 0; i < b.N; i++ { |
| 84 | + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 85 | + defer cancel() |
| 86 | + |
| 87 | + err = createAndStopVM(ctx, fcClient, request) |
| 88 | + if err != nil { |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | + }() |
| 93 | + } |
| 94 | + |
| 95 | + // Make sure all goroutines are finished successfully. |
| 96 | + for i := 0; i < parallel; i++ { |
| 97 | + require.NoError(<-ch) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func BenchmarkCreateAndStopVM_Vcpu1_Isolated(b *testing.B) { |
| 102 | + prepareIntegTest(b) |
| 103 | + benchmarkCreateAndStopVM(b, 1, defaultRuntimeConfig.KernelArgs, 1) |
| 104 | +} |
| 105 | +func BenchmarkCreateAndStopVM_Vcpu5_Isolated(b *testing.B) { |
| 106 | + prepareIntegTest(b) |
| 107 | + benchmarkCreateAndStopVM(b, 5, defaultRuntimeConfig.KernelArgs, 1) |
| 108 | +} |
| 109 | + |
| 110 | +func BenchmarkCreateAndStopVM_Quiet_Isolated(b *testing.B) { |
| 111 | + prepareIntegTest(b) |
| 112 | + benchmarkCreateAndStopVM( |
| 113 | + b, |
| 114 | + 1, |
| 115 | + // Same as https://github.com/firecracker-microvm/firecracker-demo/blob/c22499567b63b4edd85e19ca9b0e9fa398b3300b/start-firecracker.sh#L9 |
| 116 | + "ro noapic reboot=k panic=1 pci=off nomodules systemd.log_color=false systemd.unit=firecracker.target init=/sbin/overlay-init tsc=reliable quiet 8250.nr_uarts=0 ipv6.disable=1", |
| 117 | + 1, |
| 118 | + ) |
| 119 | +} |
| 120 | +func BenchmarkCreateAndStopVM_Parallel10_Isolated(b *testing.B) { |
| 121 | + prepareIntegTest(b) |
| 122 | + benchmarkCreateAndStopVM(b, 1, defaultRuntimeConfig.KernelArgs, 10) |
| 123 | +} |
0 commit comments