public
Created

Few things to benchmark

  • Download Gist
httpd_sample.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package main
 
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
 
var (
test_string = "This is just a test string."
)
 
func format_string(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, test_string)
}
 
func faux_cache() func(http.ResponseWriter, *http.Request) {
reader := strings.NewReader(test_string)
 
return func(w http.ResponseWriter, r *http.Request) {
io.Copy(w, reader)
}
}
 
func from_disk() func(http.ResponseWriter, *http.Request) {
fh, err := os.Create("temp_test_file")
if err != nil {
panic(err)
}
defer fh.Close()
 
fmt.Fprintf(fh, test_string)
 
return func(w http.ResponseWriter, r *http.Request) {
fh, err := os.Open("temp_test_file")
if err != nil {
panic(err)
}
defer fh.Close()
io.Copy(w, fh)
}
}
 
func main() {
http.HandleFunc("/1", format_string)
http.HandleFunc("/2", faux_cache())
http.HandleFunc("/3", from_disk())
http.ListenAndServe(":8080", nil)
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.