packages feed

picoparsec-0.1: benchmarks/HeadersText.hs

{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
module HeadersText (headers) where

import Control.Applicative
import Criterion.Main (bench, bgroup, nf)
import Criterion.Types (Benchmark)
import Data.Char (isSpace)
import qualified Data.Attoparsec.Text as T
import qualified Data.Text.IO as T

header = do
  name <- T.takeWhile1 (T.inClass "a-zA-Z0-9_-") <* T.char ':' <* T.skipSpace
  body <- (:) <$> bodyLine <*> many (T.takeWhile1 isSpace *> bodyLine)
  return (name, body)

bodyLine = T.takeTill (\c -> c == '\r' || c == '\n') <* T.endOfLine

requestLine =
    (,,) <$>
    (method <* T.skipSpace) <*>
    (T.takeTill isSpace <* T.skipSpace) <*>
    httpVersion
  where method = "GET" <|> "POST"

httpVersion = "HTTP/" *> ((,) <$> (int <* T.char '.') <*> int)

responseLine = (,,) <$>
               (httpVersion <* T.skipSpace) <*>
               (int <* T.skipSpace) <*>
               bodyLine

int :: T.Parser Int
int = T.decimal

request = (,) <$> (requestLine <* T.endOfLine) <*> many header

response = (,) <$> responseLine <*> many header

headers :: IO Benchmark
headers = do
  req <- T.readFile "http-request.txt"
  resp <- T.readFile "http-response.txt"
  return $ bgroup "headersT" [
      bench "request" $ nf (T.parseOnly request) req
    , bench "response" $ nf (T.parseOnly response) resp
    ]