-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
143 lines (121 loc) · 3.16 KB
/
run.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
141
142
143
package main
import (
"fmt"
"math"
"os"
"sync"
"time"
"github.com/panjf2000/ants/v2"
logger "github.com/sirupsen/logrus"
)
var (
RUNNING = false
cleanOnce sync.Once
)
func run() {
// just show pcaps 的前提是有被选中的 job, 基于 job 的 finder 来展示 pcap 列表
if config.Pingback != "" {
err := startPingback(config.Pingback)
if err != nil {
logger.Errorln(fmt.Sprintf("error when fork as daemon: %s", err))
terminate()
}
// change logger's output to the given file
logFile, err := os.OpenFile(config.daemonLogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
logger.Errorln(fmt.Sprintf("error when open daemon log file: %s", err))
terminate()
}
logger.SetOutput(logFile)
logger.Infoln(fmt.Sprintf("redirect output to %s", config.daemonLogPath))
}
if len(selectedJobs) == 0 {
logger.Errorln("no job selected !!!")
return
}
/*
并发加载 finder 的 pcap 列表, 然后决定是否仅展示 Pcap 列表
*/
loadPcaps()
if config.JustShowJobs {
for _, job := range jobs {
logger.Infoln(fmt.Sprintf("%s id is %s, using finder %s, which has %d pcaps", job, job.Id, job.finder, len(job.finder.pcaps)))
}
exit(0)
}
if config.JustShowPcaps {
exit(0)
}
if config.AsDaemon && config.Pingback == "" { // parent
err := startDaemon()
if err != nil {
logger.Errorln(fmt.Sprintf("error when fork as daemon: %s", err))
terminate()
}
return
}
if config.Duration > 0 {
go timeoutChecker()
}
runJobs()
}
func runJobs() {
defer cleanup(0)
RUNNING = true
ConcurrencyJobs := int(math.Min(float64(config.ConcurrencyJobs), float64(config.TestTimes*(len(selectedJobs)))))
jobsGroup := sync.WaitGroup{}
pool, _ := ants.NewPoolWithFunc(ConcurrencyJobs, func(i interface{}) {
defer jobsGroup.Done()
realJob := i.(*RealJob)
defer realJob.pool.Release()
if !RUNNING {
return
}
runCommands(realJob)
})
defer pool.Release()
// 按照执行次数要求反复创建任务
for t := 0; t < config.TestTimes; t++ {
for _, job := range selectedJobs {
if RUNNING {
jobPool, _ := ants.NewPoolWithFunc(config.ConcurrencyCommands, func(i interface{}) {
realCommand := i.(*RealCommand)
defer realCommand.g.Done()
realCommand.run()
})
realJob := &RealJob{
round: t + 1,
job: job,
pool: jobPool,
}
jobsGroup.Add(1)
_ = pool.Invoke(realJob)
}
}
}
jobsGroup.Wait()
}
func timeoutChecker() {
logger.Infoln(fmt.Sprintf("run time checker start to control the run time under %s", config.Duration))
time.Sleep(config.Duration)
logger.Warnf("timeout, force exit")
terminate()
}
func cleanup(waiting int) {
cleanOnce.Do(func() {
running := RUNNING
RUNNING = false
if config.workingDirectory != "" {
if config.KeepData {
logger.Warnln(fmt.Sprintf("as reminder, your data under %s, please remember to remove it after use", config.workingDirectory))
return
}
if running && waiting != 0 {
logger.Warnln("waiting 3secs to cleanup")
time.Sleep(3 * time.Second)
}
logger.Infoln(fmt.Sprintf("removing temporary working directory: %s", config.workingDirectory))
_ = os.RemoveAll(config.workingDirectory)
}
})
}