/
main.go
62 lines (53 loc) · 1.5 KB
/
main.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
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"performance-and-scalability-of-go-applications/05-scheduler/wordlinecount/solution/wlc"
"runtime/trace"
)
// URL of the txt file to analyze
const url string = "https://www.gutenberg.org/files/16/16-0.txt"
// number of files to analyze
const nReaders int = 8
func main() {
word := flag.String("word", "", "word to search")
goroutines := flag.Int("goroutines", 0, "number of goroutines (0 for sequential)")
traceFile := flag.String("trace", "", "trace file name")
flag.Parse()
// generate a trace file if requested
if *traceFile != "" {
f, err := os.Create(*traceFile)
if err != nil {
log.Fatalf("could not create trace output file %q: %v", *traceFile, err)
}
if err := trace.Start(f); err != nil {
log.Fatalf("could not start trace: %v", err)
}
defer trace.Stop()
}
// in order to keep it simple, we read nReaders time from the same URL
readers := make([]io.Reader, nReaders)
for i := range readers {
// Do the GET request
resp, err := http.Get(url)
if err != nil {
log.Fatalf("could not perform GET request to url %s", url)
}
defer resp.Body.Close()
// save the http response body as reader interface
readers[i] = resp.Body
}
// calculate total number of lines in which word appear,
// using the requested number of goroutines
var count uint64
if *goroutines == 0 {
count = wlc.WordLineCountSeq(readers, *word)
} else {
count = wlc.WordLineCountConc(readers, *word, *goroutines)
}
fmt.Println(count)
}