Skip to content

Instantly share code, notes, and snippets.

@arran4
Last active July 5, 2018 02:34
Show Gist options
  • Save arran4/70dca40573d7da9ae9c827f88cb316fb to your computer and use it in GitHub Desktop.
Save arran4/70dca40573d7da9ae9c827f88cb316fb to your computer and use it in GitHub Desktop.
Simple speed test
package main
import (
"net/http"
"math/rand"
"time"
"log"
"io"
"bytes"
)
func main() {
http.HandleFunc("/", handleRequest)
http.HandleFunc("/favicon.ico", handleFavReq)
log.SetFlags(log.Flags() | log.Lshortfile)
log.Printf("http://localhost:8083/")
err := http.ListenAndServe(":8083", nil)
log.Printf("Error: %v", err)
}
func handleFavReq(writer http.ResponseWriter, request *http.Request) {
}
func handleRequest(writer http.ResponseWriter, request *http.Request) {
writer.Header().Add("Content-type", "application/octet-stream")
writer.WriteHeader(200)
log.Printf("Got request")
b := make([]byte, 1014 * 1024 * 1024 * 1)
rand.Seed(int64(time.Now().Nanosecond()))
n, err := rand.Read(b)
if err != nil {
log.Printf("Error: %v", err)
}
log.Printf("Generated random: %d bytes. Serving", n)
t := time.Now()
n64, err := io.Copy(writer, bytes.NewReader(b))
if err != nil {
log.Printf("Error: %v", err)
}
t2 := time.Now()
d := t2.Sub(t)
log.Printf("Served %d, %d %s", n64, d, d.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment