mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
mongo working (much slower)
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"github.com/rif/cgrates/timespans"
|
||||
"log"
|
||||
"os"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
var (
|
||||
storage = flag.String("storage", "kyoto", "kyoto|redis|mongo")
|
||||
kyotofile = flag.String("kyotofile", "storage.kch", "kyoto storage file (storage.kch)")
|
||||
storage = flag.String("storage", "kyoto", "kyoto|redis|mongo")
|
||||
kyotofile = flag.String("kyotofile", "storage.kch", "kyoto storage file (storage.kch)")
|
||||
redisserver = flag.String("redisserver", "tcp:127.0.0.1:6379", "redis server address (tcp:127.0.0.1:6379)")
|
||||
redisdb = flag.Int("rdb", 10, "redis database number (10)")
|
||||
mongoserver = flag.String("mongoserver", "127.0.0.1:27017", "mongo server address (127.0.0.1:27017)")
|
||||
redisdb = flag.Int("rdb", 10, "redis database number (10)")
|
||||
mongodb = flag.String("mdb", "test", "mongo database name (test)")
|
||||
redispass = flag.String("pass", "", "redis database password")
|
||||
inputfile = flag.String("inputfile", "input.json", "redis database password")
|
||||
mongodb = flag.String("mdb", "test", "mongo database name (test)")
|
||||
redispass = flag.String("pass", "", "redis database password")
|
||||
inputfile = flag.String("inputfile", "input.json", "redis database password")
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -36,38 +36,30 @@ func main() {
|
||||
|
||||
var callDescriptors []*timespans.CallDescriptor
|
||||
if err := dec.Decode(&callDescriptors); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
switch *storage {
|
||||
case "kyoto":
|
||||
storage, _ := timespans.NewKyotoStorage(*kyotofile)
|
||||
defer storage.Close()
|
||||
for _, cd := range callDescriptors{
|
||||
for _, cd := range callDescriptors {
|
||||
storage.SetActivationPeriods(cd.GetKey(), cd.ActivationPeriods)
|
||||
log.Printf("Storing %q", cd.GetKey())
|
||||
}
|
||||
case "mongo":
|
||||
storage, _ := timespans.NewMongoStorage("127.0.0.1", "test")
|
||||
defer storage.Close()
|
||||
for _, cd := range callDescriptors {
|
||||
storage.SetActivationPeriods(cd.GetKey(), cd.ActivationPeriods)
|
||||
log.Printf("Storing %q", cd.GetKey())
|
||||
}
|
||||
/*case "mongo":
|
||||
session, err := mgo.Dial(*mongoserver)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer session.Close()
|
||||
session.SetMode(mgo.Strong, true)
|
||||
|
||||
c := session.DB(*mongodb).C("ap")
|
||||
for _, cd := range callDescriptors{
|
||||
key := cd.GetKey()
|
||||
c.Insert(&map[string]string{"_id":key, "value":cd.EncodeValues()})
|
||||
log.Printf("Storing %q", key)
|
||||
}*/
|
||||
|
||||
default:
|
||||
storage, _ := timespans.NewKyotoStorage(*redisserver, *redisdb)
|
||||
storage, _ := timespans.NewRedisStorage(*redisserver, *redisdb)
|
||||
defer storage.Close()
|
||||
for _, cd := range callDescriptors{
|
||||
key := cd.GetKey()
|
||||
for _, cd := range callDescriptors {
|
||||
storage.SetActivationPeriods(cd.GetKey(), cd.ActivationPeriods)
|
||||
log.Printf("Storing %q", cd.GetKey())
|
||||
}
|
||||
|
||||
@@ -161,7 +161,9 @@ func (cd *CallDescriptor) getPresentSecondCost(sg StorageGetter) (cost float64,
|
||||
ts := &TimeSpan{TimeStart: now, TimeEnd: now.Add(oneSecond)}
|
||||
timespans := cd.splitTimeSpan(ts)
|
||||
|
||||
cost = round(timespans[0].GetCost(), 3)
|
||||
if len(timespans)>0 {
|
||||
cost = round(timespans[0].GetCost(), 3)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -67,6 +67,20 @@ func TestRedisGetCost(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMongoGetCost(t *testing.T) {
|
||||
getter, _ := NewMongoStorage("127.0.0.1", "test")
|
||||
defer getter.Close()
|
||||
|
||||
t1 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC)
|
||||
t2 := time.Date(2012, time.February, 2, 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: 540, ConnectFee: 0}
|
||||
if result.Cost != expected.Cost || result.ConnectFee != expected.ConnectFee {
|
||||
t.Errorf("Expected %v was %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFullDestNotFound(t *testing.T) {
|
||||
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
|
||||
defer getter.Close()
|
||||
@@ -91,6 +105,8 @@ func TestMultipleActivationPeriods(t *testing.T) {
|
||||
result, _ := cd.GetCost(getter)
|
||||
expected := &CallCost{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0257", Cost: 330, ConnectFee: 0}
|
||||
if result.Cost != expected.Cost || result.ConnectFee != expected.ConnectFee {
|
||||
t.Log(result.Timespans[0].ActivationPeriod)
|
||||
t.Log(result.Timespans[1].ActivationPeriod)
|
||||
t.Errorf("Expected %v was %v", expected, result)
|
||||
}
|
||||
}
|
||||
@@ -262,21 +278,20 @@ func BenchmarkMongoGetting(b *testing.B) {
|
||||
cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2}
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
key := cd.GetKey()
|
||||
getter.Get(key)
|
||||
getter.GetActivationPeriods(cd.GetKey())
|
||||
}
|
||||
}
|
||||
|
||||
//func BenchmarkMongoGetCost(b *testing.B) {
|
||||
// b.StopTimer()
|
||||
// getter, _ := NewMongoStorage("127.0.0.1", "test")
|
||||
// defer getter.Close()
|
||||
func BenchmarkMongoGetCost(b *testing.B) {
|
||||
b.StopTimer()
|
||||
getter, _ := NewMongoStorage("127.0.0.1", "test")
|
||||
defer getter.Close()
|
||||
|
||||
// t1 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC)
|
||||
// t2 := time.Date(2012, time.February, 2, 18, 30, 0, 0, time.UTC)
|
||||
// cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2}
|
||||
// b.StartTimer()
|
||||
// for i := 0; i < b.N; i++ {
|
||||
// cd.GetCost(getter)
|
||||
// }
|
||||
//}
|
||||
t1 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC)
|
||||
t2 := time.Date(2012, time.February, 2, 18, 30, 0, 0, time.UTC)
|
||||
cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2}
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
cd.GetCost(getter)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
)
|
||||
|
||||
type KeyValue struct {
|
||||
_id string
|
||||
value string
|
||||
Key string
|
||||
ActivationPeriods []*ActivationPeriod
|
||||
}
|
||||
|
||||
type MongoStorage struct {
|
||||
@@ -30,8 +30,12 @@ func (ms *MongoStorage) Close() {
|
||||
ms.session.Close()
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) Get(key string) (string, error) {
|
||||
func (ms *MongoStorage) GetActivationPeriods(key string) (aps []*ActivationPeriod, err error) {
|
||||
result := KeyValue{}
|
||||
err := ms.db.Find(bson.M{"_id": key}).One(&result)
|
||||
return result.value, err
|
||||
err = ms.db.Find(bson.M{"key": key}).One(&result)
|
||||
return result.ActivationPeriods, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetActivationPeriods(key string, aps []*ActivationPeriod){
|
||||
ms.db.Insert(&KeyValue{key, aps})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user