2018-06-11 21:02:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/vault/builtin/logical/transit"
|
2022-05-05 17:30:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/benchhelpers"
|
2018-06-11 21:02:33 +00:00
|
|
|
"github.com/hashicorp/vault/helper/forwarding"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-06-11 21:02:33 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkHTTP_Forwarding_Stress(b *testing.B) {
|
|
|
|
testPlaintextB64 := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
|
|
|
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"transit": transit.Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-05 17:30:40 +00:00
|
|
|
cluster := vault.NewTestCluster(benchhelpers.TBtoT(b), coreConfig, &vault.TestClusterOptions{
|
2018-06-11 21:02:33 +00:00
|
|
|
HandlerFunc: Handler,
|
|
|
|
Logger: logging.NewVaultLoggerWithWriter(ioutil.Discard, log.Error),
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
cores := cluster.Cores
|
|
|
|
|
|
|
|
// make it easy to get access to the active
|
|
|
|
core := cores[0].Core
|
2022-05-05 17:30:40 +00:00
|
|
|
vault.TestWaitActive(benchhelpers.TBtoT(b), core)
|
2018-06-11 21:02:33 +00:00
|
|
|
|
|
|
|
handler := cores[0].Handler
|
|
|
|
host := fmt.Sprintf("https://127.0.0.1:%d/v1/transit/", cores[0].Listeners[0].Address.Port)
|
|
|
|
|
|
|
|
transport := &http.Transport{
|
|
|
|
TLSClientConfig: cores[0].TLSConfig,
|
|
|
|
}
|
|
|
|
if err := http2.ConfigureTransport(transport); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("https://127.0.0.1:%d/v1/sys/mounts/transit", cores[0].Listeners[0].Address.Port),
|
|
|
|
bytes.NewBuffer([]byte("{\"type\": \"transit\"}")))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
req.Header.Set(consts.AuthHeaderName, cluster.RootToken)
|
2018-06-11 21:02:33 +00:00
|
|
|
_, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var numOps uint32
|
|
|
|
|
|
|
|
doReq := func(b *testing.B, method, url string, body io.Reader) {
|
|
|
|
req, err := http.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
req.Header.Set(consts.AuthHeaderName, cluster.RootToken)
|
2018-06-11 21:02:33 +00:00
|
|
|
w := forwarding.NewRPCResponseWriter()
|
|
|
|
handler.ServeHTTP(w, req)
|
|
|
|
switch w.StatusCode() {
|
|
|
|
case 200:
|
|
|
|
case 204:
|
|
|
|
if !strings.Contains(url, "keys") {
|
|
|
|
b.Fatal("got 204")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
b.Fatalf("bad status code: %d, resp: %s", w.StatusCode(), w.Body().String())
|
|
|
|
}
|
2021-04-08 16:43:39 +00:00
|
|
|
// b.Log(w.Body().String())
|
2018-06-11 21:02:33 +00:00
|
|
|
numOps++
|
|
|
|
}
|
|
|
|
|
|
|
|
doReq(b, "POST", host+"keys/test1", bytes.NewBuffer([]byte("{}")))
|
|
|
|
keyUrl := host + "encrypt/test1"
|
|
|
|
reqBuf := []byte(fmt.Sprintf("{\"plaintext\": \"%s\"}", testPlaintextB64))
|
|
|
|
|
|
|
|
b.Run("doreq", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
doReq(b, "POST", keyUrl, bytes.NewReader(reqBuf))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Logf("total ops: %d", numOps)
|
|
|
|
}
|