a little optimization

This commit is contained in:
Radu Ioan Fericean
2012-02-07 19:36:26 +02:00
parent 8b1efe8c1e
commit 238ca8d506
3 changed files with 75 additions and 58 deletions

View File

@@ -61,15 +61,15 @@ func (cd *CallDescriptor) GetKey() string {
Finds the activation periods applicable to the call descriptior.
*/
func (cd *CallDescriptor) getActivePeriods() (is []*ActivationPeriod) {
is = make([]*ActivationPeriod, 1) // make room for the initial activation period
bestTime := time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC)
for _, ap := range cd.ActivationPeriods {
t := ap.ActivationTime
if t.After(bestTime) && t.Before(cd.TimeStart) {
bestTime = t
bestTime := cd.ActivationPeriods[0].ActivationTime
is = append(is, cd.ActivationPeriods[0])
for _, ap := range cd.ActivationPeriods {
if ap.ActivationTime.After(bestTime) && ap.ActivationTime.Before(cd.TimeStart) {
bestTime = ap.ActivationTime
is[0] = ap
}
if t.After(cd.TimeStart) && t.Before(cd.TimeEnd) {
if ap.ActivationTime.After(cd.TimeStart) && ap.ActivationTime.Before(cd.TimeEnd) {
is = append(is, ap)
}
}
@@ -86,11 +86,11 @@ func (cd *CallDescriptor) splitInTimeSpans(aps []*ActivationPeriod) (timespans [
timespans = append(timespans, ts1)
for _, ap := range aps {
for _, ts := range timespans {
for i := 0; i < len(timespans); i++ {
ts := timespans[i]
newTs := ts.SplitByActivationPeriod(ap)
if newTs != nil {
timespans = append(timespans, newTs)
break
}
}
}
@@ -117,9 +117,7 @@ func (cd *CallDescriptor) GetCost(sg StorageGetter) (result *CallCost, err error
values, err := sg.Get(key)
cd.decodeValues(values)
periods := cd.getActivePeriods()
timespans := cd.splitInTimeSpans(periods)
timespans := cd.splitInTimeSpans(cd.getActivePeriods())
cost := 0.0
for _, ts := range timespans {

View File

@@ -78,9 +78,9 @@ func TestRedisGetCost(t *testing.T) {
}
}
func BenchmarkKyotoGetCost(b *testing.B) {
func BenchmarkRedisGetting(b *testing.B) {
b.StopTimer()
getter, _ := NewKyotoStorage("test.kch")
getter, _ := NewRedisStorage("", 10)
defer getter.Close()
t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC)
@@ -88,7 +88,7 @@ func BenchmarkKyotoGetCost(b *testing.B) {
cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2}
b.StartTimer()
for i := 0; i < b.N; i++ {
cd.GetCost(getter)
getter.Get(cd.GetKey())
}
}
@@ -106,24 +106,6 @@ func BenchmarkRedisGetCost(b *testing.B) {
}
}
func BenchmarkSplitting(b *testing.B) {
b.StopTimer()
getter, _ := NewRedisStorage("", 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(values)
b.StartTimer()
for i := 0; i < b.N; i++ {
periods := cd.getActivePeriods()
cd.splitInTimeSpans(periods)
}
}
func BenchmarkKyotoGetting(b *testing.B) {
b.StopTimer()
getter, _ := NewKyotoStorage("test.kch")
@@ -134,21 +116,8 @@ func BenchmarkKyotoGetting(b *testing.B) {
cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2}
b.StartTimer()
for i := 0; i < b.N; i++ {
getter.Get(cd.GetKey())
}
}
func BenchmarkRedisGetting(b *testing.B) {
b.StopTimer()
getter, _ := NewRedisStorage("", 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}
b.StartTimer()
for i := 0; i < b.N; i++ {
getter.Get(cd.GetKey())
key := cd.GetKey()
getter.Get(key)
}
}
@@ -157,7 +126,9 @@ func BenchmarkDecoding(b *testing.B) {
getter, _ := NewKyotoStorage("test.kch")
defer getter.Close()
cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256"}
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)
@@ -166,3 +137,53 @@ func BenchmarkDecoding(b *testing.B) {
cd.decodeValues(values)
}
}
func BenchmarkSplitting(b *testing.B) {
b.StopTimer()
getter, _ := NewKyotoStorage("test.kch")
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(values)
b.StartTimer()
for i := 0; i < b.N; i++ {
cd.splitInTimeSpans(cd.getActivePeriods())
}
}
func BenchmarkKyotoGetCost(b *testing.B) {
b.StopTimer()
getter, _ := NewKyotoStorage("test.kch")
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}
b.StartTimer()
for i := 0; i < b.N; i++ {
cd.GetCost(getter)
}
}
func BenchmarkGetCostExp(b *testing.B) {
b.StopTimer()
getter, _ := NewKyotoStorage("test.kch")
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}
b.StartTimer()
for i := 0; i < b.N; i++ {
key := cd.GetKey()
values, _ := getter.Get(key)
cd.decodeValues(values)
cd.splitInTimeSpans(cd.getActivePeriods())
}
}

View File

@@ -79,10 +79,9 @@ func (ts *TimeSpan) SplitByInterval(i *Interval) (nts *TimeSpan) {
if splitTime == ts.TimeStart {
return
}
oldTimeEnd := ts.TimeEnd
nts = &TimeSpan{TimeStart: splitTime, TimeEnd: ts.TimeEnd}
ts.TimeEnd = splitTime
nts = &TimeSpan{TimeStart: splitTime, TimeEnd: oldTimeEnd}
return
}
// if only the end time is in the interval split the interval
@@ -91,10 +90,9 @@ func (ts *TimeSpan) SplitByInterval(i *Interval) (nts *TimeSpan) {
if splitTime == ts.TimeEnd {
return
}
oldTimeEnd := ts.TimeEnd
nts = &TimeSpan{TimeStart: splitTime, TimeEnd: ts.TimeEnd}
ts.TimeEnd = splitTime
nts = &TimeSpan{TimeStart: splitTime, TimeEnd: oldTimeEnd}
nts.SetInterval(i)
return
}
@@ -104,11 +102,11 @@ func (ts *TimeSpan) SplitByInterval(i *Interval) (nts *TimeSpan) {
/*
Splits the given timespan on activation period's activation time.
*/
func (ts *TimeSpan) SplitByActivationPeriod(ap *ActivationPeriod) *TimeSpan {
func (ts *TimeSpan) SplitByActivationPeriod(ap *ActivationPeriod) (newTs *TimeSpan) {
if !ts.Contains(ap.ActivationTime) {
return nil
}
oldTimeEnd := ts.TimeEnd
}
newTs = &TimeSpan{TimeStart: ap.ActivationTime, TimeEnd: ts.TimeEnd, ActivationPeriod: ap}
ts.TimeEnd = ap.ActivationTime
return &TimeSpan{TimeStart: ap.ActivationTime, TimeEnd: oldTimeEnd, ActivationPeriod: ap}
return
}