forked from ardanlabs/gotraining
/
trace.go
161 lines (130 loc) · 3.09 KB
/
trace.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program that performs a series of I/O related tasks to
// better understand tracing in Go.
package main
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"os/signal"
"runtime/pprof"
"runtime/trace"
"time"
)
// Download does network I/O.
func Download() []byte {
// Download the tar file.
r, err := http.Get("https://ftp.gnu.org/gnu/binutils/binutils-2.7.tar.gz")
if err != nil {
log.Fatal(err)
}
// Read in the entire contents of the file.
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
defer r.Body.Close()
// Return the file.
return body
}
// Write does disk I/O.
func Write(data []byte) {
// Perform the disk I/O 50 times.
for i := 0; i < 50; i++ {
// Create a new file.
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name())
// Write the data to the file.
_, err = tmpfile.Write(data)
if err != nil {
log.Fatal(err)
}
// Close the file and flush the final writes.
tmpfile.Close()
}
}
// Block waits on a channel for one second.
func Block() {
<-time.NewTimer(time.Second).C
}
// Hash does CPU-intensive work.
func Hash(data []byte) {
for i := 0; i < 50; i++ {
sha256.Sum256(data)
}
}
// Exec runs an external command.
func Exec() {
if err := exec.Command("sleep", "1").Run(); err != nil {
log.Fatal(err)
}
}
// SlowHandler performs a set of actions that take long to complete.
func SlowHandler(w http.ResponseWriter, req *http.Request) {
fmt.Println("Started Request")
// Measure the time this takes.
start := time.Now()
// Download the file from the web.
data := Download()
// Write the file to disk.
Write(data)
// Block for a second.
Block()
// Hash the data we received.
Hash(data)
// Execute an out of process command.
Exec()
// Log how long all this took.
s := fmt.Sprint("Request took:", time.Since(start))
fmt.Println(s)
fmt.Fprintf(w, s)
}
func main() {
fmt.Println("Listening on 127.0.0.1:12345")
// Start listening on the local ip.
l, err := net.Listen("tcp", "127.0.0.1:12345")
if err != nil {
log.Fatal(err)
}
// Create a file to hold profiling data.
pf, err := os.Create("cpu.pprof")
if err != nil {
log.Fatal(err)
}
defer pf.Close()
// Start gathering the profiling data.
pprof.StartCPUProfile(pf)
defer pprof.StopCPUProfile()
// Create a file to hold tracing data.
tf, err := os.Create("trace.out")
if err != nil {
log.Fatal(err)
}
defer tf.Close()
// Start gathering the tracing data.
trace.Start(tf)
defer trace.Stop()
// Bind the handler to the work route.
http.HandleFunc("/work", SlowHandler)
// Set up the OS interrup to stop the service.
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
<-sigChan
l.Close()
}()
// This will server the website until we `/stop` the listener.
// This will allow us to write the file profile and trace data.
http.Serve(l, nil)
fmt.Println("Shutdown")
}