forked from tidwall/redbench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecTest.go
80 lines (71 loc) · 1.87 KB
/
ecTest.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
package main
import (
"flag"
"fmt"
"github.com/klauspost/reedsolomon"
"log"
"time"
)
func main() {
sz := flag.Int("sz", 128, "object data size")
d := flag.Int("d", 4, "number of data shards for RS erasure coding")
p := flag.Int("p", 2, "number of parity shards for RS erasure coding")
g := flag.Int("g", 32, "max number of goroutines for RS erasure coding")
flag.Parse()
log.Println("chunk sz:", *sz, "# data shards:", *d, "# parity shards:", *p, "# max goroutines:", *g)
enc, err := reedsolomon.New(*d, *p, reedsolomon.WithMaxGoroutines(*g))
arrRange := *d + *p
data := make([][]byte, arrRange)
// Create all shards, size them at 50000 each
for i := 0; i < arrRange; i++ {
data[i] = make([]byte, *sz)
}
// Fill some data into the data shards
for i, in := range data[:*d] {
for j := range in {
in[j] = byte((i + j) & 0xff)
}
}
// Encoding phase
err = enc.Encode(data)
if err != nil {
fmt.Println("encoding encode err", err)
return
}
ok, err := enc.Verify(data)
if err != nil {
fmt.Println("encoding verify failed", err)
return
}
data[0] = nil
data[1] = nil
//data[2] = nil
//data[2][0] = 54321
// Decoding phase
t0 := time.Now()
ok, err = enc.Verify(data)
if ok {
fmt.Println("No reconstruction needed")
} else {
log.Println("First Verify() takes:", time.Since(t0))
fmt.Println("Verification failed. Reconstructing data")
t1 := time.Now()
err = enc.Reconstruct(data)
if err != nil {
fmt.Println("Reconstruct failed -", err)
}
log.Println("Reconstruct() takes:", time.Since(t1))
t2 := time.Now()
ok, err = enc.Verify(data)
if !ok {
fmt.Println("Verification failed after reconstruction, data likely corrupted.")
}
if err != nil {
fmt.Println(err)
}
log.Println("Verify() after Reconstruct() takes:", time.Since(t2))
log.Println("The whole decoding phase takes:", time.Since(t1))
}
//fmt.Println(ok)
//}
}