forked from chmduquesne/rollinghash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (125 loc) · 2.55 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"flag"
"fmt"
"hash"
"io"
"log"
"os"
"runtime/pprof"
"time"
"code.cloudfoundry.org/bytefmt"
//rollsum "github.com/chmduquesne/rollinghash/adler32"
//rollsum "github.com/chmduquesne/rollinghash/buzhash32"
rollsum "github.com/chmduquesne/rollinghash/buzhash64"
//rollsum "github.com/chmduquesne/rollinghash/bozo32"
)
const (
KiB = 1024
MiB = 1024 * KiB
GiB = 1024 * MiB
clearscreen = "\033[2J\033[1;1H"
clearline = "\x1b[2K"
)
func genMasks() (res []uint64) {
res = make([]uint64, 64)
ones := ^uint64(0) // 0xffffffffffffffff
for i := 0; i < 64; i++ {
res[i] = ones >> uint(63-i)
}
return
}
// Gets the hash sum as a uint64
func sum64(h hash.Hash) (res uint64) {
buf := make([]byte, 0, 8)
s := h.Sum(buf)
for _, b := range s {
res <<= 8
res |= uint64(b)
}
return
}
func main() {
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
dostats := flag.Bool("stats", false, "Do some stats about the rolling sum")
size := flag.String("size", "256M", "How much data to read")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
fileSize, err := bytefmt.ToBytes(*size)
if err != nil {
log.Fatal(err)
}
bufsize := 16 * MiB
buf := make([]byte, bufsize)
t := time.Now()
f, err := os.Open("/dev/urandom")
if err != nil {
log.Fatal(err)
}
defer func() {
if err := f.Close(); err != nil {
log.Fatal(err)
}
}()
io.ReadFull(f, buf)
roll := rollsum.New()
roll.Write(buf[:64])
masks := genMasks()
hits := make(map[uint64]uint64)
for _, m := range masks {
hits[m] = 0
}
n := uint64(0)
k := 0
for n < fileSize {
if k >= bufsize {
status := fmt.Sprintf("Byte count: %s", bytefmt.ByteSize(n))
if *dostats {
fmt.Printf(clearscreen)
fmt.Println(status)
for i, m := range masks {
frequency := "NaN"
if hits[m] != 0 {
frequency = bytefmt.ByteSize(n / hits[m])
}
fmt.Printf("0x%016x (%02d bits): every %s\n", m, i+1, frequency)
}
} else {
fmt.Printf(clearline)
fmt.Printf(status)
fmt.Printf("\r")
}
_, err := io.ReadFull(f, buf)
if err != nil {
panic(err)
}
k = 0
}
roll.Roll(buf[k])
if *dostats {
s := sum64(roll)
for _, m := range masks {
if s&m == m {
hits[m] += 1
} else {
break
}
}
}
k++
n++
}
duration := time.Since(t)
fmt.Printf("Rolled %s of data in %v (%s/s).\n",
bytefmt.ByteSize(n),
duration,
bytefmt.ByteSize(n*1e9/uint64(duration)),
)
}