git.iko.soy/typeable/attoparsec
on master help sign in
Add some simple HTTP header parsing benchmarks
commit15e4090dc22a4214c45855bcc3399e6cb62f31d6 authorBryan O'Sullivan <bryano@fb.com> dateThu 8 May 2014, 22:10 (12 years ago) parent7ec136c
6 files changed+84
attoparsec.cabal+2 benchmarks/Benchmarks.hs+3 benchmarks/Headers.hs+48 benchmarks/attoparsec-benchmarks.cabal+1 benchmarks/http-request.txt+14 benchmarks/http-response.txt+16
attoparsec.cabal+2
@@ -19,6 +19,7 @@ description:
1919extra-source-files:
2020 README.markdown
2121 benchmarks/*.hs
22+ benchmarks/*.txt
2223 benchmarks/Makefile
2324 benchmarks/attoparsec-benchmarks.cabal
2425 benchmarks/med.txt.bz2
@@ -93,6 +94,7 @@ benchmark benchmarks
9394 ghc-options: -O2 -Wall
9495 main-is: Benchmarks.hs
9596 other-modules:
97+ Headers
9698 Numbers
9799 build-depends:
98100 attoparsec,
benchmarks/Benchmarks.hs+3
@@ -9,6 +9,7 @@ import Data.Char (isAlpha)
99import Data.Word (Word32)
1010import Data.Word (Word8)
1111import Numbers (numbers)
12+import qualified Headers
1213import Text.Parsec.Text ()
1314import Text.Parsec.Text.Lazy ()
1415import qualified Data.Attoparsec.ByteString as AB
@@ -45,6 +46,7 @@ main = do
4546 !bl = BL.fromChunks . map BC.pack . chunksOf 4 $ s
4647 !t = T.pack s
4748 !tl = TL.fromChunks . map T.pack . chunksOf 4 $ s
49+ headers <- Headers.headers
4850 defaultMain [
4951 bgroup "many" [
5052 bgroup "attoparsec" [
@@ -82,6 +84,7 @@ main = do
8284 bench "short" $ nf (AB.parse quotedString) (BC.pack "abcdefghijk\"")
8385 , bench "long" $ nf (AB.parse quotedString) b
8486 ]
87+ , headers
8588 , numbers
8689 ]
8790
benchmarks/Headers.hsnew file+48
@@ -0,0 +1,48 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
3+module Headers (headers) where
4+
5+import Control.Applicative
6+import Criterion.Main (bench, bgroup, nf)
7+import Criterion.Types (Benchmark)
8+import Text.Parsec.Text ()
9+import Text.Parsec.Text.Lazy ()
10+import qualified Data.Attoparsec.ByteString.Char8 as B
11+import qualified Data.ByteString.Char8 as B
12+
13+header = do
14+ name <- B.takeWhile1 (B.inClass "a-zA-Z0-9_-") <* B.char ':' <* B.skipSpace
15+ body <- (:) <$> bodyLine <*> many (B.takeWhile1 B.isSpace *> bodyLine)
16+ return (name, body)
17+
18+bodyLine = B.takeTill (\c -> c == '\r' || c == '\n') <* B.endOfLine
19+
20+requestLine =
21+ (,,) <$>
22+ (method <* B.skipSpace) <*>
23+ (B.takeTill B.isSpace <* B.skipSpace) <*>
24+ httpVersion
25+ where method = "GET" <|> "POST"
26+
27+httpVersion = "HTTP/" *> ((,) <$> (int <* B.char '.') <*> int)
28+
29+responseLine = (,,) <$>
30+ (httpVersion <* B.skipSpace) <*>
31+ (int <* B.skipSpace) <*>
32+ bodyLine
33+
34+int :: B.Parser Int
35+int = B.decimal
36+
37+request = (,) <$> (requestLine <* B.endOfLine) <*> many header
38+
39+response = (,) <$> responseLine <*> many header
40+
41+headers :: IO Benchmark
42+headers = do
43+ req <- B.readFile "http-request.txt"
44+ resp <- B.readFile "http-response.txt"
45+ return $ bgroup "headers" [
46+ bench "request" $ nf (B.parseOnly request) req
47+ , bench "response" $ nf (B.parseOnly response) resp
48+ ]
benchmarks/attoparsec-benchmarks.cabal+1
@@ -9,6 +9,7 @@ build-type: Simple
99executable attoparsec-benchmarks
1010 main-is: Benchmarks.hs
1111 other-modules:
12+ Headers
1213 Numbers
1314 hs-source-dirs: .. .
1415 ghc-options: -O2 -Wall
benchmarks/http-request.txtnew file+14
@@ -0,0 +1,14 @@
1+GET / HTTP/1.1
2+Host: twitter.com
3+Accept: text/html, application/xhtml+xml, application/xml; q=0.9,
4+ image/webp, */*; q=0.8
5+Accept-Encoding: gzip,deflate,sdch
6+Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
7+Cache-Control: max-age=0
8+Cookie: guest_id=v1%3A139; _twitter_sess=BAh7CSIKZmxhc2hJQz-e1e1;
9+ __utma=43838368.452555194.1399611824.1; __utmb=43838368;
10+ __utmc=43838368; __utmz=1399611824.1.1.utmcsr=(direct)|utmcmd=(none)
11+User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5)
12+ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.86
13+ Safari/537.36
14+
benchmarks/http-response.txtnew file+16
@@ -0,0 +1,16 @@
1+HTTP/1.1 200 OK
2+Date: Fri, 09 May 2014 04:24:57 GMT
3+Expires: -1
4+Cache-Control: private, max-age=0
5+Content-Type: text/html; charset=ISO-8859-1
6+Set-Cookie: PREF=ID=1a871afc4240db5e:FF:LM=1399609497:S=e5MZ0itEekjVwNcU;
7+ expires=Sun, 08-May-2016 04:24:57 GMT; path=/; domain=.google.com
8+Set-Cookie: NID=67=-WlfaDG6VSEPk7abAjrK98HBSoCD2ID6JKkUR95tEumzDmg7Fc8pSQ8;
9+ expires=Sat, 08-Nov-2014 04:24:57 GMT; path=/; domain=.google.com; HttpOnly
10+P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
11+Server: gws
12+X-XSS-Protection: 1; mode=block
13+X-Frame-Options: SAMEORIGIN
14+Alternate-Protocol: 80:quic
15+Transfer-Encoding: chunked
16+