gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/network/node_test.go (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package network 15 16 import ( 17 "context" 18 "os" 19 "strconv" 20 "testing" 21 "time" 22 23 "gvisor.dev/gvisor/pkg/test/dockerutil" 24 "gvisor.dev/gvisor/test/benchmarks/harness" 25 "gvisor.dev/gvisor/test/benchmarks/tools" 26 "gvisor.dev/gvisor/test/metricsviz" 27 ) 28 29 // BenchmarkNode runs requests using 'hey' against a Node server run on 30 // 'runtime'. The server responds to requests by grabbing some data in a 31 // redis instance and returns the data in its reponse. The test loops through 32 // increasing amounts of concurency for requests. 33 func BenchmarkNode(b *testing.B) { 34 concurrency := []int{1, 5, 10, 25} 35 for _, c := range concurrency { 36 param := tools.Parameter{ 37 Name: "concurrency", 38 Value: strconv.Itoa(c), 39 } 40 name, err := tools.ParametersToName(param) 41 if err != nil { 42 b.Fatalf("Failed to parse parameters: %v", err) 43 } 44 b.Run(name, func(b *testing.B) { 45 hey := &tools.Hey{ 46 Requests: b.N, 47 Concurrency: c, 48 } 49 runNode(b, hey) 50 }) 51 } 52 } 53 54 // runNode runs the test for a given # of requests and concurrency. 55 func runNode(b *testing.B, hey *tools.Hey) { 56 b.Helper() 57 58 // The machine to hold Redis and the Node Server. 59 serverMachine, err := harness.GetMachine() 60 if err != nil { 61 b.Fatalf("failed to get machine with: %v", err) 62 } 63 defer serverMachine.CleanUp() 64 65 // The machine to run 'hey'. 66 clientMachine, err := harness.GetMachine() 67 if err != nil { 68 b.Fatalf("failed to get machine with: %v", err) 69 } 70 defer clientMachine.CleanUp() 71 72 ctx := context.Background() 73 74 // Spawn a redis instance for the app to use. 75 redis := serverMachine.GetNativeContainer(ctx, b) 76 if err := redis.Spawn(ctx, dockerutil.RunOpts{ 77 Image: "benchmarks/redis", 78 }); err != nil { 79 b.Fatalf("failed to spawn redis instance: %v", err) 80 } 81 defer redis.CleanUp(ctx) 82 defer metricsviz.FromContainerLogs(ctx, b, redis) 83 84 if out, err := redis.WaitForOutput(ctx, "Ready to accept connections", 3*time.Second); err != nil { 85 b.Fatalf("failed to start redis server: %v %s", err, out) 86 } 87 redisIP, err := redis.FindIP(ctx, false) 88 if err != nil { 89 b.Fatalf("failed to get IP from redis instance: %v", err) 90 } 91 92 // Node runs on port 8080. 93 port := 8080 94 95 // Start-up the Node server. 96 nodeApp := serverMachine.GetContainer(ctx, b) 97 if err := nodeApp.Spawn(ctx, dockerutil.RunOpts{ 98 Image: "benchmarks/node", 99 WorkDir: "/usr/src/app", 100 Links: []string{redis.MakeLink("redis")}, 101 Ports: []int{port}, 102 }, "node", "index.js", redisIP.String()); err != nil { 103 b.Fatalf("failed to spawn node instance: %v", err) 104 } 105 defer nodeApp.CleanUp(ctx) 106 107 // Wait until the Client sees the server as up. 108 harness.WaitUntilContainerServing(ctx, clientMachine, nodeApp, port) 109 110 heyCmd := hey.MakeCmd("node", port) 111 112 // the client should run on Native. 113 b.ResetTimer() 114 client := clientMachine.GetNativeContainer(ctx, b) 115 out, err := client.Run(ctx, dockerutil.RunOpts{ 116 Image: "benchmarks/hey", 117 Links: []string{nodeApp.MakeLink("node")}, 118 }, heyCmd...) 119 if err != nil { 120 b.Fatalf("hey container failed: %v logs: %s", err, out) 121 } 122 123 // Stop the timer to parse the data and report stats. 124 hey.Report(b, out) 125 } 126 127 func TestMain(m *testing.M) { 128 harness.Init() 129 os.Exit(m.Run()) 130 }