<commit_before>{-# LANGUAGE PackageImports #-}

module Main where

import Control.Monad (replicateM)
import Data.ByteString.Lazy (ByteString)
import Data.Binary (decode, encode)
import Data.Binary.Get (runGet)
import Data.Binary.Put (runPut)
import System.Random (StdGen, newStdGen)

import Criterion.Main (defaultMain, bench, nf)
import Test.QuickCheck (arbitrary, resize)
import Test.QuickCheck.Gen (unGen)
import "bresson" Data.Bson.Binary ()
import "bresson" Data.Bson.Tests.Instances ()
import qualified "bresson" Data.Bson as Bresson
import qualified "bson" Data.Bson.Binary as Bson

testBresson :: [ByteString] -> [ByteString]
testBresson = map (encode . (decode :: ByteString -> Bresson.BsonDocument))

testBson :: [ByteString] -> [ByteString]
testBson = map (runPut . Bson.putDocument . runGet Bson.getDocument)

generate :: Int -> Int -> IO ByteString
generate size seed = newStdGen >>= \gen ->
    return $ encode $ mkDoc gen
  where
    mkDoc :: StdGen -> Bresson.BsonDocument
    mkDoc gen = unGen (resize size arbitrary) gen seed

main :: IO ()
main = do
    testData <- replicateM 100 $ generate 100 42
    defaultMain [ bench "bson" $ nf testBson testData
                , bench "bresson" $ nf testBresson testData
                ]
<commit_msg>Support for GHC 7.4<commit_after>{-# LANGUAGE PackageImports #-}

module Main where

import Control.Monad (replicateM)
import Data.ByteString.Lazy (ByteString)
import Data.Binary (decode, encode)
import Data.Binary.Get (runGet)
import Data.Binary.Put (runPut)
import System.Random (StdGen, newStdGen)

import Criterion.Main (defaultMain, bench, nf)
import Test.QuickCheck (arbitrary, resize)
import Test.QuickCheck.Gen (unGen)
import "bresson" Data.Bson ()  -- NFData instance for ByteString if missing
import "bresson" Data.Bson.Binary ()
import "bresson" Data.Bson.Tests.Instances ()
import qualified "bresson" Data.Bson as Bresson
import qualified "bson" Data.Bson.Binary as Bson

testBresson :: [ByteString] -> [ByteString]
testBresson = map (encode . (decode :: ByteString -> Bresson.BsonDocument))

testBson :: [ByteString] -> [ByteString]
testBson = map (runPut . Bson.putDocument . runGet Bson.getDocument)

generate :: Int -> Int -> IO ByteString
generate size seed = newStdGen >>= \gen ->
    return $ encode $ mkDoc gen
  where
    mkDoc :: StdGen -> Bresson.BsonDocument
    mkDoc gen = unGen (resize size arbitrary) gen seed

main :: IO ()
main = do
    testData <- replicateM 100 $ generate 100 42
    defaultMain [ bench "bson" $ nf testBson testData
                , bench "bresson" $ nf testBresson testData
                ]

{'fraction_non_alphanumeric': 0.10974673829623945, 'fraction_numerical': 0.006907137375287797, 'mean_word_length': 3.9003759398496243, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2817926', 'n_tokens_mistral': 871, 'n_tokens_neox': 840, 'n_words': 254}