Debian Codegraph @

examples/example.go

raw symbols SCIP
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7 "runtime"
8 "runtime/pprof"
9
10 "github.com/bettercap/recording"
11 )
12
13 var (
14 fileName = "/home/evilsocket/bettercap-session.record"
15 cpuProfile = ""
16 memProfile = ""
17 )
18
19 func init() {
20 flag.StringVar(&fileName, "file", fileName, "Record file name to load.")
21 flag.StringVar(&cpuProfile, "cpu-profile", cpuProfile, "If filled, it'll save a CPU profile on this file.")
22 flag.StringVar(&memProfile, "mem-profile", memProfile, "If filled, it'll save a memory profile on this file.")
23 }
24
25 func PrintMemUsage() {
26 var m runtime.MemStats
27 runtime.ReadMemStats(&m)
28 // For info on each, see: https://golang.org/pkg/runtime/#MemStats
29 fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
30 fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
31 fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
32 fmt.Printf("\tNumGC = %v\n", m.NumGC)
33 }
34
35 func bToMb(b uint64) uint64 {
36 return b / 1024 / 1024
37 }
38
39 func main() {
40 flag.Parse()
41
42 if cpuProfile != "" {
43 fmt.Printf("saving cpu profile to %s\n", cpuProfile)
44 f, err := os.Create(cpuProfile)
45 if err != nil {
46 panic(err)
47 }
48 pprof.StartCPUProfile(f)
49 defer pprof.StopCPUProfile()
50 }
51
52 // load a recording from a file and print progress while loading it
53 arch, err := recording.Load(fileName, func(perc float64, done int, total int) {
54 fmt.Printf("loaded %d/%d frames ( %.2f ) ...\n", done, total, perc)
55 })
56 if err != nil {
57 panic(err)
58 }
59
60 fmt.Printf("loaded session started at %s and stopped at %s (%s):\n\n", arch.Session.StartedAt(), arch.Session.StoppedAt(), arch.Session.Duration())
61
62 // keep iterating every session frame until they're over
63 // the same is doable with the `arch.Events` field
64 for arch.Session.Over() == false {
65 raw := arch.Session.Next()
66 // parse the raw JSON into a map or whatever to access its attributes
67 fmt.Printf("loaded %d bytes of frame\n", len(raw))
68 }
69
70 PrintMemUsage()
71
72 if memProfile != "" {
73 f, err := os.Create(memProfile)
74 if err != nil {
75 panic(err)
76 }
77 pprof.WriteHeapProfile(f)
78 f.Close()
79 }
80 }