osrm-backend/traffic_updater/go/osrm_traffic_updater/dumper_statistic.go
Xun(Perry) Liu c613b1737c Feature/traffic updater statistic (#44)
* feat: implement statistic for osrm traffic dumper.
issues: https://github.com/Telenav/osrm-backend/issues/43

* feat: Implement statistic for loading traffic map.
issues: https://github.com/Telenav/osrm-backend/issues/43

* fix: Experiment to use channel to collect statistic information.
issues: https://github.com/Telenav/osrm-backend/issues/43
2019-07-19 09:32:33 +08:00

71 lines
1.8 KiB
Go

package main
import (
"fmt"
)
type dumperStatisticItems struct {
wayCnt uint64
nodeCnt uint64
fwdRecordCnt uint64
bwdRecordCnt uint64
wayMatchedCnt uint64
nodeMatchedCnt uint64
}
type dumperStatistic struct {
c chan dumperStatisticItems
sum dumperStatisticItems
init bool
close bool
}
func (d* dumperStatistic) Init(n int) {
d.c = make(chan dumperStatisticItems, n)
d.init = true
}
func (d* dumperStatistic) Close() {
if !d.init {
return
}
close(d.c)
for item := range d.c {
d.sum.wayCnt += item.wayCnt
d.sum.nodeCnt += item.nodeCnt
d.sum.fwdRecordCnt += item.fwdRecordCnt
d.sum.bwdRecordCnt += item.bwdRecordCnt
d.sum.wayMatchedCnt += item.wayMatchedCnt
d.sum.nodeMatchedCnt += item.nodeMatchedCnt
}
d.close = true
}
func (d* dumperStatistic) Sum() (dumperStatisticItems) {
return d.sum
}
func (d* dumperStatistic) Update(wayCnt uint64, nodeCnt uint64, fwdRecordCnt uint64,
bwdRecordCnt uint64, wayMatchedCnt uint64, nodeMatchedCnt uint64) {
if !d.init {
fmt.Printf("dumperStatistic->Update() failed, please call Init() first otherwise will block all functions. \n")
return
}
d.c <- (dumperStatisticItems{wayCnt, nodeCnt, fwdRecordCnt, bwdRecordCnt, wayMatchedCnt, nodeMatchedCnt})
}
func (d* dumperStatistic) Output() {
if !d.close {
fmt.Printf("Close() hasn't been called, no statistic collected.\n")
return
}
fmt.Printf("Statistic: \n")
fmt.Printf("Load %d way from data with %d nodes.\n", d.sum.wayCnt, d.sum.nodeCnt)
fmt.Printf("%d way with %d nodes matched with traffic record.\n",
d.sum.wayMatchedCnt, d.sum.nodeMatchedCnt)
fmt.Printf("Generate %d records in final result with %d of them from forward traffic and %d from backword.\n",
d.sum.fwdRecordCnt+ d.sum.bwdRecordCnt, d.sum.fwdRecordCnt, d.sum.bwdRecordCnt)
}