Skip to content

Commit 233ddb9

Browse files
Allow file payload in HTTP invoker.
Signed-off-by: Luka Simić <git@kocka.tech>
1 parent 48e9659 commit 233ddb9

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

pkg/config/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type LoaderConfiguration struct {
6262
RpsMemoryMB int `json:"RpsMemoryMB"`
6363
RpsIterationMultiplier int `json:"RpsIterationMultiplier"`
6464
RpsDataSizeMB float64 `json:"RpsDataSizeMB"`
65+
RpsFile string `json:"RpsFile"`
6566

6667
TracePath string `json:"TracePath"`
6768
Granularity string `json:"Granularity"`

pkg/driver/clients/http_client.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"crypto/rand"
66
"encoding/json"
77
"io"
8+
"mime/multipart"
89
"net/http"
10+
"os"
911
"strconv"
1012
"strings"
1113
"time"
@@ -36,11 +38,11 @@ func newHTTPInvoker(cfg *config.LoaderConfiguration) *httpInvoker {
3638
}
3739

3840
var payload []byte = nil
41+
var contentType string = "application/octet-stream"
3942

40-
func CreateRequestPayload(sizeInMB float64) *bytes.Buffer {
41-
byteCount := int(sizeInMB * 1024.0 * 1024.0) // MB -> B
42-
43+
func CreateRandomPayload(sizeInMB float64) *bytes.Buffer {
4344
if payload == nil {
45+
byteCount := int(sizeInMB * 1024.0 * 1024.0) // MB -> B
4446
payload = make([]byte, byteCount)
4547

4648
n, err := rand.Read(payload)
@@ -52,6 +54,35 @@ func CreateRequestPayload(sizeInMB float64) *bytes.Buffer {
5254
return bytes.NewBuffer(payload)
5355
}
5456

57+
func CreateFilePayload(filePath string) *bytes.Buffer {
58+
if payload == nil {
59+
file, err := os.Open(filePath)
60+
if err != nil {
61+
log.Fatalf("Failed to open file %s: %v", filePath, err)
62+
}
63+
64+
buffer := &bytes.Buffer{}
65+
writer := multipart.NewWriter(buffer)
66+
part, err := writer.CreateFormFile("images", "invitro.payload")
67+
if err != nil {
68+
log.Fatalf("Failed to create form file: %v", err)
69+
}
70+
71+
if _, err = io.Copy(part, file); err != nil {
72+
log.Fatalf("Failed to enter file into the form: %v", err)
73+
}
74+
if err = writer.Close(); err != nil {
75+
log.Fatalf("Failed to close writer: %v", err)
76+
}
77+
78+
payload = buffer.Bytes()
79+
contentType = writer.FormDataContentType()
80+
return buffer
81+
}
82+
83+
return bytes.NewBuffer(payload)
84+
}
85+
5586
func (i *httpInvoker) Invoke(function *common.Function, runtimeSpec *common.RuntimeSpecification) (bool, *mc.ExecutionRecord) {
5687
isDandelion := strings.Contains(strings.ToLower(i.cfg.Platform), "dandelion")
5788
isKnative := strings.Contains(strings.ToLower(i.cfg.Platform), "knative")
@@ -77,14 +108,20 @@ func (i *httpInvoker) Invoke(function *common.Function, runtimeSpec *common.Runt
77108
}
78109
if i.cfg.RpsTarget != 0 {
79110
ts := time.Now()
80-
requestBody = CreateRequestPayload(i.cfg.RpsDataSizeMB)
81-
log.Debugf("Took %v to generate request body.", time.Since(ts))
111+
if i.cfg.RpsFile != "" {
112+
requestBody = CreateFilePayload(i.cfg.RpsFile)
113+
log.Debugf("Took %v to create file body.", time.Since(ts))
114+
} else {
115+
requestBody = CreateRandomPayload(i.cfg.RpsDataSizeMB)
116+
log.Debugf("Took %v to generate request body.", time.Since(ts))
117+
}
82118
}
83119

84120
start := time.Now()
85121
record.StartTime = start.UnixMicro()
86122

87123
req, err := http.NewRequest("POST", "http://"+function.Endpoint, requestBody)
124+
req.Header.Add("Content-Type", contentType)
88125
if err != nil {
89126
log.Errorf("Failed to create a HTTP request - %v\n", err)
90127

0 commit comments

Comments
 (0)