diff options
Diffstat (limited to 'bench/Main.hs')
-rw-r--r-- | bench/Main.hs | 84 |
1 files changed, 74 insertions, 10 deletions
diff --git a/bench/Main.hs b/bench/Main.hs index ef9f08c..1cd2429 100644 --- a/bench/Main.hs +++ b/bench/Main.hs | |||
@@ -1,16 +1,20 @@ | |||
1 | {-# LANGUAGE PackageImports #-} | 1 | {-# LANGUAGE PackageImports #-} |
2 | {-# LANGUAGE DeriveGeneric #-} | 2 | {-# LANGUAGE DeriveGeneric #-} |
3 | {-# LANGUAGE RecordWildCards #-} | ||
4 | {-# LANGUAGE OverloadedStrings #-} | ||
3 | module Main (main) where | 5 | module Main (main) where |
4 | 6 | ||
7 | import Control.Applicative | ||
5 | import Control.DeepSeq | 8 | import Control.DeepSeq |
6 | import Data.Maybe | ||
7 | import Data.Attoparsec.ByteString as Atto | 9 | import Data.Attoparsec.ByteString as Atto |
8 | import Data.ByteString as B | 10 | import Data.ByteString as BS |
9 | import Data.ByteString.Lazy as BL | 11 | import qualified Data.ByteString.Lazy as BL |
10 | import Data.List as L | 12 | import Data.List as L |
11 | import Criterion.Main | 13 | import Data.Maybe |
14 | import Data.Monoid | ||
12 | import System.Environment | 15 | import System.Environment |
13 | 16 | ||
17 | import Criterion.Main | ||
14 | import GHC.Generics | 18 | import GHC.Generics |
15 | 19 | ||
16 | import "bencode" Data.BEncode as A | 20 | import "bencode" Data.BEncode as A |
@@ -51,11 +55,63 @@ replicate' c x | |||
51 | go 0 = Nil | 55 | go 0 = Nil |
52 | go n = Cons x $ go (pred n) | 56 | go n = Cons x $ go (pred n) |
53 | 57 | ||
58 | {----------------------------------------------------------------------- | ||
59 | -- Big dicts | ||
60 | -----------------------------------------------------------------------} | ||
61 | |||
62 | data Torrent = Torrent { | ||
63 | tAnnounce :: !ByteString | ||
64 | , tInfo :: !BDict | ||
65 | , tAnnounceList :: !(Maybe ByteString) | ||
66 | , tComment :: !(Maybe ByteString) | ||
67 | , tCreatedBy :: !(Maybe ByteString) | ||
68 | , tCreationDate :: !(Maybe ByteString) | ||
69 | , tEncoding :: !(Maybe ByteString) | ||
70 | , tPublisher :: !(Maybe ByteString) | ||
71 | , tPublisherURL :: !(Maybe ByteString) | ||
72 | , tSignature :: !(Maybe ByteString) | ||
73 | } deriving (Show, Eq) | ||
74 | |||
75 | instance NFData Torrent where | ||
76 | rnf Torrent {..} = () | ||
77 | |||
78 | instance C.BEncode Torrent where | ||
79 | toBEncode Torrent {..} = fromAscAssocs | ||
80 | [ "announce" --> tAnnounce | ||
81 | , "announce-list" -->? tAnnounceList | ||
82 | , "comment" -->? tComment | ||
83 | , "created by" -->? tCreatedBy | ||
84 | , "creation date" -->? tCreationDate | ||
85 | , "encoding" -->? tEncoding | ||
86 | , "info" --> tInfo | ||
87 | , "publisher" -->? tPublisher | ||
88 | , "publisher-url" -->? tPublisherURL | ||
89 | , "signature" -->? tSignature | ||
90 | ] | ||
91 | |||
92 | fromBEncode (C.BDict d) = | ||
93 | Torrent <$> d >-- "announce" | ||
94 | <*> d >-- "info" | ||
95 | <*> d >--? "announce-list" | ||
96 | <*> d >--? "comment" | ||
97 | <*> d >--? "created by" | ||
98 | <*> d >--? "creation date" | ||
99 | <*> d >--? "encoding" | ||
100 | <*> d >--? "publisher" | ||
101 | <*> d >--? "publisher-url" | ||
102 | <*> d >--? "signature" | ||
103 | |||
104 | fromBEncode _ = decodingError "Torrent" | ||
105 | |||
106 | {----------------------------------------------------------------------- | ||
107 | -- Main | ||
108 | -----------------------------------------------------------------------} | ||
109 | |||
54 | main :: IO () | 110 | main :: IO () |
55 | main = do | 111 | main = do |
56 | (path : args) <- getArgs | 112 | (path : args) <- getArgs |
57 | torrentFile <- B.readFile path | 113 | torrentFile <- BS.readFile path |
58 | let lazyTorrentFile = fromChunks [torrentFile] | 114 | let lazyTorrentFile = BL.fromChunks [torrentFile] |
59 | 115 | ||
60 | case rnf (torrentFile, lazyTorrentFile) of | 116 | case rnf (torrentFile, lazyTorrentFile) of |
61 | () -> return () | 117 | () -> return () |
@@ -100,11 +156,11 @@ main = do | |||
100 | 156 | ||
101 | , let d = BL.toStrict (C.encoded (L.replicate 10000 ())) | 157 | , let d = BL.toStrict (C.encoded (L.replicate 10000 ())) |
102 | in d `seq` (bench "list10000unit/bencoding/decode" $ nf | 158 | in d `seq` (bench "list10000unit/bencoding/decode" $ nf |
103 | (C.decoded :: B.ByteString -> Either String [()]) d) | 159 | (C.decoded :: BS.ByteString -> Either String [()]) d) |
104 | 160 | ||
105 | , let d = BL.toStrict $ C.encoded $ L.replicate 10000 (0 :: Int) | 161 | , let d = BL.toStrict $ C.encoded $ L.replicate 10000 (0 :: Int) |
106 | in d `seq` (bench "list10000int/bencoding/decode" $ nf | 162 | in d `seq` (bench "list10000int/bencoding/decode" $ nf |
107 | (C.decoded :: B.ByteString -> Either String [Int]) d) | 163 | (C.decoded :: BS.ByteString -> Either String [Int]) d) |
108 | 164 | ||
109 | , let d = L.replicate 10000 0 | 165 | , let d = L.replicate 10000 0 |
110 | in bench "list10000int/bencoding/encode>>decode" $ | 166 | in bench "list10000int/bencoding/encode>>decode" $ |
@@ -117,4 +173,12 @@ main = do | |||
117 | nf (getRight . C.decoded . BL.toStrict . C.encoded | 173 | nf (getRight . C.decoded . BL.toStrict . C.encoded |
118 | :: List Int -> List Int) | 174 | :: List Int -> List Int) |
119 | d | 175 | d |
176 | |||
177 | , let Right be = C.decode torrentFile | ||
178 | id' x = let t = either error id (fromBEncode x) | ||
179 | in toBEncode (t :: Torrent) | ||
180 | |||
181 | in bench "bigdict" $ nf | ||
182 | (appEndo $ mconcat $ L.replicate 1000 (Endo id')) | ||
183 | be | ||
120 | ] | 184 | ] |