on 0b1aece help sign in
typeable/uuid/tests/BenchUUID.hs
52 lines 1.8 kB history raw
1import Control.Monad (replicateM)
2import Control.Parallel.Strategies
3import Criterion.Main
4import Data.Char (ord)
5import Data.Maybe (fromJust)
6import Data.Word
7import qualified Data.ByteString.Lazy as BL
8import qualified Data.ByteString.Lazy.Internal as BL
9import qualified Data.UUID as U
10import qualified Data.UUID.Internal as U
11import qualified Data.UUID.V1 as U
12import qualified Data.UUID.V5 as U
13import System.Random
16instance NFData BL.ByteString where
17 rnf BL.Empty = ()
18 rnf (BL.Chunk _ ts) = rnf ts
20instance NFData U.UUID where
21 rnf (U.UUID tl tm th ch cl node) =
22 rnf tl `seq` rnf tm `seq` rnf th `seq` rnf ch `seq` rnf cl `seq` rnf node
24instance NFData U.Node where
25 rnf (U.Node w1 w2 w3 w4 w5 w6) =
26 rnf w1 `seq` rnf w2 `seq` rnf w3 `seq` rnf w4 `seq` rnf w5 `seq` rnf w6
30main :: IO ()
31main = do
32 u1 <- randomIO
33 let s1 = U.toString u1
34 b1 = U.toByteString u1
35 n1 = (map (fromIntegral . ord) "http://www.haskell.org/") :: [Word8]
36 defaultMain [
37 bgroup "null" [
38 bench "null" $ B U.null u1,
39 bench "null nil" $ B U.null U.nil
40 ],
41 bgroup "conversion" [
42 bench "toString" $ B (rnf . U.toString) u1,
43 bench "fromString" $ B (rnf . U.fromString) s1,
44 bench "toByteString" $ B (rnf . U.toByteString) u1,
45 bench "fromByteString" $ B (rnf . U.fromByteString) b1
46 ],
47 bgroup "generation" [
48 bench "V1" $ rnf `fmap` U.nextUUID,
49 bench "V4" $ rnf `fmap` (randomIO :: IO U.UUID),
50 bench "V5" $ B (rnf . U.generateNamed U.namespaceURL) n1
51 ]
52 ]