summaryrefslogtreecommitdiff
path: root/bench/Main.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-02 05:01:46 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-02 05:01:46 +0400
commit01f51b51af8a67516238bc7264079601a7e2ece5 (patch)
tree13b346ad0ba930e7d964a3de9988365f6cc6ba4f /bench/Main.hs
parent5e92eec501e0a1ca6d09a01e078cf54ff3277273 (diff)
~ Use IntSet instead of ByteString for bitfields.
There are several reasons for this: * IntSet is stored in ordinary heap, while ByteStrings in pinned memory; * Our IntSet's should be much faster 90% time. (in typical BT client) Hovewer in worst case IntSet is slower, but difference should is not so big. (We should measure this although) * It's pure, tested, error-free and much more convenient. Moreover we have kill a lot of ugly code!
Diffstat (limited to 'bench/Main.hs')
-rw-r--r--bench/Main.hs69
1 files changed, 2 insertions, 67 deletions
diff --git a/bench/Main.hs b/bench/Main.hs
index 6e8a0ce3..120f5b04 100644
--- a/bench/Main.hs
+++ b/bench/Main.hs
@@ -22,8 +22,7 @@ instance NFData BlockIx where
22instance NFData Block where 22instance NFData Block where
23 rnf (Block a b c) = a `deepseq` b `deepseq` rnf c 23 rnf (Block a b c) = a `deepseq` b `deepseq` rnf c
24 24
25instance NFData Bitfield where 25instance NFData Bitfield
26 rnf = rnf . bfBits
27 26
28instance NFData Message where 27instance NFData Message where
29 rnf (Have i) = rnf i 28 rnf (Have i) = rnf i
@@ -40,69 +39,5 @@ encodeMessages xs = runPut (mapM_ put xs)
40decodeMessages :: ByteString -> Either String [Message] 39decodeMessages :: ByteString -> Either String [Message]
41decodeMessages = runGet (many get) 40decodeMessages = runGet (many get)
42 41
43bitfieldMin :: Int -> Maybe Int
44bitfieldMin n = findMin (BT.empty n)
45
46bitfieldMax :: Int -> Maybe Int
47bitfieldMax n = findMax (BT.empty n)
48
49bitfieldDiff :: Int -> Bitfield
50bitfieldDiff n = BT.empty n `difference` BT.empty n
51
52bitfieldInter :: Int -> Bitfield
53bitfieldInter n = BT.empty n `intersection` BT.empty n
54
55bitfieldUnion :: Int -> Bitfield
56bitfieldUnion n = BT.empty n `union` BT.empty n
57
58bitfieldHaveCount :: Int -> Int
59bitfieldHaveCount n = haveCount (BT.full n)
60
61selectionStrictFirst :: Int -> Maybe Int
62selectionStrictFirst n = strictFirst (BT.empty n) (BT.empty n) []
63
64selectionStrictLast :: Int -> Maybe Int
65selectionStrictLast n = strictLast (BT.empty n) (BT.empty n) []
66
67selectionRarestFirst :: Int -> Maybe Int
68selectionRarestFirst n = rarestFirst (BT.empty n) (BT.empty n)
69 (replicate 10 (BT.empty n))
70
71selectionEndGame :: Int -> Maybe Int
72selectionEndGame n = endGame (BT.empty n) (BT.empty n) []
73
74main :: IO () 42main :: IO ()
75main = do 43main = defaultMain []
76 let blockixs = replicate 5000 (Request (BlockIx 0 0 0))
77 let bitfields = replicate 5000 (Bitfield (MkBitfield (B.replicate 1000 0)))
78 let chokes = replicate 5000 Choke
79 let havenones = replicate 5000 HaveNone
80
81 let m = 1024 * 1024
82
83 defaultMain $
84 concatMap (uncurry mkMsgBench)
85 [ ("blockIx", blockixs)
86 , ("bitfield", bitfields)
87 , ("choke", chokes)
88 , ("havenone", havenones)
89 ]
90 ++ -- 256KiB * 10M = 2.5TiB bitfield for 10 ms
91 [ bench "bitfield/min" $ nf bitfieldMin (10 * m)
92 , bench "bitfield/max" $ nf bitfieldMax (10 * m)
93 , bench "bitfield/difference" $ nf bitfieldDiff (10 * m)
94 , bench "bitfield/intersection" $ nf bitfieldInter (10 * m)
95 , bench "bitfield/union" $ nf bitfieldUnion (10 * m)
96 , bench "bitfield/haveCount" $ nf bitfieldHaveCount (10 * m)
97
98 , bench "selection/strictFirst" $ nf selectionStrictFirst (10 * m)
99 , bench "selection/strictLast" $ nf selectionStrictLast (10 * m)
100 , bench "selection/rarestFirst" $ nf selectionRarestFirst (10 * m)
101 , bench "selection/endGame" $ nf selectionEndGame (10 * m)
102 ]
103 where
104 mkMsgBench name msgs =
105 [ msgs `deepseq` bench ("message/" ++ name ++ "/encode") $ nf encodeMessages msgs
106 , let binary = encodeMessages msgs in
107 binary `deepseq` bench ("message/" ++ name ++ "/decode") $ nf decodeMessages binary
108 ]