first bench 7100 op/s

This commit is contained in:
Radu Ioan Fericean
2012-02-04 17:18:41 +02:00
parent b4a82e0538
commit e292190958
4 changed files with 29 additions and 28 deletions

View File

@@ -51,7 +51,7 @@ func main() {
db.Close()
} else {
db := godis.New(*redisserver, *redisdb, *redispass)
db.Set(key, string(value))
db.Set(key, value)
db.Quit()
}
fmt.Println("Done!")

View File

@@ -1,9 +1,7 @@
package timespans
import (
"encoding/json"
"fmt"
"log"
"time"
"strings"
"strconv"
@@ -23,7 +21,7 @@ func (ap *ActivationPeriod) AddInterval(is ...*Interval) {
}
}
func (ap *ActivationPeriod) Store() (result string){
func (ap *ActivationPeriod) store() (result string){
result += strconv.FormatInt(ap.ActivationTime.Unix(), 10) + ";"
var is string
for _,i := range ap.Intervals {
@@ -44,11 +42,11 @@ func (ap *ActivationPeriod) Store() (result string){
return
}
func (ap *ActivationPeriod) Restore(input string) {
func (ap *ActivationPeriod) restore(input string) {
elements := strings.Split(input, ";")
unix, _ := strconv.ParseInt(elements[0], 0, 64)
ap.ActivationTime = time.Unix(unix, 0)
for _, is := range elements[1:len(elements) - 1]{
ap.ActivationTime = time.Unix(unix, 0)
for _, is := range elements[1:len(elements) - 1]{
i := &Interval{}
ise := strings.Split(is, "|")
month, _ := strconv.Atoi(ise[0])
@@ -85,25 +83,27 @@ func (cd *CallDescriptor) AddActivationPeriod(aps ...*ActivationPeriod) {
}
}
func (cd *CallDescriptor) EncodeValues() []byte {
jo, err := json.Marshal(cd.ActivationPeriods)
if err != nil {
log.Print("Cannot encode intervals: ", err)
func (cd *CallDescriptor) EncodeValues() (result string) {
for _, ap := range cd.ActivationPeriods {
result += ap.store() + "\n"
}
return
}
func (cd *CallDescriptor) decodeValues(v string) {
for _, aps := range strings.Split(v, "\n") {
if(len(aps)>0){
ap := &ActivationPeriod{}
ap.restore(aps)
cd.ActivationPeriods = append(cd.ActivationPeriods, ap)
}
}
return jo
}
func (cd *CallDescriptor) GetKey() string {
return fmt.Sprintf("%s:%s:%s", cd.CstmId, cd.Subject, cd.DestinationPrefix)
}
func (cd *CallDescriptor) decodeValues(v []byte) {
err := json.Unmarshal(v, &cd.ActivationPeriods)
if err != nil {
log.Print("Cannot decode intervals: ", err)
}
}
func (cd *CallDescriptor) getActiveIntervals() (is []*Interval) {
now := time.Now()
// add a second in the future to be able to pick the active timestamp
@@ -143,7 +143,7 @@ func (cd *CallDescriptor) GetCost(sg StorageGetter) (result *CallCost, err error
key := cd.GetKey()
values, err := sg.Get(key)
cd.decodeValues([]byte(values))
cd.decodeValues(values)
intervals := cd.getActiveIntervals()
timespans := cd.splitInTimeSpans(intervals)

View File

@@ -15,7 +15,7 @@ func TestKyotoSplitSpans(t *testing.T) {
key := cd.GetKey()
values, _ := getter.Get(key)
cd.decodeValues([]byte(values))
cd.decodeValues(values)
intervals := cd.getActiveIntervals()
timespans := cd.splitInTimeSpans(intervals)
@@ -34,7 +34,7 @@ func TestRedisSplitSpans(t *testing.T) {
key := cd.GetKey()
values, _ := getter.Get(key)
cd.decodeValues([]byte(values))
cd.decodeValues(values)
intervals := cd.getActiveIntervals()
timespans := cd.splitInTimeSpans(intervals)
@@ -73,7 +73,8 @@ func TestRedisGetCost(t *testing.T) {
}
func TestApStoreRestore(t *testing.T) {
d := time.Date(2012, time.February, 1, 14, 30, 1, 0, time.UTC)
loc, _ := time.LoadLocation("Local")
d := time.Date(2012, time.February, 1, 14, 30, 1, 0, loc)
i := &Interval{Month: time.February,
MonthDay: 1,
WeekDays: []time.Weekday{time.Wednesday, time.Thursday},
@@ -81,13 +82,13 @@ func TestApStoreRestore(t *testing.T) {
EndTime: "15:00:00"}
ap := ActivationPeriod{ActivationTime: d}
ap.AddInterval(i)
result := ap.Store()
expected := "1328106601;2|1|3,4|14:30:00|15:00:00|0|0|0|0;"
result := ap.store()
expected := "1328099401;2|1|3,4|14:30:00|15:00:00|0|0|0|0;"
if result != expected {
t.Errorf("Expected %q was %q", expected, result)
}
ap1 := ActivationPeriod{}
ap1.Restore(result)
ap1.restore(result)
if ap1.ActivationTime != ap.ActivationTime {
t.Errorf("Expected %v was %v", ap.ActivationTime, ap1.ActivationTime)
}
@@ -205,13 +206,13 @@ func BenchmarkDecoding(b *testing.B) {
b.StartTimer()
for i := 0; i < b.N; i++ {
cd.decodeValues([]byte(values))
cd.decodeValues(values)
}
}
func BenchmarkRestore(b *testing.B) {
ap1 := ActivationPeriod{}
for i := 0; i < b.N; i++ {
ap1.Restore("1328106601,1328106601000000000;2|1|3,4|14:30:00|15:00:00|0|0|0|0;")
ap1.restore("1328106601;2|1|3,4|14:30:00|15:00:00|0|0|0|0;")
}
}

Binary file not shown.