From 7f036b7e5ae30384567128e6a303602c6b79bbd4 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 3 Feb 2012 16:37:41 +0200 Subject: [PATCH] redis up to speed --- cmd/loader/redis/redis_loader.go | 36 +++++++++++++++++++++++++----- cmd/rater/rater.go | 2 +- timespans/calldesc_test.go | 38 ++++++++++++++++++++++++++++++-- timespans/redis_storage.go | 10 ++++----- timespans/timespans_test.go | 2 +- 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/cmd/loader/redis/redis_loader.go b/cmd/loader/redis/redis_loader.go index 546457d78..fff219dc4 100644 --- a/cmd/loader/redis/redis_loader.go +++ b/cmd/loader/redis/redis_loader.go @@ -1,12 +1,38 @@ package main import ( - "fmt" - "github.com/simonz05/godis" + "fmt" + "github.com/simonz05/godis" + "github.com/rif/cgrates/timespans" + "time" ) func main() { - r := godis.New("", 10, "") - r.Set("test", 12224) - fmt.Println("Done!") + r := godis.New("", 10, "") + t1 := time.Date(2012, time.January, 1, 0, 0, 0, 0, time.UTC) + cd1 := ×pans.CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256"} + ap1 := ×pans.ActivationPeriod{ActivationTime: t1} + ap1.AddInterval(×pans.Interval{ + WeekDays: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}, + EndTime: "18:00:00", + ConnectFee: 0, + Price: 0.2, + BillingUnit: 1.0}) + ap1.AddInterval(×pans.Interval{ + WeekDays: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}, + StartTime: "18:00:00", + ConnectFee: 0, + Price: 0.1, + BillingUnit: 1.0}) + ap1.AddInterval(×pans.Interval{ + WeekDays: []time.Weekday{time.Saturday, time.Sunday}, + ConnectFee: 0, + Price: 0.1, + BillingUnit: 1.0}) + cd1.AddActivationPeriod(ap1) + key := cd1.GetKey() + + value := cd1.EncodeValues() + r.Set(key, string(value)) + fmt.Println("Done!") } diff --git a/cmd/rater/rater.go b/cmd/rater/rater.go index ad76e6923..63dbef9ad 100644 --- a/cmd/rater/rater.go +++ b/cmd/rater/rater.go @@ -45,7 +45,7 @@ func (s *Storage) Shutdown(args string, reply *string) (err error) { func main() { flag.Parse() getter, err := timespans.NewKyotoStorage("storage.kch") - //getter, err := NewRedisStorage("tcp:127.0.0.1:6379") + //getter, err := NewRedisStorage("tcp:127.0.0.1:6379", 10) //defer getter.Close() if err != nil { log.Printf("Cannot open storage file: %v", err) diff --git a/timespans/calldesc_test.go b/timespans/calldesc_test.go index a3ebf55f1..4dba52289 100644 --- a/timespans/calldesc_test.go +++ b/timespans/calldesc_test.go @@ -5,7 +5,7 @@ import ( "time" ) -func TestSplitSpans(t *testing.T) { +func TestKyotoSplitSpans(t *testing.T) { getter, _ := NewKyotoStorage("test.kch") defer getter.Close() @@ -24,7 +24,27 @@ func TestSplitSpans(t *testing.T) { } } -func ATestGetCost(t *testing.T) { +func TestRedisSplitSpans(t *testing.T) { + getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10) + defer getter.Close() + + t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC) + t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) + cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2} + key := cd.GetKey() + values, _ := getter.Get(key) + + cd.decodeValues([]byte(values)) + + intervals := cd.getActiveIntervals() + timespans := cd.splitInTimeSpans(intervals) + if len(timespans) != 2 { + t.Error("Wrong number of timespans: ", len(timespans)) + } +} + + +func TestKyotoGetCost(t *testing.T) { getter, _ := NewKyotoStorage("test.kch") defer getter.Close() @@ -38,6 +58,20 @@ func ATestGetCost(t *testing.T) { } } +func TestRedisGetCost(t *testing.T) { + getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10) + defer getter.Close() + + t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC) + t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) + cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2} + result, _ := cd.GetCost(getter) + expected := &CallCost{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", Cost: 360, ConnectFee: 0} + if *result != *expected { + t.Errorf("Expected %v was %v", expected, result) + } +} + func BenchmarkGetCost(b *testing.B) { b.StopTimer() getter, _ := NewKyotoStorage("test.kch") diff --git a/timespans/redis_storage.go b/timespans/redis_storage.go index 68f02fa4e..a43d5c7b8 100644 --- a/timespans/redis_storage.go +++ b/timespans/redis_storage.go @@ -2,21 +2,21 @@ package timespans import ( "github.com/simonz05/godis" - "log" + //"log" ) type RedisStorage struct { db *godis.Client } -func NewRedisStorage(address string) (*RedisStorage, error) { - ndb := godis.New(address, 10, "") - log.Print("Starting redis storage") +func NewRedisStorage(address string, db int) (*RedisStorage, error) { + ndb := godis.New(address, db, "") + //log.Print("Starting redis storage") return &RedisStorage{db: ndb}, nil } func (rs *RedisStorage) Close() { - log.Print("Closing redis storage") + //log.Print("Closing redis storage") rs.db.Quit() } diff --git a/timespans/timespans_test.go b/timespans/timespans_test.go index 4ef7f7ea0..c7b113ab0 100644 --- a/timespans/timespans_test.go +++ b/timespans/timespans_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestGetCost(t *testing.T) { +func TestTimespanGetCost(t *testing.T) { t1 := time.Date(2012, time.February, 5, 17, 45, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 5, 17, 55, 0, 0, time.UTC) ts1 := TimeSpan{TimeStart: t1, TimeEnd: t2}