forked from deankarn/go-download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
72 lines (51 loc) · 1.19 KB
/
benchmarks_test.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package download
import (
"io"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
)
func BenchmarkDownload(b *testing.B) {
mux := http.NewServeMux()
mux.HandleFunc("/testdata/", func(w http.ResponseWriter, r *http.Request) {
fi, _ := os.Stat(data)
w.Header().Add("Content-Length", strconv.FormatInt(fi.Size(), 10))
w.Header().Add("Last-Modified", fi.ModTime().UTC().String())
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
if r.Method == http.MethodHead {
return
}
f, _ := os.Open(data)
defer f.Close()
io.Copy(w, f)
})
server := httptest.NewServer(mux)
defer server.Close()
url := server.URL + "/testdata/data.txt"
b.ResetTimer()
for n := 0; n < b.N; n++ {
f, err := Open(url, nil)
if err != nil {
b.Fatal(err)
}
f.Close()
}
}
func BenchmarkDownloadWithRange(b *testing.B) {
fs := http.StripPrefix("/testdata/", http.FileServer(http.Dir("./testdata")))
mux := http.NewServeMux()
mux.Handle("/testdata/", fs)
server := httptest.NewServer(mux)
defer server.Close()
url := server.URL + "/testdata/data.txt"
b.ResetTimer()
for n := 0; n < b.N; n++ {
f, err := Open(url, nil)
if err != nil {
b.Fatal(err)
}
f.Close()
}
}