|
| 1 | +// Copyright 2024 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 | + |
| 15 | +// A simple `curl`-like HTTP client that prints metrics after the request. |
| 16 | +// All of its output is structured to be unambiguous even if stdout/stderr |
| 17 | +// is combined, as is the case for Kubernetes logs. |
| 18 | +// Useful for communicating with SGLang. |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "bufio" |
| 23 | + "bytes" |
| 24 | + "encoding/base64" |
| 25 | + "encoding/json" |
| 26 | + "flag" |
| 27 | + "fmt" |
| 28 | + "net/http" |
| 29 | + "os" |
| 30 | + "sort" |
| 31 | + "strings" |
| 32 | + "time" |
| 33 | +) |
| 34 | + |
| 35 | +// LINT.IfChange |
| 36 | + |
| 37 | +// Flags. |
| 38 | +var ( |
| 39 | + url = flag.String("url", "", "HTTP request URL.") |
| 40 | + method = flag.String("method", "GET", "HTTP request method (GET or POST).") |
| 41 | + postDataBase64 = flag.String("post_base64", "", "HTTP request POST data in base64 format; ignored for GET requests.") |
| 42 | + timeout = flag.Duration("timeout", 0, "HTTP request timeout; 0 for no timeout.") |
| 43 | +) |
| 44 | + |
| 45 | +// bufSize is the size of buffers used for HTTP requests and responses. |
| 46 | +const bufSize = 1024 * 1024 // 1MiB |
| 47 | + |
| 48 | +// fatalf crashes the program with a given error message. |
| 49 | +func fatalf(format string, values ...any) { |
| 50 | + fmt.Fprintf(os.Stderr, "FATAL: "+format+"\n", values...) |
| 51 | + os.Exit(1) |
| 52 | +} |
| 53 | + |
| 54 | +// Metrics contains the request metrics to export to JSON. |
| 55 | +// This is parsed by the sglang library at `test/gpu/sglang/sglang.go`. |
| 56 | +type Metrics struct { |
| 57 | + // ProgramStarted is the time when the program started. |
| 58 | + ProgramStarted time.Time `json:"program_started"` |
| 59 | + // RequestSent is the time when the HTTP request was sent. |
| 60 | + RequestSent time.Time `json:"request_sent"` |
| 61 | + // ResponseReceived is the time when the HTTP response headers were received. |
| 62 | + ResponseReceived time.Time `json:"response_received"` |
| 63 | + // FirstByteRead is the time when the first HTTP response body byte was read. |
| 64 | + FirstByteRead time.Time `json:"first_byte_read"` |
| 65 | + // LastByteRead is the time when the last HTTP response body byte was read. |
| 66 | + LastByteRead time.Time `json:"last_byte_read"` |
| 67 | +} |
| 68 | + |
| 69 | +func main() { |
| 70 | + var metrics Metrics |
| 71 | + metrics.ProgramStarted = time.Now() |
| 72 | + flag.Parse() |
| 73 | + if *url == "" { |
| 74 | + fatalf("--url is required") |
| 75 | + } |
| 76 | + client := http.Client{ |
| 77 | + Transport: &http.Transport{ |
| 78 | + MaxIdleConns: 1, |
| 79 | + IdleConnTimeout: *timeout, |
| 80 | + ReadBufferSize: bufSize, |
| 81 | + WriteBufferSize: bufSize, |
| 82 | + }, |
| 83 | + Timeout: *timeout, |
| 84 | + } |
| 85 | + var request *http.Request |
| 86 | + var err error |
| 87 | + switch *method { |
| 88 | + case "GET": |
| 89 | + request, err = http.NewRequest("GET", *url, nil) |
| 90 | + case "POST": |
| 91 | + postData, postDataErr := base64.StdEncoding.DecodeString(*postDataBase64) |
| 92 | + if postDataErr != nil { |
| 93 | + fatalf("cannot decode POST data: %v", postDataErr) |
| 94 | + } |
| 95 | + request, err = http.NewRequest("POST", *url, bytes.NewBuffer(postData)) |
| 96 | + default: |
| 97 | + err = fmt.Errorf("unknown method %q", *method) |
| 98 | + } |
| 99 | + if err != nil { |
| 100 | + fatalf("cannot create request: %v", err) |
| 101 | + } |
| 102 | + orderedReqHeaders := make([]string, 0, len(request.Header)) |
| 103 | + for k := range request.Header { |
| 104 | + orderedReqHeaders = append(orderedReqHeaders, k) |
| 105 | + } |
| 106 | + sort.Strings(orderedReqHeaders) |
| 107 | + for _, k := range orderedReqHeaders { |
| 108 | + for _, v := range request.Header[k] { |
| 109 | + fmt.Fprintf(os.Stderr, "REQHEADER: %s: %s\n", k, v) |
| 110 | + } |
| 111 | + } |
| 112 | + metrics.RequestSent = time.Now() |
| 113 | + resp, err := client.Do(request) |
| 114 | + metrics.ResponseReceived = time.Now() |
| 115 | + if err != nil { |
| 116 | + fatalf("cannot make request: %v", err) |
| 117 | + } |
| 118 | + gotFirstByte := false |
| 119 | + scanner := bufio.NewScanner(resp.Body) |
| 120 | + for scanner.Scan() { |
| 121 | + if !gotFirstByte { |
| 122 | + metrics.FirstByteRead = time.Now() |
| 123 | + gotFirstByte = true |
| 124 | + } |
| 125 | + if scanner.Text() == "" { |
| 126 | + continue |
| 127 | + } |
| 128 | + fmt.Printf("BODY: %q\n", strings.TrimPrefix(scanner.Text(), "data: ")) |
| 129 | + } |
| 130 | + // Check for any errors that may have occurred during scanning |
| 131 | + if err := scanner.Err(); err != nil { |
| 132 | + fatalf("error reading response body: %v", err) |
| 133 | + } |
| 134 | + metrics.LastByteRead = time.Now() |
| 135 | + if err := resp.Body.Close(); err != nil { |
| 136 | + fatalf("cannot close response body: %v", err) |
| 137 | + } |
| 138 | + orderedRespHeaders := make([]string, 0, len(resp.Header)) |
| 139 | + for k := range resp.Header { |
| 140 | + orderedRespHeaders = append(orderedRespHeaders, k) |
| 141 | + } |
| 142 | + sort.Strings(orderedRespHeaders) |
| 143 | + for _, k := range orderedRespHeaders { |
| 144 | + for _, v := range resp.Header[k] { |
| 145 | + fmt.Fprintf(os.Stderr, "RESPHEADER: %s: %s\n", k, v) |
| 146 | + } |
| 147 | + } |
| 148 | + metricsBytes, err := json.Marshal(&metrics) |
| 149 | + if err != nil { |
| 150 | + fatalf("cannot marshal metrics: %v", err) |
| 151 | + } |
| 152 | + fmt.Fprintf(os.Stderr, "STATS: %s\n", string(metricsBytes)) |
| 153 | +} |
| 154 | + |
| 155 | +// LINT.ThenChange(../../ollama/client/client.go) |
0 commit comments