bench: add http benchmark

LGTM=adg, bradfitz
R=adg, bradfitz
CC=golang-codereviews
https://golang.org/cl/61040044
diff --git a/bench/bench.go b/bench/bench.go
index b93af35..123e9d2 100644
--- a/bench/bench.go
+++ b/bench/bench.go
@@ -8,6 +8,7 @@
 	"code.google.com/p/go.benchmarks/driver"
 
 	_ "code.google.com/p/go.benchmarks/build"
+	_ "code.google.com/p/go.benchmarks/http"
 	_ "code.google.com/p/go.benchmarks/json"
 )
 
diff --git a/http/http.go b/http/http.go
new file mode 100644
index 0000000..810301c
--- /dev/null
+++ b/http/http.go
@@ -0,0 +1,105 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package http
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"net"
+	"net/http"
+	"os"
+	"runtime"
+	"time"
+
+	"code.google.com/p/go.benchmarks/driver"
+)
+
+func init() {
+	driver.Register("http", benchmark)
+}
+
+func benchmark() driver.Result {
+	return driver.Benchmark(benchmarkHTTPImpl)
+}
+
+const procs = 4
+
+func benchmarkHTTPImpl(N uint64) {
+	driver.Parallel(N, procs, func() {
+		t0 := time.Now()
+		makeOneRequest()
+		driver.LatencyNote(t0)
+	})
+}
+
+func makeOneRequest() bool {
+	res, err := client.Get(server.Addr)
+	if err != nil {
+		// Under heavy load with GOMAXPROCS>>1, it frequently fails
+		// with transient failures like:
+		// "dial tcp: cannot assign requested address"
+		// or:
+		// "ConnectEx tcp: Only one usage of each socket address
+		// (protocol/network address/port) is normally permitted".
+		// So we just log and continue,
+		// otherwise significant fraction of benchmarks will fail.
+		log.Printf("Get: %v", err)
+		return false
+	}
+	defer res.Body.Close()
+	b, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		log.Fatalf("ReadAll: %v", err)
+	}
+	if s := string(b); s != "Hello world.\n" {
+		log.Fatalf("Got body: " + s)
+	}
+	return true
+}
+
+var (
+	server *http.Server
+	client *http.Client
+)
+
+func init() {
+	// These environment variables affect net/http behavior,
+	// ensure that we get predictable results regardless of environment on the machine.
+	os.Setenv("HTTP_PROXY", "")
+	os.Setenv("http_proxy", "")
+	os.Setenv("NO_PROXY", "")
+	os.Setenv("no_proxy", "")
+
+	l, err := net.Listen("tcp", "127.0.0.1:0")
+	if err != nil {
+		if l, err = net.Listen("tcp6", "[::1]:0"); err != nil {
+			log.Fatalf("failed to listen: %v", err)
+		}
+	}
+	server = &http.Server{
+		Addr:           "http://" + l.Addr().String(),
+		ReadTimeout:    10 * time.Second,
+		WriteTimeout:   10 * time.Second,
+		MaxHeaderBytes: 1 << 20,
+		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+			fmt.Fprintf(w, "Hello world.\n")
+		}),
+	}
+	go server.Serve(l)
+
+	client = &http.Client{
+		Transport: &http.Transport{
+			// just what default client uses
+			Proxy: http.ProxyFromEnvironment,
+			// this leads to more stable numbers
+			MaxIdleConnsPerHost: procs * runtime.GOMAXPROCS(0),
+		},
+	}
+
+	if !makeOneRequest() {
+		log.Fatalf("server is not listening")
+	}
+}