gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/network/ruby_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  	"fmt"
    19  	"os"
    20  	"strconv"
    21  	"testing"
    22  	"time"
    23  
    24  	"gvisor.dev/gvisor/pkg/test/dockerutil"
    25  	"gvisor.dev/gvisor/test/benchmarks/harness"
    26  	"gvisor.dev/gvisor/test/benchmarks/tools"
    27  	"gvisor.dev/gvisor/test/metricsviz"
    28  )
    29  
    30  // BenchmarkRuby runs requests using 'hey' against a ruby application server.
    31  // On start, ruby app generates some random data and pushes it to a redis
    32  // instance. On a request, the app grabs for random entries from the redis
    33  // server, publishes it to a document, and returns the doc to the request.
    34  func BenchmarkRuby(b *testing.B) {
    35  	concurrency := []int{1, 5, 10, 25}
    36  	for _, c := range concurrency {
    37  		param := tools.Parameter{
    38  			Name:  "concurrency",
    39  			Value: strconv.Itoa(c),
    40  		}
    41  		name, err := tools.ParametersToName(param)
    42  		if err != nil {
    43  			b.Fatalf("Failed to parse parameters: %v", err)
    44  		}
    45  		b.Run(name, func(b *testing.B) {
    46  			hey := &tools.Hey{
    47  				Requests:    b.N,
    48  				Concurrency: c,
    49  			}
    50  			runRuby(b, hey)
    51  		})
    52  	}
    53  }
    54  
    55  // runRuby runs the test for a given # of requests and concurrency.
    56  func runRuby(b *testing.B, hey *tools.Hey) {
    57  	// The machine to hold Redis and the Ruby Server.
    58  	serverMachine, err := harness.GetMachine()
    59  	if err != nil {
    60  		b.Fatalf("failed to get machine with: %v", err)
    61  	}
    62  	defer serverMachine.CleanUp()
    63  
    64  	// The machine to run 'hey'.
    65  	clientMachine, err := harness.GetMachine()
    66  	if err != nil {
    67  		b.Fatalf("failed to get machine with: %v", err)
    68  	}
    69  	defer clientMachine.CleanUp()
    70  	ctx := context.Background()
    71  
    72  	// Spawn a redis instance for the app to use.
    73  	redis := serverMachine.GetNativeContainer(ctx, b)
    74  	if err := redis.Spawn(ctx, dockerutil.RunOpts{
    75  		Image: "benchmarks/redis",
    76  	}); err != nil {
    77  		b.Fatalf("failed to spawn redis instance: %v", err)
    78  	}
    79  	defer redis.CleanUp(ctx)
    80  	defer metricsviz.FromContainerLogs(ctx, b, redis)
    81  
    82  	if out, err := redis.WaitForOutput(ctx, "Ready to accept connections", 3*time.Second); err != nil {
    83  		b.Fatalf("failed to start redis server: %v %s", err, out)
    84  	}
    85  
    86  	// Ruby runs on port 9292.
    87  	const port = 9292
    88  
    89  	// Start-up the Ruby server.
    90  	rubyApp := serverMachine.GetContainer(ctx, b)
    91  	if err := rubyApp.Spawn(ctx, dockerutil.RunOpts{
    92  		Image:   "benchmarks/ruby",
    93  		WorkDir: "/app",
    94  		Links:   []string{redis.MakeLink("redis")},
    95  		Ports:   []int{port},
    96  		Env: []string{
    97  			fmt.Sprintf("PORT=%d", port),
    98  			"WEB_CONCURRENCY=20",
    99  			"WEB_MAX_THREADS=20",
   100  			"RACK_ENV=production",
   101  			"HOST=redis",
   102  		},
   103  		User: "nobody",
   104  	}, "sh", "-c", "/usr/bin/puma"); err != nil {
   105  		b.Fatalf("failed to spawn node instance: %v", err)
   106  	}
   107  	defer rubyApp.CleanUp(ctx)
   108  
   109  	// Wait until the Client sees the server as up.
   110  	if err := harness.WaitUntilContainerServing(ctx, clientMachine, rubyApp, port); err != nil {
   111  		b.Fatalf("failed to wait until  serving: %v", err)
   112  	}
   113  	heyCmd := hey.MakeCmd("ruby", port)
   114  
   115  	// the client should run on Native.
   116  	b.ResetTimer()
   117  	client := clientMachine.GetNativeContainer(ctx, b)
   118  	defer client.CleanUp(ctx)
   119  	out, err := client.Run(ctx, dockerutil.RunOpts{
   120  		Image: "benchmarks/hey",
   121  		Links: []string{rubyApp.MakeLink("ruby")},
   122  	}, heyCmd...)
   123  	if err != nil {
   124  		b.Fatalf("hey container failed: %v logs: %s", err, out)
   125  	}
   126  	b.StopTimer()
   127  	hey.Report(b, out)
   128  }
   129  
   130  func TestMain(m *testing.M) {
   131  	harness.Init()
   132  	os.Exit(m.Run())
   133  }