Skip to content

Commit

Permalink
Reitit + plaintest & db (#4691)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi authored and NateBrady23 committed Apr 28, 2019
1 parent 2915539 commit 8e53a43
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
4 changes: 3 additions & 1 deletion frameworks/Clojure/reitit/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"tests": [{
"default": {
"json_url": "/json",
"plaintext_url": "/plaintext",
"db_url": "/db",
"port": 8080,
"approach": "Realistic",
"classification": "Micro",
"database": "None",
"database": "Postgres",
"framework": "reitit",
"language": "Clojure",
"flavor": "None",
Expand Down
7 changes: 5 additions & 2 deletions frameworks/Clojure/reitit/project.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
(defproject hello "reitit"
:description "Immutant, Reitit, Jsonista"
:description "Immutant-nio, Reitit, Jsonista & Porsas"
:dependencies [[org.clojure/clojure "1.10.0"]
[ikitommi/immutant-web "3.0.0-alpha1"]
[metosin/jsonista "0.2.2"]
[metosin/reitit "0.2.12"]]
[hikari-cp "2.7.1"]
[org.postgresql/postgresql "42.2.5"]
[metosin/porsas "0.0.1-alpha4"]
[metosin/reitit "0.3.1"]]
:jvm-opts ^:replace ["-Dclojure.compiler.direct-linking=true"]
:main hello.handler
:aot :all)
68 changes: 56 additions & 12 deletions frameworks/Clojure/reitit/src/hello/handler.clj
Original file line number Diff line number Diff line change
@@ -1,24 +1,68 @@
(ns hello.handler
(:require [immutant.web :as web]
[hikari-cp.core :as hikari]
[reitit.ring :as ring]
[porsas.core :as p]
[jsonista.core :as j])
(:gen-class))
(:gen-class)
(:import (java.util.concurrent ThreadLocalRandom)
(io.undertow.server HttpServerExchange)))

(defn blocking [handler]
(fn [req]
(.startBlocking ^HttpServerExchange (:server-exchange req))
(handler req)))

(defn random []
(unchecked-inc (.nextInt (ThreadLocalRandom/current) 10000)))

(def query-one (:query-one (p/compile {:row (p/rs->compiled-record)})))

(defn random-world [ds]
(with-open [con (p/get-connection ds)]
(query-one con ["SELECT id, randomnumber from WORLD where id=?" (random)])))

(defn plain-text-handler [_]
{:status 200
:headers {"content-type" "text/plain; charset=utf-8"}
:body (.getBytes "Hello, World!")})

(defn json-handler [_]
{:status 200
:headers {"Content-Type" "application/json"}
:body (j/write-value-as-bytes {:message "Hello, World!"})})

(def app
(ring/ring-handler
(ring/router ["/json" json-handler])
(ring/create-default-handler)
{:inject-match? false, :inject-router? false}))
(defn db-handler [ds]
(fn [_]
{:status 200
:headers {"Content-Type" "application/json"}
:body (j/write-value-as-bytes (random-world ds))}))

(defn -main [& _]
(web/run
app
{:port 8080
:host "0.0.0.0"
:dispatch? false
:server {:always-set-keep-alive false}}))
(let [ds (hikari/make-datasource
{:read-only true
:maximum-pool-size 64
:pool-name "db-pool"
:adapter "postgresql"
:username "benchmarkdbuser"
:password "benchmarkdbpass"
:database-name "hello_world"
:server-name "tfb-database"
:port-number 5432
:register-mbeans false})]
(web/run
(ring/ring-handler
(ring/router
[["/plaintext" plain-text-handler]
["/json" json-handler]
["/db" (blocking (db-handler ds))]])
(ring/create-default-handler)
{:inject-match? false, :inject-router? false})
{:port 8080
:host "0.0.0.0"
:dispatch? false
:worker-threads 64
:server {:always-set-keep-alive false}})))

(comment
(-main))
7 changes: 0 additions & 7 deletions frameworks/Clojure/reitit/src/log4j.xml

This file was deleted.

4 changes: 4 additions & 0 deletions frameworks/Clojure/reitit/src/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<root level="ERROR"/>
</configuration>

0 comments on commit 8e53a43

Please sign in to comment.