Skip to content

Instantly share code, notes, and snippets.

@mjard
Created November 26, 2012 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjard/4147299 to your computer and use it in GitHub Desktop.
Save mjard/4147299 to your computer and use it in GitHub Desktop.
Few things to benchmark
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment