11(ns ring-http-exchange.benchmark
22 (:gen-class )
33 (:require
4- [jsonista.core :as json]
54 [jj.majavat :as majavat]
65 [jj.majavat.renderer :refer [->StringRenderer]]
76 [jj.majavat.renderer.sanitizer :refer [->Html]]
8- [ring-http-exchange.core :as server]
9- [next.jdbc :as jdbc]
10- [next.jdbc.connection :as connection])
7+ [jj.sql.boa :as boa]
8+ [jsonista.core :as json]
9+ [next.jdbc.connection :as connection]
10+ [ring-http-exchange.core :as server])
1111 (:import
1212 (com.zaxxer.hikari HikariDataSource)
13- (java.util.concurrent Executors )))
13+ (io.netty.channel.epoll EpollEventLoopGroup )))
1414
15- (def db-spec
16- {:jdbcUrl " jdbc:postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass" })
15+ (def query-fortunes (boa/execute (boa/->NextJdbcAdapter ) " fortune.sql" ))
1716
18- (def datasource
19- (connection/->pool HikariDataSource db-spec))
20-
21- (defn query-fortunes []
22- (jdbc/execute! datasource
23- [" SELECT * FROM \" Fortune\" " ]
24- {:builder-fn next.jdbc.result-set/as-unqualified-lower-maps}))
17+ (def db-spec {:auto-commit true
18+ :read-only false
19+ :connection-timeout 30000
20+ :validation-timeout 5000
21+ :idle-timeout 600000
22+ :max-lifetime 1800000
23+ :minimum-idle 10
24+ :maximum-pool-size 520
25+ :minimum-pool-size 512
26+ :register-mbeans false
27+ :jdbcUrl " jdbc:postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass" })
2528
2629(def ^:private ^:const additional-message {:id 0
2730 :message " Additional fortune added at request time." })
3740 {:renderer (->StringRenderer
3841 {:sanitizer (->Html )})}))
3942
43+ (defn- get-body [datasource]
44+ (let [context (as-> (query-fortunes datasource) fortunes
45+ (conj fortunes additional-message)
46+ (sort-by :message fortunes))]
47+ (render-fortune {:messages context})))
48+
4049(defn -main
4150 [& _]
4251 (println " Starting server on port 8080" )
43- (server/run-http-server
44- (fn [req]
45- (case (req :uri )
46- " /json" {:status 200
47- :headers json-headers
48- :body (json/write-value-as-bytes {:message " Hello, World!" })}
49- " /fortunes" (let [input (as-> (query-fortunes ) fortunes
50- (conj fortunes additional-message)
51- (sort-by :message fortunes))
52- body (render-fortune {:messages input})]
53- {:status 200
54- :headers fortune-headers
55- :body body})
56- plaintext-response))
57- {:port 8080
58- :host " 0.0.0.0"
59- :executor (Executors/newVirtualThreadPerTaskExecutor )}))
52+ (let [datasource (connection/->pool HikariDataSource db-spec)]
53+ (server/run-http-server
54+ (fn [req]
55+ (case (req :uri )
56+ " /json" {:status 200
57+ :headers json-headers
58+ :body (json/write-value-as-bytes {:message " Hello, World!" })}
59+ " /fortunes" (let [body (get-body datasource)]
60+ {:status 200
61+ :headers fortune-headers
62+ :body body})
63+ plaintext-response))
64+ {:port 8080
65+ :host " 0.0.0.0"
66+ :executor (EpollEventLoopGroup. )})))
0 commit comments