@@ -27,42 +27,44 @@ import (
27
27
"github.com/stretchr/testify/require"
28
28
)
29
29
30
+ const benchmarkCount = 10
31
+
30
32
func createAndStopVM (
31
33
ctx context.Context ,
32
34
fcClient fccontrol.FirecrackerService ,
33
35
request proto.CreateVMRequest ,
34
- ) error {
36
+ ) (time. Duration , error ) {
35
37
uuid , err := uuid .NewV4 ()
36
38
if err != nil {
37
- return err
39
+ return 0 , err
38
40
}
39
41
40
42
request .VMID = uuid .String ()
41
43
44
+ t0 := time .Now ()
42
45
_ , err = fcClient .CreateVM (ctx , & request )
43
46
if err != nil {
44
- return err
47
+ return 0 , err
45
48
}
49
+ elapsed := time .Since (t0 )
46
50
47
51
_ , err = fcClient .StopVM (ctx , & proto.StopVMRequest {VMID : request .VMID })
48
52
if err != nil {
49
- return err
53
+ return 0 , err
50
54
}
51
55
52
- return nil
56
+ return elapsed , nil
53
57
}
54
58
55
- func benchmarkCreateAndStopVM (b * testing.B , vcpuCount uint32 , kernelArgs string , parallel int ) {
56
- require := require .New (b )
57
-
59
+ func benchmarkCreateAndStopVM (t * testing.T , vcpuCount uint32 , kernelArgs string ) {
58
60
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 )
61
+ require .NoError (t , err , "unable to create client to containerd service at %s, is containerd running?" , containerdSockPath )
60
62
defer client .Close ()
61
63
62
64
ctx := namespaces .WithNamespace (context .Background (), "default" )
63
65
64
66
pluginClient , err := ttrpcutil .NewClient (containerdSockPath + ".ttrpc" )
65
- require .NoError (err , "failed to create ttrpc client" )
67
+ require .NoError (t , err , "failed to create ttrpc client" )
66
68
67
69
fcClient := fccontrol .NewFirecrackerClient (pluginClient .Client ())
68
70
request := proto.CreateVMRequest {
@@ -72,52 +74,42 @@ func benchmarkCreateAndStopVM(b *testing.B, vcpuCount uint32, kernelArgs string,
72
74
},
73
75
}
74
76
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
- }()
77
+ var samples []time.Duration
78
+ for i := 0 ; i < benchmarkCount ; i ++ {
79
+ ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
80
+ defer cancel ()
81
+
82
+ elapsed , err := createAndStopVM (ctx , fcClient , request )
83
+ if err != nil {
84
+ return
85
+ }
86
+ samples = append (samples , elapsed )
93
87
}
94
88
95
- // Make sure all goroutines are finished successfully.
96
- for i := 0 ; i < parallel ; i ++ {
97
- require . NoError ( <- ch )
89
+ var average time. Duration
90
+ for _ , x := range samples {
91
+ average += x
98
92
}
99
- }
93
+ average = time . Duration ( int64 ( average ) / int64 ( len ( samples )))
100
94
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 )
95
+ t .Logf ("%s: avg:%v samples:%v" , t .Name (), average , samples )
108
96
}
109
97
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 )
98
+ func TestPerf_CreateVM (t * testing.T ) {
99
+ prepareIntegTest (t )
100
+
101
+ t .Run ("vcpu=1" , func (subtest * testing.T ) {
102
+ benchmarkCreateAndStopVM (subtest , 1 , defaultRuntimeConfig .KernelArgs )
103
+ })
104
+ t .Run ("vcpu=5" , func (subtest * testing.T ) {
105
+ benchmarkCreateAndStopVM (subtest , 1 , defaultRuntimeConfig .KernelArgs )
106
+ })
107
+ t .Run ("firecracker-demo" , func (subtest * testing.T ) {
108
+ benchmarkCreateAndStopVM (
109
+ subtest ,
110
+ 1 ,
111
+ // Same as https://github.com/firecracker-microvm/firecracker-demo/blob/c22499567b63b4edd85e19ca9b0e9fa398b3300b/start-firecracker.sh#L9
112
+ "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" ,
113
+ )
114
+ })
123
115
}
0 commit comments