|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "flag" |
| 22 | + "fmt" |
| 23 | + "log" |
| 24 | + "os" |
| 25 | + "strconv" |
| 26 | + |
| 27 | + ttrpc "github.com/containerd/ttrpc" |
| 28 | + payload "github.com/containerd/ttrpc/cmd/ttrpc-stress/payload" |
| 29 | + |
| 30 | + "golang.org/x/sync/errgroup" |
| 31 | +) |
| 32 | + |
| 33 | +// main is the entry point of the stress utility. |
| 34 | +func main() { |
| 35 | + // Define a flag for displaying usage information. |
| 36 | + flagHelp := flag.Bool("help", false, "Display usage") |
| 37 | + flag.Parse() |
| 38 | + |
| 39 | + // Check if help flag is set or if there are insufficient arguments. |
| 40 | + if *flagHelp || flag.NArg() < 2 { |
| 41 | + usage() |
| 42 | + } |
| 43 | + |
| 44 | + // Switch based on the first argument to determine mode (server or client). |
| 45 | + switch flag.Arg(0) { |
| 46 | + case "server": |
| 47 | + // Ensure correct number of arguments for server mode. |
| 48 | + if flag.NArg() != 2 { |
| 49 | + usage() |
| 50 | + } |
| 51 | + |
| 52 | + addr := flag.Arg(1) |
| 53 | + |
| 54 | + // Run the server and handle any errors. |
| 55 | + err := runServer(context.Background(), addr) |
| 56 | + if err != nil { |
| 57 | + log.Fatalf("error: %s", err) |
| 58 | + } |
| 59 | + |
| 60 | + case "client": |
| 61 | + // Ensure correct number of arguments for client mode. |
| 62 | + if flag.NArg() != 4 { |
| 63 | + usage() |
| 64 | + } |
| 65 | + |
| 66 | + addr := flag.Arg(1) |
| 67 | + |
| 68 | + // Parse iterations and workers arguments. |
| 69 | + iters, err := strconv.Atoi(flag.Arg(2)) |
| 70 | + if err != nil { |
| 71 | + log.Fatalf("failed parsing iters: %s", err) |
| 72 | + } |
| 73 | + |
| 74 | + workers, err := strconv.Atoi(flag.Arg(3)) |
| 75 | + if err != nil { |
| 76 | + log.Fatalf("failed parsing workers: %s", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Run the client and handle any errors. |
| 80 | + err = runClient(context.Background(), addr, iters, workers) |
| 81 | + if err != nil { |
| 82 | + log.Fatalf("runtime error: %s", err) |
| 83 | + } |
| 84 | + |
| 85 | + default: |
| 86 | + // Display usage information if the mode is unrecognized. |
| 87 | + usage() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// usage prints the usage information and exits the program. |
| 92 | +// usage prints the usage information for the program and exits. |
| 93 | +func usage() { |
| 94 | + fmt.Fprintf(os.Stderr, `Usage: |
| 95 | + stress server <ADDR> |
| 96 | + Run the server with the specified unix socket or named pipe. |
| 97 | + stress client <ADDR> <ITERATIONS> <WORKERS> |
| 98 | + Run the client with the specified unix socket or named pipe, number of ITERATIONS, and number of WORKERS. |
| 99 | +`) |
| 100 | + os.Exit(1) |
| 101 | +} |
| 102 | + |
| 103 | +// runServer sets up and runs the server. |
| 104 | +func runServer(ctx context.Context, addr string) error { |
| 105 | + log.Printf("Starting server on %s", addr) |
| 106 | + |
| 107 | + // Listen for connections on the specified address. |
| 108 | + l, err := listenConnection(addr) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("failed listening on %s: %w", addr, err) |
| 111 | + } |
| 112 | + |
| 113 | + // Create a new ttrpc server. |
| 114 | + server, err := ttrpc.NewServer() |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed creating ttrpc server: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + // Register a service and method with the server. |
| 120 | + server.Register("ttrpc.stress.test.v1", map[string]ttrpc.Method{ |
| 121 | + "TEST": func(_ context.Context, unmarshal func(interface{}) error) (interface{}, error) { |
| 122 | + req := &payload.Payload{} |
| 123 | + // Unmarshal the request payload. |
| 124 | + if err := unmarshal(req); err != nil { |
| 125 | + log.Fatalf("failed unmarshalling request: %s", err) |
| 126 | + } |
| 127 | + id := req.Value |
| 128 | + log.Printf("got request: %d", id) |
| 129 | + // Return the same payload as the response. |
| 130 | + return &payload.Payload{Value: id}, nil |
| 131 | + }, |
| 132 | + }) |
| 133 | + |
| 134 | + // Serve the server and handle any errors. |
| 135 | + if err := server.Serve(ctx, l); err != nil { |
| 136 | + return fmt.Errorf("failed serving server: %w", err) |
| 137 | + } |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +// runClient sets up and runs the client. |
| 142 | +func runClient(ctx context.Context, addr string, iters int, workers int) error { |
| 143 | + log.Printf("Starting client on %s", addr) |
| 144 | + |
| 145 | + // Dial a connection to the specified pipe. |
| 146 | + c, err := dialConnection(addr) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("failed dialing connection to %s: %w", addr, err) |
| 149 | + } |
| 150 | + |
| 151 | + // Create a new ttrpc client. |
| 152 | + client := ttrpc.NewClient(c) |
| 153 | + ch := make(chan int) |
| 154 | + var eg errgroup.Group |
| 155 | + |
| 156 | + // Start worker goroutines to send requests. |
| 157 | + for i := 0; i < workers; i++ { |
| 158 | + eg.Go(func() error { |
| 159 | + for { |
| 160 | + i, ok := <-ch |
| 161 | + if !ok { |
| 162 | + return nil |
| 163 | + } |
| 164 | + // Send the request and handle any errors. |
| 165 | + if err := send(ctx, client, uint32(i)); err != nil { |
| 166 | + return fmt.Errorf("failed sending request: %w", err) |
| 167 | + } |
| 168 | + } |
| 169 | + }) |
| 170 | + } |
| 171 | + |
| 172 | + // Send iterations to the channel. |
| 173 | + for i := 0; i < iters; i++ { |
| 174 | + ch <- i |
| 175 | + } |
| 176 | + close(ch) |
| 177 | + |
| 178 | + // Wait for all goroutines to finish. |
| 179 | + if err := eg.Wait(); err != nil { |
| 180 | + return fmt.Errorf("failed waiting for goroutines: %w", err) |
| 181 | + } |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +// send sends a request to the server and verifies the response. |
| 186 | +func send(ctx context.Context, client *ttrpc.Client, id uint32) error { |
| 187 | + req := &payload.Payload{Value: id} |
| 188 | + resp := &payload.Payload{} |
| 189 | + |
| 190 | + log.Printf("sending request: %d", id) |
| 191 | + // Call the server method and handle any errors. |
| 192 | + if err := client.Call(ctx, "ttrpc.stress.test.v1", "TEST", req, resp); err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + |
| 196 | + ret := resp.Value |
| 197 | + log.Printf("got response: %d", ret) |
| 198 | + // Verify the response matches the request. |
| 199 | + if ret != id { |
| 200 | + return fmt.Errorf("expected return value %d but got %d", id, ret) |
| 201 | + } |
| 202 | + return nil |
| 203 | +} |
0 commit comments