Skip to content

Commit 5b70bca

Browse files
committed
Initial commit for benchmarks
- Extracted out code for creating a connection to the test DB - Added ‘benchmark’ section/target to cabal file
1 parent bdeb800 commit 5b70bca

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

Test/Test.hs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,16 @@ import qualified Data.Time as Time
2323
import qualified Data.Aeson as Json
2424
import qualified Data.Text as T
2525

26-
import System.Environment (lookupEnv)
27-
28-
import Control.Applicative ((<$>), (<*>), (<|>))
26+
import Control.Applicative ((<$>), (<*>))
2927
import qualified Control.Applicative as A
3028
import qualified Control.Arrow as Arr
3129
import Control.Arrow ((&&&), (***), (<<<), (>>>))
3230

3331
import GHC.Int (Int64)
32+
import TestConnection
3433

3534
import Test.Hspec
3635

37-
import qualified Configuration.Dotenv as Dotenv
38-
3936
{-
4037
4138
Status
@@ -914,24 +911,7 @@ jsonbTests = [testJsonGetFieldValue table9Q,testJsonGetFieldText table9Q,
914911

915912
main :: IO ()
916913
main = do
917-
let envVarName = "POSTGRES_CONNSTRING"
918-
919-
connectStringEnvVar <- lookupEnv envVarName
920-
921-
connectStringDotEnv <- do vars <- Dotenv.parseFile ".env"
922-
return (lookup envVarName vars)
923-
`Dotenv.onMissingFile`
924-
return Nothing
925-
926-
let connectString = connectStringEnvVar <|> connectStringDotEnv
927-
928-
conn <- maybe
929-
(fail ("Set " ++ envVarName ++ " environment variable\n"
930-
++ "For example " ++ envVarName ++ "='user=tom dbname=opaleye_test "
931-
++ "host=localhost port=25433 password=tom'"))
932-
(PGS.connectPostgreSQL . String.fromString)
933-
connectString
934-
914+
conn <- getTestDbConnection
935915
dropAndCreateDB conn
936916

937917
let insert (writeable, columndata) =

Test/TestConnection.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module TestConnection where
2+
3+
import qualified Database.PostgreSQL.Simple as PGS
4+
import System.Environment
5+
import qualified Configuration.Dotenv as Dotenv
6+
import qualified Data.String as String
7+
import Control.Applicative ((<|>))
8+
9+
getTestDbConnection :: IO PGS.Connection
10+
getTestDbConnection = do
11+
let envVarName = "POSTGRES_CONNSTRING"
12+
connectStringEnvVar <- lookupEnv envVarName
13+
connectStringDotEnv <- do vars <- Dotenv.parseFile ".env"
14+
return (lookup envVarName vars)
15+
`Dotenv.onMissingFile`
16+
return Nothing
17+
let connectString = connectStringEnvVar <|> connectStringDotEnv
18+
conn <- maybe
19+
(fail ("Set " ++ envVarName ++ " environment variable\n"
20+
++ "For example " ++ envVarName ++ "='user=tom dbname=opaleye_test "
21+
++ "host=localhost port=25433 password=tom'"))
22+
(PGS.connectPostgreSQL . String.fromString)
23+
connectString
24+
pure conn

benchmarks/Main.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Main where
2+
3+
import Opaleye.RunQuery
4+
import Criterion.Main
5+
import TestConnection
6+
7+
main :: IO ()
8+
main = do
9+
conn <- getTestDbConnection
10+
pure ()

opaleye.cabal

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ test-suite test
9898
type: exitcode-stdio-1.0
9999
main-is: Test.hs
100100
other-modules: QuickCheck
101+
, TestConnection
101102
hs-source-dirs: Test
102103
build-depends:
103104
aeson >= 0.6 && < 1.3,
@@ -138,3 +139,20 @@ test-suite tutorial
138139
time,
139140
opaleye
140141
ghc-options: -Wall
142+
143+
test-suite benchmark
144+
default-language: Haskell2010
145+
type: exitcode-stdio-1.0
146+
main-is: Main.hs
147+
other-modules: TestConnection
148+
hs-source-dirs: benchmarks
149+
, Test
150+
build-depends: base >= 4 && < 5
151+
, postgresql-simple
152+
, profunctors
153+
, product-profunctors >= 0.6
154+
, time
155+
, opaleye
156+
, criterion
157+
, dotenv
158+
ghc-options: -Wall

0 commit comments

Comments
 (0)