| 1 |
/* |
| 2 |
Copyright 2022 The Kubernetes 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 tests |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"context" |
| 22 |
"io" |
| 23 |
"net/http" |
| 24 |
"net/http/httptest" |
| 25 |
"testing" |
| 26 |
) |
| 27 |
|
| 28 |
func BenchmarkLargeResponse_GRPC(b *testing.B) { |
| 29 |
b.StopTimer() |
| 30 |
|
| 31 |
expectCleanShutdown(b) |
| 32 |
|
| 33 |
ctx := context.Background() |
| 34 |
const length = 1 << 20 // 1M |
| 35 |
const chunks = 10 |
| 36 |
server := httptest.NewServer(newSizedServer(length, chunks)) |
| 37 |
defer server.Close() |
| 38 |
|
| 39 |
ps := runGRPCProxyServer(b) |
| 40 |
defer ps.Stop() |
| 41 |
|
| 42 |
a := runAgent(b, ps.AgentAddr()) |
| 43 |
waitForConnectedServerCount(b, 1, a) |
| 44 |
|
| 45 |
req, err := http.NewRequest("GET", server.URL, nil) |
| 46 |
if err != nil { |
| 47 |
b.Fatal(err) |
| 48 |
} |
| 49 |
req.Close = true |
| 50 |
|
| 51 |
for n := 0; n < b.N; n++ { |
| 52 |
// run test client |
| 53 |
tunnel, err := createSingleUseGrpcTunnel(ctx, ps.FrontAddr()) |
| 54 |
if err != nil { |
| 55 |
b.Fatal(err) |
| 56 |
} |
| 57 |
|
| 58 |
c := &http.Client{ |
| 59 |
Transport: &http.Transport{ |
| 60 |
DialContext: tunnel.DialContext, |
| 61 |
}, |
| 62 |
} |
| 63 |
|
| 64 |
r, err := c.Do(req) |
| 65 |
if err != nil { |
| 66 |
b.Fatal(err) |
| 67 |
} |
| 68 |
|
| 69 |
b.StartTimer() // BEGIN CRITICAL SECTION |
| 70 |
|
| 71 |
size, err := io.Copy(io.Discard, r.Body) |
| 72 |
if err != nil { |
| 73 |
b.Fatal(err) |
| 74 |
} |
| 75 |
r.Body.Close() |
| 76 |
|
| 77 |
b.StopTimer() // END CRITICAL SECTION |
| 78 |
|
| 79 |
if size != length*chunks { |
| 80 |
b.Fatalf("expect data length %d; got %d", length*chunks, size) |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func BenchmarkLargeRequest_GRPC(b *testing.B) { |
| 86 |
b.StopTimer() |
| 87 |
|
| 88 |
expectCleanShutdown(b) |
| 89 |
|
| 90 |
const length = 10 << 20 // 10M |
| 91 |
|
| 92 |
ctx := context.Background() |
| 93 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 94 |
size, err := io.Copy(io.Discard, req.Body) |
| 95 |
if err != nil { |
| 96 |
b.Fatal(err) |
| 97 |
} |
| 98 |
if size != length { |
| 99 |
b.Fatalf("Expected data length %d; got %d", length, size) |
| 100 |
} |
| 101 |
req.Body.Close() |
| 102 |
|
| 103 |
w.WriteHeader(http.StatusOK) |
| 104 |
})) |
| 105 |
defer server.Close() |
| 106 |
|
| 107 |
ps := runGRPCProxyServer(b) |
| 108 |
defer ps.Stop() |
| 109 |
|
| 110 |
a := runAgent(b, ps.AgentAddr()) |
| 111 |
waitForConnectedServerCount(b, 1, a) |
| 112 |
|
| 113 |
bodyBytes := make([]byte, length) |
| 114 |
body := bytes.NewReader(bodyBytes) |
| 115 |
req, err := http.NewRequest("POST", server.URL, body) |
| 116 |
if err != nil { |
| 117 |
b.Fatal(err) |
| 118 |
} |
| 119 |
req.Close = true |
| 120 |
for n := 0; n < b.N; n++ { |
| 121 |
// run test client |
| 122 |
tunnel, err := createSingleUseGrpcTunnel(ctx, ps.FrontAddr()) |
| 123 |
if err != nil { |
| 124 |
b.Fatal(err) |
| 125 |
} |
| 126 |
|
| 127 |
c := &http.Client{ |
| 128 |
Transport: &http.Transport{ |
| 129 |
DialContext: tunnel.DialContext, |
| 130 |
}, |
| 131 |
} |
| 132 |
body.Reset(bodyBytes) // We're reusing the request, so make sure to reset the body reader. |
| 133 |
|
| 134 |
b.StartTimer() // BEGIN CRITICAL SECTION |
| 135 |
|
| 136 |
if _, err := c.Do(req); err != nil { |
| 137 |
b.Fatal(err) |
| 138 |
} |
| 139 |
|
| 140 |
b.StopTimer() // END CRITICAL SECTION |
| 141 |
} |
| 142 |
} |