Last active
July 5, 2018 02:34
-
-
Save arran4/70dca40573d7da9ae9c827f88cb316fb to your computer and use it in GitHub Desktop.
Simple speed test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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