我有一个执行乘法的下面的代码。我已经计算了执行相同操作所需的时间并将其记录到文件中。时间平均为3.0002毫秒。
func Mul() {
t := time.Now()
for i := 0; i < 10; i++ {
c := i * 2
fmt.Println(c)
}
l, err := logutil.GetLogger("replicateIT.main")
if err != nil {
log.Println("Error in getting current method" + err.Error())
}
sub := time.Since(t)
fmt.Println("The time difference", sub.String())
l.Log(logutil.INFO, "time difference "+sub.String())
}
然后我在svc包的execute方法中包含了函数调用来运行它作为服务,如下所示。现在时差为294.0168ms。
为什么这么多花时间?我在实施服务时做错了吗?
请提出一些建议!
func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
changes <- svc.Status{State: svc.StartPending}
fasttick := time.Tick(500 * time.Minute)
slowtick := time.Tick(10 * time.Minute)
tick := fasttick
go Mul()
//creates log instance
l, err := logutil.GetLogger("replicateIT.main")
if err != nil {
log.Println("Error in getting current method" + err.Error())
}
l.Log(logutil.INFO, "-----------------STARTED OUR APP------------------------")
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
case <-tick:
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
// Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4
//time.Sleep(100 * time.Millisecond)
//changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
break loop
case svc.Pause:
changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted}
tick = slowtick
case svc.Continue:
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
tick = fasttick
default:
elog.Error(1, fmt.Sprintf("unexpected control request #%d", c))
}
}
}
changes <- svc.Status{State: svc.Running}
return
}
为Mul Function和调用Mul的startService函数添加了基准。
package testing
// from fib_test.go
import (
"strconv"
"fmt"
"service"
"testing"
"example")
func BenchmarkMul10(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
example.Mul()
}
}
var i int
func BenchmarkStartSer(b *testing.B) {
fmt.Println("START SERVICE")
// run the Fib function b.N times
for n := 0; n <b.N; n++ {
i++
err:=service.InstallService("PTS-EBDR-MULT"+strconv.Itoa(i),"PTS-EBDR-MULT"+strconv.Itoa(i))
if err!=nil{
fmt.Println(err.Error())
break
}
}
}