Main.hs (4107B)
1 {-# OPTIONS_GHC -fno-warn-orphans #-} 2 {-# LANGUAGE OverloadedStrings #-} 3 4 -- Benchmarks after those of the taproot-assets SQL-backed tree store 5 -- (tapdb/mssmt_bench_test.go): inserting a batch of leaves into a fresh 6 -- store in one transaction, one at a time or batched, and proving keys 7 -- in populated stores, each in its own read transaction. 8 9 module Main where 10 11 import Control.DeepSeq 12 import Control.Exception (evaluate) 13 import qualified Crypto.Hash.SHA256 as SHA256 14 import qualified Data.Array as A 15 import qualified Data.ByteString as BS 16 import qualified Data.ByteString.Char8 as B8 17 import Data.IORef 18 import Data.MSSMT.Internal (Commitment, Hash, Key, Neighbor, Scheme(..)) 19 import qualified Data.MSSMT.Internal as M 20 import qualified Data.MSSMT.LMDB.Internal as I 21 import Data.Word (Word64) 22 import qualified Database.LMDB as L 23 import Criterion.Main 24 import System.Directory (getTemporaryDirectory, removeFile) 25 import System.IO (hClose, openTempFile) 26 27 instance NFData I.Store where 28 rnf = rwhnf 29 30 instance NFData Hash where 31 rnf = rwhnf 32 33 instance NFData Key where 34 rnf = rwhnf 35 36 instance NFData Commitment where 37 rnf = rwhnf 38 39 instance NFData Neighbor where 40 rnf = rwhnf 41 42 -- n leaves, after tapdb's benchRandLeaves: pseudorandom keys, values of 43 -- 1 to 255 bytes, and 32-bit sums 44 leaves :: Int -> [(Key, BS.ByteString, Word64)] 45 leaves n = fmap leaf [1 .. n] where 46 leaf i = 47 let h = SHA256.hash (B8.pack (show i)) 48 v = BS.take (1 + fromIntegral (BS.index h 0) `mod` 255) . BS.concat $ 49 [SHA256.hash (h <> B8.pack (show j)) | j <- [0 .. 7 :: Int]] 50 s = fromIntegral (BS.foldl' (\a w -> a * 256 + fromIntegral w) 51 (0 :: Word64) (BS.take 4 h)) 52 in case M.key h of 53 Just k -> (k, v, s) 54 Nothing -> error "bang" 55 56 -- a fresh store under a scheme, in a temporary file 57 fresh :: Bool -> Scheme -> IO (FilePath, I.Store) 58 fresh synced sc = do 59 tmp <- getTemporaryDirectory 60 (path, h) <- openTempFile tmp "mssmt-lmdb-bench.mdb" 61 hClose h 62 st <- I.open sc path L.defaultEnvFlags { 63 L.envMapSize = 1024 * 1024 * 1024 64 , L.envNoSubdir = True 65 , L.envNoSync = not synced 66 } 67 pure (path, st) 68 69 discard :: (FilePath, I.Store) -> IO () 70 discard (path, st) = do 71 I.close st 72 removeFile path 73 removeFile (path <> "-lock") 74 75 main :: IO () 76 main = defaultMain [ 77 bgroup "insert, fresh store" [ 78 insert_bench synced sc n 79 | n <- [100, 1000], synced <- [True, False], sc <- [Split, Aggregate] 80 ] 81 , bgroup "prove" [ 82 prove_bench sc n 83 | n <- [1000, 10000, 100000], sc <- [Split, Aggregate] 84 ] 85 ] 86 87 scheme_name :: Scheme -> String 88 scheme_name Split = "split" 89 scheme_name Aggregate = "aggregate" 90 91 insert_bench :: Bool -> Scheme -> Int -> Benchmark 92 insert_bench synced sc n = env (pure (leaves n)) $ \ls -> 93 bgroup (scheme_name sc <> ", batch=" <> show n <> sync_name) [ 94 bench "one at a time" $ perRunEnvWithCleanup (fresh synced sc) discard $ 95 \(_, st) -> L.withWriteTxn (I.store_env st) $ \txn -> 96 mapM_ (\(k, v, s) -> I.insert_in st txn "tree" k v s) ls 97 , bench "batched" $ perRunEnvWithCleanup (fresh synced sc) discard $ 98 \(_, st) -> () <$ I.insert_many st "tree" ls 99 ] 100 where 101 sync_name = if synced then "" else ", unsynced" 102 103 prove_bench :: Scheme -> Int -> Benchmark 104 prove_bench sc n = envWithCleanup populated (discard . fst) $ \ ~(_, ks) -> 105 bench (scheme_name sc <> ", leaves=" <> show n) $ 106 whnfIO (next ks) 107 where 108 populated = do 109 fs@(_, st) <- fresh False sc 110 let ls = leaves n 111 _ <- I.insert_many st "tree" ls 112 ref <- newIORef (0 :: Int) 113 let arr = A.listArray (0, n - 1) [k | (k, _, _) <- ls] 114 _ <- evaluate (force arr) 115 pure (fs, (st, arr, ref)) 116 117 next (st, arr, ref) = do 118 i <- atomicModifyIORef' ref (\j -> (j + 1, j)) 119 let k = arr A.! (i `mod` n) 120 case sc of 121 Split -> I.prove_split st "tree" k >>= evaluate . force 122 >>= \(_, ps) -> pure (length ps) 123 Aggregate -> I.prove_aggregate st "tree" k >>= evaluate . force 124 >>= pure . length