Commit 67ee51a4 authored by Jean de Klerk's avatar Jean de Klerk
Browse files

storage: update benchmarking emulator with real URLs

- Allow URLs for Go, Node, Java.
- Add 1mb and 100mb files.
- Add --port flag.
- Add README.md.

Change-Id: If650bf88e705de002cac31a82393c7da104b355e
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/44013

Reviewed-by: default avatarEmmanuel Odeke <[email protected]>
Reviewed-by: default avatarkokoro <[email protected]>
parent b771179c
# benchserver
A simple Storage emulator for benchmarking purposes.
## Running
```
cd benchserver
go run *.go --port 8080
```
......@@ -16,22 +16,91 @@
package main
import (
"crypto/rand"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"google.golang.org/api/googleapi"
"google.golang.org/api/storage/v1"
raw "google.golang.org/api/storage/v1"
)
var (
port = flag.String("port", "", "specify a port to run on")
files = map[string][]byte{} // objectName -> contents
)
func init() {
oneMB, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 1000000))
if err != nil {
log.Fatal(err)
}
files["oneMB"] = oneMB
oneHundredMB, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 100000000))
if err != nil {
log.Fatal(err)
}
files["oneHundredMB"] = oneHundredMB
}
func main() {
// Serves the read from an object.
http.HandleFunc("/some-bucket-name/some-object-name", func(resp http.ResponseWriter, req *http.Request) {
})
// Serves the write to an object.
http.HandleFunc("/b/some-bucket-name/o/", func(resp http.ResponseWriter, req *http.Request) {
flag.Parse()
if *port == "" {
log.Fatalf("usage: %s --port=8080", os.Args[0])
}
// Read.
for objectName, contents := range files {
contents := contents
// Go: Download object.
http.HandleFunc("/some-bucket-name/"+objectName, func(resp http.ResponseWriter, req *http.Request) {
if _, err := resp.Write(contents); err != nil {
log.Fatal(err)
}
})
// Node: Download object.
http.HandleFunc("/b/some-bucket-name/o/"+objectName, func(resp http.ResponseWriter, req *http.Request) {
fmt.Println("emulator read <<NODE>> called")
if _, err := resp.Write(contents); err != nil {
log.Fatal(err)
}
})
// Java: Download metadata (pointing to download link for object).
http.HandleFunc("/storage/v1/b/some-bucket-name/o/"+objectName, func(resp http.ResponseWriter, req *http.Request) {
fmt.Println("emulator metadata read <<JAVA>> called")
resp.Header().Set("Content-Type", "application/json")
if _, err := fmt.Fprintf(resp, `{
"name": "%s-download",
"bucket": "some-bucket-name-download",
"generation": "1"
}`, objectName); err != nil {
log.Fatal(err)
}
})
// Java: Download object.
http.HandleFunc(fmt.Sprintf("/download/storage/v1/b/some-bucket-name-download/o/%s-download", objectName), func(resp http.ResponseWriter, req *http.Request) {
fmt.Println("emulator read <<JAVA>> called")
if _, err := resp.Write(contents); err != nil {
log.Fatal(err)
}
})
}
// Write.
http.HandleFunc("/b/some-bucket-name/o", func(resp http.ResponseWriter, req *http.Request) {
fmt.Println("emulator write called")
resp.Header().Set("Content-Type", "application/json")
ret := &storage.Object{
ret := &raw.Object{
ServerResponse: googleapi.ServerResponse{
Header: resp.Header(),
HTTPStatusCode: http.StatusCreated,
......@@ -41,6 +110,6 @@ func main() {
log.Fatal(err)
}
})
log.Println("listening on localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
log.Printf("listening on localhost:%s\n", *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", *port), nil))
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment