tested new radis redis client

This commit is contained in:
Radu Ioan Fericean
2012-06-28 16:57:11 +03:00
parent a8b0f06f09
commit 7adaa00f14
13 changed files with 184 additions and 60 deletions

View File

@@ -15,6 +15,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package balancer
import (

View File

@@ -15,6 +15,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (

View File

@@ -15,6 +15,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (
@@ -185,7 +186,7 @@ func loadAccountActions() {
if record[0] == "Tenant" {
continue
}
tag := fmt.Sprintf("%s:%s:%s", record[0], record[1], record[2])
tag := fmt.Sprintf("%s:%s:%s", record[2], record[0], record[1])
aTriggers, exists := actionsTriggers[record[4]]
if !exists {
log.Printf("Could not get action triggers for tag %v", record[4])

View File

@@ -15,6 +15,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (
@@ -60,7 +61,7 @@ func writeToDatabase() {
// rating profiles
for _, cds := range ratingProfiles {
for _, cd := range cds {
storage.SetActivationPeriodsOrFallback(cd.GetKey(), cd.ActivationPeriods, cd.FallbackKey)
err = storage.SetActivationPeriodsOrFallback(cd.GetKey(), cd.ActivationPeriods, cd.FallbackKey)
log.Print(cd.GetKey())
}
}

View File

@@ -15,6 +15,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (

View File

@@ -15,6 +15,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (

View File

@@ -15,6 +15,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (
@@ -26,10 +27,9 @@ import (
)
var (
runs = flag.Int("runs", 10000, "stress cycle number")
runs = flag.Int("runs", 10000, "stress cycle number")
)
func main() {
flag.Parse()
t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC)

View File

@@ -15,6 +15,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (
@@ -65,7 +66,7 @@ func main() {
log.Print(result, j, err)
memstats := new(runtime.MemStats)
runtime.ReadMemStats(memstats)
log.Printf("memstats before GC: Kbytes = %d footprint = %d",
runtime.ReadMemStats(memstats)
log.Printf("memstats before GC: Kbytes = %d footprint = %d",
memstats.HeapAlloc/1024, memstats.Sys/1024)
}

View File

@@ -84,6 +84,7 @@ var (
"CHANGE_TO_PREPAID": changeToPrepaidAction,
"TOPUP_RESET": topupResetAction,
"TOPUP_ADD": topupAddAction,
"DEBIT": debitAction,
}
)
@@ -108,11 +109,16 @@ func changeToPrepaidAction(ub *UserBalance, a *Action) (err error) {
}
func topupResetAction(ub *UserBalance, a *Action) (err error) {
return
ub.BalanceMap[a.BalanceId] = a.Units
return storageGetter.SetUserBalance(ub)
}
func topupAddAction(ub *UserBalance, a *Action) (err error) {
ub.BalanceMap[a.BalanceId] += a.Units
return storageGetter.SetUserBalance(ub)
}
func debitAction(ub *UserBalance, a *Action) (err error) {
return
}

View File

@@ -25,7 +25,7 @@ import (
)
func init() {
sg, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
sg, _ := NewRedixStorage("tcp:127.0.0.1:6379", 10)
SetStorageGetter(sg)
}
@@ -78,8 +78,6 @@ func TestMultipleResultMerge(t *testing.T) {
}
func TestMultipleInputLeftMerge(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
t1 := time.Date(2012, time.February, 2, 17, 59, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 2, 18, 01, 0, 0, time.UTC)
cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}
@@ -104,8 +102,6 @@ func TestMultipleInputLeftMerge(t *testing.T) {
}
func TestMultipleInputRightMerge(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
t1 := time.Date(2012, time.February, 2, 17, 58, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 2, 17, 59, 0, 0, time.UTC)
cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}

View File

@@ -24,19 +24,13 @@ import (
//"log"
)
/*
json
BenchmarkRedisGetCost 5000 462787 ns/op
BenchmarkKyotoGetCost 10000 203543 ns/op
gob
BenchmarkRedisGetCost 10000 258751 ns/op
BenchmarkKyotoGetCost 50000 38449 ns/op
*/
var (
getter StorageGetter
)
func init() {
sg, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
SetStorageGetter(sg)
getter, _ = NewRedisStorage("tcp:127.0.0.1:6379", 10)
SetStorageGetter(getter)
}
func TestSplitSpans(t *testing.T) {
@@ -116,9 +110,6 @@ func TestMultipleActivationPeriods(t *testing.T) {
}
func TestSpansMultipleActivationPeriods(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
t1 := time.Date(2012, time.February, 7, 23, 50, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 8, 0, 30, 0, 0, time.UTC)
cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0257308200", TimeStart: t1, TimeEnd: t2}
@@ -130,9 +121,6 @@ func TestSpansMultipleActivationPeriods(t *testing.T) {
}
func TestLessThanAMinute(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
t1 := time.Date(2012, time.February, 8, 23, 50, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 8, 23, 50, 30, 0, time.UTC)
cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0257308200", TimeStart: t1, TimeEnd: t2}
@@ -144,9 +132,6 @@ func TestLessThanAMinute(t *testing.T) {
}
func TestUniquePrice(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
t1 := time.Date(2012, time.February, 8, 22, 50, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 8, 23, 50, 21, 0, time.UTC)
cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0723045326", TimeStart: t1, TimeEnd: t2}
@@ -158,9 +143,6 @@ func TestUniquePrice(t *testing.T) {
}
func TestPresentSecodCost(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
t1 := time.Date(2012, time.February, 8, 22, 50, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 8, 23, 50, 21, 0, time.UTC)
cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0723", TimeStart: t1, TimeEnd: t2}
@@ -172,9 +154,6 @@ func TestPresentSecodCost(t *testing.T) {
}
func TestMinutesCost(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
t1 := time.Date(2012, time.February, 8, 22, 50, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 8, 22, 51, 50, 0, time.UTC)
cd := &CallDescriptor{Tenant: "vdf", Subject: "minutosu", Destination: "0723", TimeStart: t1, TimeEnd: t2}
@@ -187,8 +166,6 @@ func TestMinutesCost(t *testing.T) {
}
func TestMaxSessionTimeNoUserBalance(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0723", Amount: 1000}
result, err := cd.GetMaxSessionTime()
if result != 1000 || err != nil {
@@ -197,8 +174,6 @@ func TestMaxSessionTimeNoUserBalance(t *testing.T) {
}
func TestMaxSessionTimeWithUserBalance(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
cd := &CallDescriptor{Tenant: "vdf", Subject: "minutosu", Destination: "0723", Amount: 5400}
result, err := cd.GetMaxSessionTime()
if result != 1080 || err != nil {
@@ -207,8 +182,6 @@ func TestMaxSessionTimeWithUserBalance(t *testing.T) {
}
func TestMaxSessionTimeNoCredit(t *testing.T) {
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
defer getter.Close()
cd := &CallDescriptor{Tenant: "vdf", Subject: "broker", Destination: "0723", Amount: 5400}
result, err := cd.GetMaxSessionTime()
if result != 100 || err != nil {
@@ -236,9 +209,6 @@ func TestMaxSessionTimeNoCredit(t *testing.T) {
/*********************************** BENCHMARKS ***************************************/
func BenchmarkRedisGetting(b *testing.B) {
b.StopTimer()
getter, _ := NewRedisStorage("", 10)
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{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}
@@ -250,9 +220,6 @@ func BenchmarkRedisGetting(b *testing.B) {
func BenchmarkRedisRestoring(b *testing.B) {
b.StopTimer()
getter, _ := NewRedisStorage("", 10)
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{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}
@@ -264,9 +231,6 @@ func BenchmarkRedisRestoring(b *testing.B) {
func BenchmarkRedisGetCost(b *testing.B) {
b.StopTimer()
getter, _ := NewRedisStorage("", 10)
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{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}
@@ -278,7 +242,6 @@ func BenchmarkRedisGetCost(b *testing.B) {
func BenchmarkSplitting(b *testing.B) {
b.StopTimer()
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{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}
@@ -291,8 +254,6 @@ func BenchmarkSplitting(b *testing.B) {
func BenchmarkRedisSingleGetSessionTime(b *testing.B) {
b.StopTimer()
getter, _ := NewRedisStorage("", 10)
defer getter.Close()
cd := &CallDescriptor{Tenant: "vdf", Subject: "minutosu", Destination: "0723", Amount: 100}
b.StartTimer()
for i := 0; i < b.N; i++ {
@@ -302,8 +263,6 @@ func BenchmarkRedisSingleGetSessionTime(b *testing.B) {
func BenchmarkRedisMultipleGetSessionTime(b *testing.B) {
b.StopTimer()
getter, _ := NewRedisStorage("", 10)
defer getter.Close()
cd := &CallDescriptor{Tenant: "vdf", Subject: "minutosu", Destination: "0723", Amount: 5400}
b.StartTimer()
for i := 0; i < b.N; i++ {

View File

@@ -19,8 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package timespans
import (
"github.com/simonz05/godis"
// "bytes"
// "encoding/gob"
"encoding/json"
"github.com/simonz05/godis"
)
const (
@@ -30,6 +32,7 @@ const (
type RedisStorage struct {
dbNb int
db *godis.Client
//net bytes.Buffer
}
func NewRedisStorage(address string, db int) (*RedisStorage, error) {
@@ -51,8 +54,12 @@ func (rs *RedisStorage) GetActivationPeriodsOrFallback(key string) (aps []*Activ
if err != nil {
return
}
// rs.net.Reset()
// rs.net.Write(elem)
// err = gob.NewDecoder(&rs.net).Decode(&aps)
err = json.Unmarshal(elem, &aps)
if err != nil {
// err = gob.NewDecoder(&rs.net).Decode(&fallbackKey)
err = json.Unmarshal(elem, &fallbackKey)
}
return
@@ -61,11 +68,15 @@ func (rs *RedisStorage) GetActivationPeriodsOrFallback(key string) (aps []*Activ
func (rs *RedisStorage) SetActivationPeriodsOrFallback(key string, aps []*ActivationPeriod, fallbackKey string) (err error) {
//.db.Select(rs.dbNb)
var result []byte
//rs.net.Reset()
if len(aps) > 0 {
//gob.NewEncoder(&rs.net).Encode(aps)
result, err = json.Marshal(aps)
} else {
//gob.NewEncoder(&rs.net).Encode(fallbackKey)
result, err = json.Marshal(fallbackKey)
}
//result = rs.net.Bytes()
return rs.db.Set(key, result)
}

145
timespans/storage_redix.go Normal file
View File

@@ -0,0 +1,145 @@
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2012 Radu Ioan Fericean
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package timespans
import (
"github.com/fzzbt/radix/redis"
"encoding/json"
// "encoding/gob"
// "bytes"
"log"
)
type RedixStorage struct {
db *redis.Client
//net bytes.Buffer
}
func NewRedixStorage(address string, db int) (*RedixStorage, error) {
ndb, err := redis.NewClient(redis.Configuration{Address: address, Database: db})
if err != nil {
log.Fatalf("Could not connect to redis server: %v", err)
}
return &RedixStorage{db: ndb}, nil
}
func (rs *RedixStorage) Close() {
rs.db.Close()
}
func (rs *RedixStorage) Flush() error {
rs.db.Flushdb()
return nil
}
func (rs *RedixStorage) GetActivationPeriodsOrFallback(key string) (aps []*ActivationPeriod, fallbackKey string, err error) {
elem, err := rs.db.Get(key).Bytes()
if err != nil {
return
}
//rs.net.Reset()
//rs.net.Write(elem)
//err = gob.NewDecoder(&rs.net).Decode(&aps)
err = json.Unmarshal(elem, &aps)
if err != nil {
//err = gob.NewDecoder(&rs.net).Decode(&fallbackKey)
err = json.Unmarshal(elem, &fallbackKey)
}
return
}
func (rs *RedixStorage) SetActivationPeriodsOrFallback(key string, aps []*ActivationPeriod, fallbackKey string) (err error) {
var result []byte
//rs.net.Reset()
if len(aps) > 0 {
//gob.NewEncoder(&rs.net).Encode(aps)
result, err = json.Marshal(aps)
} else {
//gob.NewEncoder(&rs.net).Encode(fallbackKey)
result, err = json.Marshal(fallbackKey)
}
//result = rs.net.Bytes()
return rs.db.Set(key, result).Err
}
func (rs *RedixStorage) GetDestination(key string) (dest *Destination, err error) {
if values, err := rs.db.Get(key).Bytes(); err == nil {
dest = &Destination{Id: key}
err = json.Unmarshal(values, dest)
}
return
}
func (rs *RedixStorage) SetDestination(dest *Destination) (err error) {
result, err := json.Marshal(dest)
return rs.db.Set(dest.Id, result).Err
}
func (rs *RedixStorage) GetActions(key string) (as []*Action, err error) {
if values, err := rs.db.Get(key).Bytes(); err == nil {
err = json.Unmarshal(values, as)
}
return
}
func (rs *RedixStorage) SetActions(key string, as []*Action) (err error) {
result, err := json.Marshal(as)
return rs.db.Set(key, result).Err
}
func (rs *RedixStorage) GetUserBalance(key string) (ub *UserBalance, err error) {
if values, err := rs.db.Get(key).Bytes(); err == nil {
ub = &UserBalance{Id: key}
err = json.Unmarshal(values, ub)
}
return
}
func (rs *RedixStorage) SetUserBalance(ub *UserBalance) (err error) {
result, err := json.Marshal(ub)
return rs.db.Set(ub.Id, result).Err
}
func (rs *RedixStorage) GetActionTimings(key string) (ats []*ActionTiming, err error) {
if values, err := rs.db.Get(key).Bytes(); err == nil {
err = json.Unmarshal(values, ats)
}
return
}
func (rs *RedixStorage) SetActionTimings(key string, ats []*ActionTiming) (err error) {
result, err := json.Marshal(ats)
return rs.db.Set(key, result).Err
}
func (rs *RedixStorage) GetAllActionTimings() (ats []*ActionTiming, err error) {
keys, err := rs.db.Keys(ACTION_TIMING_PREFIX + "*").List()
if err != nil {
return
}
values, err := rs.db.Mget(keys).List()
if err != nil {
return
}
for _, v := range values {
var tempAts []*ActionTiming
err = json.Unmarshal([]byte(v), &tempAts)
ats = append(ats, tempAts...)
}
return
}