From e9d19bcf72cad98287575bb918d5c0c9cd4d8311 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 31 Jan 2012 21:29:14 +0200 Subject: [PATCH] better cpu detection --- README | 9 ++++- cmd/inquirer/inquirer.go | 17 +++++----- timeslots/timeslots.go | 66 ++++++++++++++++++++++++++++--------- timeslots/timeslots_test.go | 16 ++++++++- 4 files changed, 83 insertions(+), 25 deletions(-) diff --git a/README b/README index 4d5f50775..c19c7e388 100644 --- a/README +++ b/README @@ -1 +1,8 @@ -Rating system for telecom providers. \ No newline at end of file +Rating system for telecom providers. + +Match datetime objects instead of timestamp. Example +Year \d{4} #any year +Month \d{2} #any month +Day \d{2} # any day +Hour (0[89]|1\d) #from 08 till 19 +Minute \d{2} diff --git a/cmd/inquirer/inquirer.go b/cmd/inquirer/inquirer.go index 0a69804cd..e04a4b913 100644 --- a/cmd/inquirer/inquirer.go +++ b/cmd/inquirer/inquirer.go @@ -13,15 +13,15 @@ import ( "sync" ) -const NCPU = 4 var ( + nCPU = runtime.NumCPU() raterList *RaterList - inChannels [NCPU]chan string - outChannels [NCPU]chan string + inChannels []chan string + outChannels []chan string multiplexerIndex int mu sync.Mutex - sem = make(chan int, NCPU) + sem = make(chan int, nCPU) ) @@ -43,9 +43,10 @@ Creates a gorutine for every cpu core and the multiplexses the calls to each of */ func initThreadedCallRater(){ multiplexerIndex = 0 - runtime.GOMAXPROCS(NCPU) - fmt.Println(runtime.GOMAXPROCS(NCPU)) - for i:= 0; i< NCPU; i++ { + runtime.GOMAXPROCS(nCPU) + inChannels = make([]chan string, nCPU) + outChannels = make([]chan string, nCPU) + for i:= 0; i< nCPU; i++ { inChannels[i] = make(chan string) outChannels[i] = make(chan string) go func(in, out chan string){ @@ -62,7 +63,7 @@ func initThreadedCallRater(){ func ThreadedCallRater(key string) (replay string) { mu.Lock() defer mu.Unlock() - if multiplexerIndex >= NCPU { + if multiplexerIndex >= nCPU { multiplexerIndex = 0 } inChannels[multiplexerIndex] <- key diff --git a/timeslots/timeslots.go b/timeslots/timeslots.go index b7175a0a2..ad7652374 100644 --- a/timeslots/timeslots.go +++ b/timeslots/timeslots.go @@ -4,31 +4,61 @@ import ( "time" ) -type BilingUnit int - type RatingProfile struct { - StartTime time.Time - ConnectFee float32 - Price float32 - BillingUnit BilingUnit + StartTime time.Duration + ConnectFee, Price, BillingUnit float32 } type ActivationPeriod struct { ActivationTime time.Time - RatingProfiles []RatingProfile + RatingProfiles []*RatingProfile +} + +func (cd *ActivationPeriod) SplitInTimeSpans(t []*ActivationPeriods) (timespans []*TimeSpans) { + for i, ap := range aps { + t := &TimeSpan{TimeStart: cd.TimeStart, TimeEnd: cdTimeEnd} + timespans = append(timespans, aps.SplitInTimeSlots(t)) + } + return +} + +func (c *ActivationPeriod) AddRatingProfile(rp ...*RatingProfile) { + for _, r := range rp { + c.RatingProfiles = append(c.RatingProfiles, r) + } } type Customer struct { - Id string - Prefix string - ActivationPeriods []ActivationPeriod + CstmId string + DestinationPrefix string + ActivationPeriods []*ActivationPeriod +} + +func (c *Customer) AddActivationPeriod(ap ...*ActivationPeriod) { + for _,a := range ap { + c.ActivationPeriods = append(c.ActivationPeriods, a) + } +} + +func (c *Customer) SplitInTimeSpans(cd *CallDescription) (timespans []*TimeSpans) { + t := &TimeSpan{TimeStart: cd.TimeStart, TimeEnd: cdTimeEnd} + for i, ap := range c.ActivationPeriods { + timespans = append(timespans, aps.SplitInTimeSlots(t)) + } + return +} + +func (c *Customer) CleanOldActivationPeriods() { + now := time.Now() + obsoleteIndex := -1 + for i, ap := range c.ActivationPeriods { + if i > len(c.ActivationPeriods) - 2 { + break + } + if a + } } -const ( - SECONDS =iota - COUNT - BYTES -) type CallDescription struct { TOR int @@ -36,6 +66,12 @@ type CallDescription struct { TimeStart, TimeEnd time.Time } +type TimeSpan struct { + TimeStart, TimeEnd time.Time + RatingProfile *RatingProfile +} + + type CallCost struct { TOR int CstmId, Subject, Prefix string diff --git a/timeslots/timeslots_test.go b/timeslots/timeslots_test.go index 03182aea8..4f71566de 100644 --- a/timeslots/timeslots_test.go +++ b/timeslots/timeslots_test.go @@ -1,10 +1,24 @@ package timeslots import ( - "testing" + "time" + "testing" ) +func setUp() { + c1 := &Customer{CstmId:"rif",DestinationPrefix: "40256"} + t1 := time.Date(2012, time.January, 10, 23, 0, 0, 0, time.UTC) + ap1 := &ActivationPeriod{ActivationTime: t1} + c1.AddActivationPeriod(ap1) + d1,_ := time.ParseDuration("1m") + d2,_ := time.ParseDuration("2m") + r1 := &RatingProfile{StartTime: d1, ConnectFee: 1.1, Price: 0.1, BillingUnit: SECOND} + r2 := &RatingProfile{StartTime: d2, ConnectFee: 2.2, Price: 0.2, BillingUnit: SECOND} + ap1.AddRatingProfile(r1, r2) +} + func TestSimple(t *testing.T){ + setUp() cc, err := GetCost(nil, nil) if err != nil { t.Error("Got error on getting cost")