-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (126 loc) · 3.22 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
package main
import (
"bufio"
"fmt"
"io"
"math/rand"
"os"
"strings"
"sync"
"time"
"github.com/spf13/cobra"
)
const (
workerFlagName = "workers"
fileFlagName = "file"
)
var dstCmd = &cobra.Command{
Use: "dst",
RunE: func(cmd *cobra.Command, args []string) error {
nWorkers, err := cmd.Flags().GetInt(workerFlagName)
if err != nil {
return err
}
return dstInternal(nWorkers, os.Stdout)
},
}
var entropyCmd = &cobra.Command{
Use: "entropy",
Long: "Calculates the entropy of the file lines. If all lines are the same, result is 100, if all are different result is 0",
RunE: func(cmd *cobra.Command, args []string) error {
fileName, err := cmd.Flags().GetString(fileFlagName)
if err != nil {
return err
}
f, err := os.Open(fileName)
if err != nil {
return err
}
defer f.Close()
s := bufio.NewScanner(f)
distinct := make(map[string]struct{})
lines := 0
for s.Scan() {
distinct[s.Text()] = struct{}{}
lines++
}
result := 100 - (float64(len(distinct))/float64(lines))*100
if len(distinct) == 1 {
result = 100
}
fmt.Printf("%d distinct executions out of %d executions: score: %0.2f%% \n", len(distinct), lines, result)
return nil
},
}
func init() {
dstCmd.Flags().Int(workerFlagName, 2, "Number of workers to spawn")
entropyCmd.PersistentFlags().String(fileFlagName, "", "File to calculate entropy for")
dstCmd.AddCommand(entropyCmd)
}
func main() {
// runtime.GOMAXPROCS(1) not necessary because our deterministic runtime
// runs on WASM, which is single-threaded.
if err := dstCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// dstInternal spawns a couple of goroutines that each do some work and
// communicate over a channel to the main goroutine that records these
// interactions. The order is written to an external file that can be analyzed
// so that distinct run orders can be compared.
func dstInternal(nWorkers int, w io.Writer) error {
orderChan := make(chan workerInfo, nWorkers)
var wg sync.WaitGroup
wg.Add(nWorkers)
// These selectChans are all closed and verify deterministic select case
// selection.
selectChans := make([]chan struct{}, 0)
for i := 0; i < 4; i++ {
selectChan := make(chan struct{})
close(selectChan)
selectChans = append(selectChans, selectChan)
}
for i := 0; i < nWorkers; i++ {
id := i
go func() {
defer wg.Done()
worker(id, orderChan, selectChans)
}()
}
wg.Wait()
close(orderChan)
order := make([]string, 0, nWorkers)
for info := range orderChan {
order = append(order, info.String())
}
if _, err := w.Write([]byte(fmt.Sprintf("%s\n", strings.Join(order, "-")))); err != nil {
return err
}
return nil
}
type workerInfo struct {
// id of the goroutine.
id int
// selectCase this worker selected.
selectCase int
}
func (i workerInfo) String() string {
return fmt.Sprintf("{%d,%d}", i.id, i.selectCase)
}
func worker(id int, orderChan chan<- workerInfo, selectChans []chan struct{}) {
info := workerInfo{id: id}
select {
case <-selectChans[0]:
info.selectCase = 0
case <-selectChans[1]:
info.selectCase = 1
case <-selectChans[2]:
info.selectCase = 2
case <-selectChans[3]:
info.selectCase = 3
}
mSleep := time.Duration(rand.Intn(10)) * time.Millisecond
time.Sleep(mSleep)
orderChan <- info
}