Permalink
Browse files

Merge branch 'rate-simplify'

  • Loading branch information...
2 parents 3e4439c + 7f510aa commit b0db2bfa6e7a5dfa0138cd1d5d5ee26a1f736018 @jlouis committed May 31, 2010
Showing with 8 additions and 10 deletions.
  1. +8 −10 src/RateCalc.hs
View
@@ -20,13 +20,12 @@ data Rate = Rate
{ rate :: !Double -- ^ The current rate
, bytes :: !Int -- ^ The amount of bytes transferred since last rate extraction
, count :: !Int -- ^ The amount of bytes transferred since last count extraction
- , nextExpected :: !UTCTime -- ^ When is the next rate update expected
, lastExt :: !UTCTime -- ^ When was the last rate update
, rateSince :: !UTCTime -- ^ From where is the rate measured
}
instance NFData Rate where
- rnf (Rate r b c _ _ _) =
+ rnf (Rate r b c _ _) =
rnf r `seq` rnf b `seq` rnf c
fudge :: NominalDiffTime
@@ -39,7 +38,6 @@ new :: UTCTime -> Rate
new t = Rate { rate = 0.0
, bytes = 0
, count = 0
- , nextExpected = addUTCTime fudge t
, lastExt = addUTCTime (-fudge) t
, rateSince = addUTCTime (-fudge) t
}
@@ -56,19 +54,19 @@ update n rt = {-# SCC "update" #-}
-- structure and updates the rate structures internal book-keeping
extractRate :: UTCTime -> Rate -> (Double, Rate)
extractRate t rt = {-# SCC "extractRate" #-}
- let oldWindow :: Double
- oldWindow = {-# SCC "diffUTC1" #-} realToFrac $ diffUTCTime (lastExt rt) (rateSince rt)
+ let since = rateSince rt
+ lext = lastExt rt
+ n = bytes rt
+ oldWindow :: Double
+ oldWindow = {-# SCC "diffUTC1" #-} realToFrac $ diffUTCTime lext since
newWindow :: Double
- newWindow = {-# SCC "diffUTS2" #-} realToFrac $ diffUTCTime t (rateSince rt)
- n = bytes rt
+ newWindow = {-# SCC "diffUTS2" #-} realToFrac $ diffUTCTime t since
!r = {-# SCC "r" #-} (rate rt * oldWindow + (fromIntegral n)) / newWindow
- expectN = min 5 (round $ (fromIntegral n / (max r 0.0001)))
!nrt = {-# SCC "rt_creat" #-}
rt { rate = r
, bytes = 0
- , nextExpected = {-# SCC "addUTCTime" #-} addUTCTime (fromInteger expectN) t
, lastExt = t
- , rateSince = {-# SCC "max" #-} max (rateSince rt) (addUTCTime (-maxRatePeriod) t)
+ , rateSince = {-# SCC "max" #-} max since (addUTCTime (-maxRatePeriod) t)
}
in
-- Update the rate and book-keep the missing pieces. The total is simply a built-in

0 comments on commit b0db2bf

Please sign in to comment.