-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstatus.go
54 lines (47 loc) · 1.05 KB
/
status.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
package gospider
import (
"fmt"
"sync/atomic"
"time"
)
type SpiderStatus struct {
TotalTask int64
FinishedTask int64
TotalItem int64
ExecSpeed int64
itemSpeed int64
}
func NewSpiderStatus() *SpiderStatus {
s := &SpiderStatus{}
lastFinish := int64(0)
lastItem := int64(0)
go func() {
for true {
s.ExecSpeed = (s.FinishedTask - lastFinish) / 5
s.itemSpeed = (s.TotalItem - lastItem) / 5
lastFinish = s.FinishedTask
lastItem = s.TotalItem
time.Sleep(5 * time.Second)
}
}()
return s
}
func (s *SpiderStatus) AddTask() {
atomic.AddInt64(&s.TotalTask, 1)
}
func (s *SpiderStatus) AddItem() {
atomic.AddInt64(&s.TotalTask, 1)
}
func (s *SpiderStatus) FinishTask() {
atomic.AddInt64(&s.FinishedTask, 1)
}
func (s *SpiderStatus) PrintSignalLine(name string) {
log.Println(
"["+name+"]",
s.TotalItem,
"items("+fmt.Sprint(s.itemSpeed)+"item/sec)",
s.FinishedTask,
"task finished("+fmt.Sprint(s.ExecSpeed)+"task/sec)",
s.TotalTask, "task total"+fmt.Sprint(int((float64(s.FinishedTask)/float64(s.TotalTask))*100))+"%",
)
}