I have a below code which performs multiplication .I have calculated the time it takes to do the same and logged it into a file .Time is always 3.0002ms on average .
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())
}
Then I have included the function call in execute method of svc package to run this as service ,as given below .The time difference now is 294.0168ms .
Why is it taking time this much ? Am i doing anything wrong in implementing service ?
Please give some suggestions !
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
}
Benchmark has been added for Mul Function and the startService function which invokes Mul .
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
}
}
}