-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmachine.go
137 lines (120 loc) · 2.93 KB
/
machine.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 brutemachine
import (
"runtime"
"sync"
"sync/atomic"
"time"
)
// This structure contains some runtime statistics.
type Statistics struct {
// Time the execution started
Start time.Time
// Time the execution finished
Stop time.Time
// Total duration of the execution
Total time.Duration
// Total number of inputs from the wordlist
Inputs uint64
// Executions per second
Eps float64
// Total number of executions
Execs uint64
// Total number of executions with positive results.
Results uint64
}
// This is where the main logic goes.
type RunHandler func(line string) interface{}
// This is where positive results are handled.
type ResultHandler func(result interface{})
// The main object.
type Machine struct {
// Runtime statistics.
Stats Statistics
// Number of input consumers.
consumers uint
// Dictionary file name.
filename string
// Positive results channel.
output chan interface{}
// Inputs channel.
input chan string
// WaitGroup to stop while the machine is running.
wait sync.WaitGroup
// Main logic handler.
run_handler RunHandler
// Positive results handler.
res_handler ResultHandler
}
// Builds a new machine object, if consumers is less or equal than 0, CPU*2 will be used as default value.
func New( consumers int, filename string, run_handler RunHandler, res_handler ResultHandler) *Machine {
workers := uint(0)
if consumers <= 0 {
workers = uint(runtime.NumCPU() * 2)
} else {
workers = uint(consumers)
}
return &Machine{
Stats: Statistics{},
consumers: workers,
filename: filename,
output: make(chan interface{}),
input: make(chan string),
wait: sync.WaitGroup{},
run_handler: run_handler,
res_handler: res_handler,
}
}
func (m *Machine) inputConsumer() {
for in := range m.input {
atomic.AddUint64(&m.Stats.Execs, 1)
res := m.run_handler(in)
if res != nil {
atomic.AddUint64(&m.Stats.Results, 1)
m.output <- res
}
m.wait.Done()
}
}
func (m *Machine) outputConsumer() {
for res := range m.output {
m.res_handler(res)
}
}
// Start the machine.
func (m *Machine) Start() error {
// start a fixed amount of consumers for inputs
for i := uint(0); i < m.consumers; i++ {
go m.inputConsumer()
}
// start the output consumer on a goroutine
go m.outputConsumer()
m.Stats.Start = time.Now()
// count the inputs we have
lines, err := LineReader(m.filename, 0)
if err != nil {
return err
}
for _ = range lines {
m.Stats.Inputs++
}
lines, err = LineReader(m.filename, 0)
if err != nil {
return err
}
for line := range lines {
m.wait.Add(1)
m.input <- line
}
return nil
}
func (m *Machine) UpdateStats() {
m.Stats.Stop = time.Now()
m.Stats.Total = m.Stats.Stop.Sub(m.Stats.Start)
m.Stats.Eps = float64(m.Stats.Execs) / m.Stats.Total.Seconds()
}
// Wait for all jobs to be completed.
func (m *Machine) Wait() {
// wait for everything to be completed
m.wait.Wait()
m.UpdateStats()
}