-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.go
More file actions
96 lines (81 loc) · 1.75 KB
/
Copy pathprofile.go
File metadata and controls
96 lines (81 loc) · 1.75 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
// Implement profiling
var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile = flag.String("memprofile", "", "write memory profile to `file`")
)
func CpuProfile() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
// ----
if len(flag.Args()) == 0 {
log.Error("No files to process")
return
}
result := make(map[string]int)
start := time.Now()
for _, fn := range flag.Args() {
processFile(result, fn)
}
defer fmt.Printf("Processing took: %v\n", time.Since(start))
printResult(result)
// ---- My code ----
}
func MemProfile() {
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}
func processFile(result map[string]int, fn string) {
var w string
r, err := os.Open(fn)
if nil != err {
log.Warn(err)
return
}
defer r.Close()
sc := bufio.NewScanner(r)
sc.Split(bufio.ScanWords)
for sc.Scan() {
w = strings.ToLower(sc.Text())
result[w] = result[w] + 1
}
}
func printResult(result map[string]int) {
fmt.Printf("%-10s%s\n", "Count", "Word")
fmt.Printf("%-10s%s\n", "-----", "----")
for w, c := range result {
fmt.Printf("%-10v%s\n", c, w)
}
}
func Suma(a, b int) int {
return a + b
}