@@ -27,42 +27,44 @@ import (
2727 "github.com/stretchr/testify/require"
2828)
2929
30+ const benchmarkCount = 10
31+
3032func createAndStopVM (
3133 ctx context.Context ,
3234 fcClient fccontrol.FirecrackerService ,
3335 request proto.CreateVMRequest ,
34- ) error {
36+ ) (time. Duration , error ) {
3537 uuid , err := uuid .NewV4 ()
3638 if err != nil {
37- return err
39+ return 0 , err
3840 }
3941
4042 request .VMID = uuid .String ()
4143
44+ t0 := time .Now ()
4245 _ , err = fcClient .CreateVM (ctx , & request )
4346 if err != nil {
44- return err
47+ return 0 , err
4548 }
49+ elapsed := time .Since (t0 )
4650
4751 _ , err = fcClient .StopVM (ctx , & proto.StopVMRequest {VMID : request .VMID })
4852 if err != nil {
49- return err
53+ return 0 , err
5054 }
5155
52- return nil
56+ return elapsed , nil
5357}
5458
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 ) {
5860 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 )
6062 defer client .Close ()
6163
6264 ctx := namespaces .WithNamespace (context .Background (), "default" )
6365
6466 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" )
6668
6769 fcClient := fccontrol .NewFirecrackerClient (pluginClient .Client ())
6870 request := proto.CreateVMRequest {
@@ -72,52 +74,42 @@ func benchmarkCreateAndStopVM(b *testing.B, vcpuCount uint32, kernelArgs string,
7274 },
7375 }
7476
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 )
9387 }
9488
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
9892 }
99- }
93+ average = time . Duration ( int64 ( average ) / int64 ( len ( samples )))
10094
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 )
10896}
10997
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+ })
123115}
0 commit comments