/
main.go
122 lines (104 loc) · 2.53 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
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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
)
var uid = flag.String("uid", "", "uid of sigchain owner")
var username = flag.String("username", "", "username of sigchain owner")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func errout(msg string) {
fmt.Fprintf(os.Stderr, msg+"\n")
os.Exit(1)
}
func read() []byte {
var in io.Reader
switch flag.NArg() {
case 0:
fmt.Println("reading sigchain from stdin")
in = os.Stdin
case 1:
fmt.Printf("reading sigchain from %q\n", flag.Arg(0))
f, err := os.Open(flag.Arg(0))
if err != nil {
errout(err.Error())
}
defer f.Close()
in = f
default:
errout("provide 0 or 1 args")
}
all, err := ioutil.ReadAll(in)
if err != nil {
errout(err.Error())
}
fmt.Printf("%d bytes read\n", len(all))
return all
}
func memstats() {
runtime.GC()
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
fmt.Printf("Alloc: %d (%d KB) (%d MB)\n", ms.Alloc, ms.Alloc/1024, ms.Alloc/(1024*1024))
fmt.Printf("TotalAlloc: %d (%d KB) (%d MB)\n", ms.TotalAlloc, ms.TotalAlloc/1024, ms.TotalAlloc/(1024*1024))
}
func memprof() {
f, err := os.Create("/tmp/sc_memprof")
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()
fmt.Printf("wrote memory profile to /tmp/sc_memprof\n")
}
// don't GC me
var sc *libkb.SigChain
func main() {
fmt.Println("sigchain loader")
flag.Parse()
raw := read()
g := libkb.NewGlobalContext().Init()
g.Log = logger.New("sc")
g.ConfigureCaches()
iterations := 1
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
iterations = 10
}
m := libkb.NewMetaContextBackground(g)
for i := 0; i < iterations; i++ {
start := time.Now()
sc = &libkb.SigChain{Contextified: libkb.NewContextified(g)}
sc.SetUIDUsername(keybase1.UID(*uid), *username)
if _, err := sc.LoadServerBody(m, raw, 0, nil, ""); err != nil {
errout(err.Error())
}
if err := sc.VerifyChain(m); err != nil {
errout(err.Error())
}
if err := sc.Store(m); err != nil {
errout(err.Error())
}
elapsed := time.Since(start)
fmt.Printf("sig chain load time: %s\n", elapsed)
}
memstats()
memprof()
}