redis up to speed

This commit is contained in:
Radu Ioan Fericean
2012-02-03 16:37:41 +02:00
parent 66721ba8c0
commit 7f036b7e5a
5 changed files with 74 additions and 14 deletions

View File

@@ -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 := &timespans.CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256"}
ap1 := &timespans.ActivationPeriod{ActivationTime: t1}
ap1.AddInterval(&timespans.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(&timespans.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(&timespans.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!")
}

View File

@@ -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)

View File

@@ -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")

View File

@@ -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()
}

View File

@@ -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}