-
Notifications
You must be signed in to change notification settings - Fork 127
/
benchmark_test.go
155 lines (134 loc) · 3.61 KB
/
benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package firecracker
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/firecracker-microvm/firecracker-go-sdk/client/models"
)
const numberOfVMs = 200
func createMachine(ctx context.Context, name string, forwardSignals []os.Signal) (*Machine, func(), error) {
dir, err := os.MkdirTemp("", name)
if err != nil {
return nil, nil, err
}
cleanup := func() {
os.RemoveAll(dir)
}
socketPath := filepath.Join(dir, "api.sock")
vmlinuxPath := filepath.Join(testDataPath, "./vmlinux")
logFifo := filepath.Join(dir, "log.fifo")
metrics := filepath.Join(dir, "metrics.fifo")
config := Config{
SocketPath: socketPath,
KernelImagePath: vmlinuxPath,
LogFifo: logFifo,
MetricsFifo: metrics,
LogLevel: "Info",
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(1),
MemSizeMib: Int64(256),
Smt: Bool(false),
},
Drives: []models.Drive{
{
DriveID: String("root"),
IsRootDevice: Bool(true),
IsReadOnly: Bool(true),
PathOnHost: String(testRootfs),
},
},
ForwardSignals: forwardSignals,
}
cmd := VMCommandBuilder{}.
WithSocketPath(socketPath).
WithBin(getFirecrackerBinaryPath()).
Build(ctx)
log := logrus.New()
log.SetLevel(logrus.FatalLevel)
machine, err := NewMachine(ctx, config, WithProcessRunner(cmd), WithLogger(logrus.NewEntry(log)))
if err != nil {
return nil, cleanup, err
}
return machine, cleanup, nil
}
func startAndWaitVM(ctx context.Context, m *Machine) error {
err := m.Start(ctx)
if err != nil {
return err
}
file, err := os.Open(m.LogFile())
if err != nil {
return err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Guest-boot-time") {
break
}
}
err = m.StopVMM()
if err != nil {
return err
}
err = m.Wait(ctx)
if err != nil {
return err
}
return nil
}
func benchmarkForwardSignals(b *testing.B, forwardSignals []os.Signal) {
ctx := context.Background()
b.Logf("%s: %d", b.Name(), b.N)
for i := 0; i < b.N; i++ {
errCh := make(chan error, numberOfVMs)
for j := 0; j < numberOfVMs; j++ {
go func() {
var err error
defer func() { errCh <- err }()
machine, cleanup, err := createMachine(ctx, b.Name(), forwardSignals)
if err != nil {
err = fmt.Errorf("failed to create a VM: %v", err)
return // anonymous defer func() will deliver the error
}
defer cleanup()
err = startAndWaitVM(ctx, machine)
if err != nil && !strings.Contains(err.Error(), "signal: terminated") {
err = fmt.Errorf("failed to start the VM: %v", err)
return // anonymous defer func() will deliver the error
}
return // anonymous defer func() will deliver this nil error
}()
}
for k := 0; k < numberOfVMs; k++ {
err := <-errCh
if err != nil {
b.Fatal(err)
}
}
close(errCh)
}
}
func BenchmarkForwardSignalsDefault(t *testing.B) {
benchmarkForwardSignals(t, nil)
}
func BenchmarkForwardSignalsDisable(t *testing.B) {
benchmarkForwardSignals(t, []os.Signal{})
}