diff options
Diffstat (limited to 'bench/Main.hs')
-rw-r--r-- | bench/Main.hs | 87 |
1 files changed, 51 insertions, 36 deletions
diff --git a/bench/Main.hs b/bench/Main.hs index 4953a06..0259b3c 100644 --- a/bench/Main.hs +++ b/bench/Main.hs | |||
@@ -1,7 +1,9 @@ | |||
1 | {-# LANGUAGE PackageImports #-} | 1 | {-# LANGUAGE PackageImports #-} |
2 | {-# LANGUAGE DeriveGeneric #-} | 2 | {-# LANGUAGE DeriveGeneric #-} |
3 | {-# LANGUAGE RecordWildCards #-} | 3 | {-# LANGUAGE RecordWildCards #-} |
4 | {-# LANGUAGE OverloadedStrings #-} | 4 | {-# LANGUAGE OverloadedStrings #-} |
5 | {-# LANGUAGE DeriveDataTypeable #-} | ||
6 | {-# LANGUAGE BangPatterns #-} | ||
5 | module Main (main) where | 7 | module Main (main) where |
6 | 8 | ||
7 | import Control.Applicative | 9 | import Control.Applicative |
@@ -12,6 +14,7 @@ import qualified Data.ByteString.Lazy as BL | |||
12 | import Data.List as L | 14 | import Data.List as L |
13 | import Data.Maybe | 15 | import Data.Maybe |
14 | import Data.Monoid | 16 | import Data.Monoid |
17 | import Data.Typeable | ||
15 | import System.Environment | 18 | import System.Environment |
16 | 19 | ||
17 | import Criterion.Main | 20 | import Criterion.Main |
@@ -23,6 +26,8 @@ import Data.AttoBencode.Parser as B | |||
23 | import "bencoding" Data.BEncode as C | 26 | import "bencoding" Data.BEncode as C |
24 | import "bencoding" Data.BEncode.Internal as C | 27 | import "bencoding" Data.BEncode.Internal as C |
25 | import "bencoding" Data.BEncode.Types as C | 28 | import "bencoding" Data.BEncode.Types as C |
29 | import Debug.Trace | ||
30 | |||
26 | 31 | ||
27 | instance NFData A.BEncode where | 32 | instance NFData A.BEncode where |
28 | rnf (A.BInt i) = rnf i | 33 | rnf (A.BInt i) = rnf i |
@@ -62,47 +67,47 @@ replicate' c x | |||
62 | 67 | ||
63 | data Torrent = Torrent { | 68 | data Torrent = Torrent { |
64 | tAnnounce :: !ByteString | 69 | tAnnounce :: !ByteString |
65 | , tInfo :: !BDict | ||
66 | , tAnnounceList :: !(Maybe ByteString) | 70 | , tAnnounceList :: !(Maybe ByteString) |
67 | , tComment :: !(Maybe ByteString) | 71 | , tComment :: !(Maybe ByteString) |
68 | , tCreatedBy :: !(Maybe ByteString) | 72 | , tCreatedBy :: !(Maybe ByteString) |
69 | , tCreationDate :: !(Maybe ByteString) | 73 | , tCreationDate :: !(Maybe ByteString) |
70 | , tEncoding :: !(Maybe ByteString) | 74 | , tEncoding :: !(Maybe ByteString) |
75 | , tInfo :: !BDict | ||
71 | , tPublisher :: !(Maybe ByteString) | 76 | , tPublisher :: !(Maybe ByteString) |
72 | , tPublisherURL :: !(Maybe ByteString) | 77 | , tPublisherURL :: !(Maybe ByteString) |
73 | , tSignature :: !(Maybe ByteString) | 78 | , tSignature :: !(Maybe ByteString) |
74 | } deriving (Show, Eq) | 79 | } deriving (Show, Eq, Typeable) |
75 | 80 | ||
76 | instance NFData Torrent where | 81 | instance NFData Torrent where |
77 | rnf Torrent {..} = () | 82 | rnf Torrent {..} = () |
78 | 83 | ||
79 | instance C.BEncode Torrent where | 84 | instance C.BEncode Torrent where |
80 | toBEncode Torrent {..} = fromAscAssocs | 85 | toBEncode Torrent {..} = toDict $ |
81 | [ "announce" --> tAnnounce | 86 | "announce" .=! tAnnounce |
82 | , "announce-list" -->? tAnnounceList | 87 | C..: "announce-list" .=? tAnnounceList |
83 | , "comment" -->? tComment | 88 | C..: "comment" .=? tComment |
84 | , "created by" -->? tCreatedBy | 89 | C..: "created by" .=? tCreatedBy |
85 | , "creation date" -->? tCreationDate | 90 | C..: "creation date" .=? tCreationDate |
86 | , "encoding" -->? tEncoding | 91 | C..: "encoding" .=? tEncoding |
87 | , "info" --> tInfo | 92 | C..: "info" .=! tInfo |
88 | , "publisher" -->? tPublisher | 93 | C..: "publisher" .=? tPublisher |
89 | , "publisher-url" -->? tPublisherURL | 94 | C..: "publisher-url" .=? tPublisherURL |
90 | , "signature" -->? tSignature | 95 | C..: "signature" .=? tSignature |
91 | ] | 96 | C..: endDict |
92 | 97 | ||
93 | fromBEncode (C.BDict d) = | 98 | |
94 | Torrent <$> d >-- "announce" | 99 | |
95 | <*> d >-- "info" | 100 | fromBEncode = fromDict $ do |
96 | <*> d >--? "announce-list" | 101 | Torrent <$>! "announce" |
97 | <*> d >--? "comment" | 102 | <*>? "announce-list" |
98 | <*> d >--? "created by" | 103 | <*>? "comment" |
99 | <*> d >--? "creation date" | 104 | <*>? "created by" |
100 | <*> d >--? "encoding" | 105 | <*>? "creation date" |
101 | <*> d >--? "publisher" | 106 | <*>? "encoding" |
102 | <*> d >--? "publisher-url" | 107 | <*>! "info" |
103 | <*> d >--? "signature" | 108 | <*>? "publisher" |
104 | 109 | <*>? "publisher-url" | |
105 | fromBEncode _ = decodingError "Torrent" | 110 | <*>? "signature" |
106 | 111 | ||
107 | {----------------------------------------------------------------------- | 112 | {----------------------------------------------------------------------- |
108 | -- Main | 113 | -- Main |
@@ -175,11 +180,21 @@ main = do | |||
175 | :: List Int -> List Int) | 180 | :: List Int -> List Int) |
176 | d | 181 | d |
177 | 182 | ||
178 | , let Right be = C.parse torrentFile | 183 | , let Right !be = C.parse torrentFile |
179 | id' x = let t = either error id (fromBEncode x) | 184 | id' x = let t = either error id (fromBEncode x) |
180 | in toBEncode (t :: Torrent) | 185 | in toBEncode (t :: Torrent) |
186 | !test = let Right t = C.decode torrentFile | ||
187 | in if C.decode (BL.toStrict (C.encode t)) | ||
188 | /= Right (t :: Torrent) | ||
189 | then error "invalid instance: BEncode Torrent" | ||
190 | else True | ||
191 | |||
192 | replFn n f = go n | ||
193 | where go 0 = id | ||
194 | go n = f . go (pred n) | ||
195 | |||
196 | in bench "bigdict" $ nf (replFn (1000 :: Int) id') be | ||
181 | 197 | ||
182 | in bench "bigdict" $ nf | 198 | , let fn x = let Right t = C.decode x in t :: Torrent |
183 | (appEndo $ mconcat $ L.replicate 1000 (Endo id')) | 199 | in bench "torrent/decode" $ nf fn torrentFile |
184 | be | ||
185 | ] | 200 | ] |