summaryrefslogtreecommitdiffhomepage
path: root/test/benchmarks/base/startup_test.go
blob: 8ef9f99c4918ccaca4e51ab53e81f9684f5a0e0b (plain)
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
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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 startup_test

import (
	"context"
	"fmt"
	"os"
	"testing"

	"gvisor.dev/gvisor/pkg/test/dockerutil"
	"gvisor.dev/gvisor/test/benchmarks/base"
	"gvisor.dev/gvisor/test/benchmarks/harness"
)

var testHarness harness.Harness

// BenchmarkStartEmpty times startup time for an empty container.
func BenchmarkStartupEmpty(b *testing.B) {
	machine, err := testHarness.GetMachine()
	if err != nil {
		b.Fatalf("failed to get machine: %v", err)
	}
	defer machine.CleanUp()

	ctx := context.Background()
	for i := 0; i < b.N; i++ {
		harness.DebugLog(b, "Running container: %d", i)
		container := machine.GetContainer(ctx, b)
		defer container.CleanUp(ctx)
		if _, err := container.Run(ctx, dockerutil.RunOpts{
			Image: "benchmarks/alpine",
		}, "true"); err != nil {
			b.Fatalf("failed to run container: %v", err)
		}
		harness.DebugLog(b, "Ran container: %d", i)
	}
}

// BenchmarkStartupNginx times startup for a Nginx instance.
// Time is measured from start until the first request is served.
func BenchmarkStartupNginx(b *testing.B) {
	// The machine to hold Nginx and the Node Server.
	machine, err := testHarness.GetMachine()
	if err != nil {
		b.Fatalf("failed to get machine with: %v", err)
	}
	defer machine.CleanUp()

	ctx := context.Background()
	runOpts := dockerutil.RunOpts{
		Image: "benchmarks/nginx",
	}
	runServerWorkload(ctx, b,
		base.ServerArgs{
			Machine: machine,
			RunOpts: runOpts,
			Port:    80,
			Cmd:     []string{"nginx", "-c", "/etc/nginx/nginx_gofer.conf"},
		})
}

// BenchmarkStartupNode times startup for a Node application instance.
// Time is measured from start until the first request is served.
// Note that the Node app connects to a Redis instance before serving.
func BenchmarkStartupNode(b *testing.B) {
	machine, err := testHarness.GetMachine()
	if err != nil {
		b.Fatalf("failed to get machine with: %v", err)
	}
	defer machine.CleanUp()

	ctx := context.Background()
	redis, redisIP := base.RedisInstance(ctx, b, machine)
	defer redis.CleanUp(ctx)
	runOpts := dockerutil.RunOpts{
		Image:   "benchmarks/node",
		WorkDir: "/usr/src/app",
		Links:   []string{redis.MakeLink("redis")},
	}

	cmd := []string{"node", "index.js", redisIP.String()}
	runServerWorkload(ctx, b,
		base.ServerArgs{
			Machine: machine,
			Port:    8080,
			RunOpts: runOpts,
			Cmd:     cmd,
		})
}

// runServerWorkload runs a server workload defined by 'runOpts' and 'cmd'.
// 'clientMachine' is used to connect to the server on 'serverMachine'.
func runServerWorkload(ctx context.Context, b *testing.B, args base.ServerArgs) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		harness.DebugLog(b, "Running iteration: %d", i)
		if err := func() error {
			server := args.Machine.GetContainer(ctx, b)
			defer func() {
				b.StopTimer()
				// Cleanup servers as we run so that we can go indefinitely.
				server.CleanUp(ctx)
				b.StartTimer()
			}()
			harness.DebugLog(b, "Spawning container: %s", args.RunOpts.Image)
			if err := server.Spawn(ctx, args.RunOpts, args.Cmd...); err != nil {
				return fmt.Errorf("failed to spawn node instance: %v", err)
			}

			harness.DebugLog(b, "Finding Container IP")
			servingIP, err := server.FindIP(ctx, false)
			if err != nil {
				return fmt.Errorf("failed to get ip from server: %v", err)
			}

			harness.DebugLog(b, "Waiting for container to start.")
			// Wait until the Client sees the server as up.
			if err := harness.WaitUntilServing(ctx, args.Machine, servingIP, args.Port); err != nil {
				return fmt.Errorf("failed to wait for serving: %v", err)
			}
			return nil
		}(); err != nil {
			b.Fatal(err)
		}
		harness.DebugLog(b, "Ran iteration: %d", i)
	}
}

// TestMain is the main method for package network.
func TestMain(m *testing.M) {
	testHarness.Init()
	os.Exit(m.Run())
}