Skip to content

Commit

Permalink
mv bench examples into their own ns
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpancakes committed Feb 20, 2024
1 parent ccf88d4 commit e05b655
Show file tree
Hide file tree
Showing 5 changed files with 412 additions and 328 deletions.
224 changes: 224 additions & 0 deletions dev/bench/chassis.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
(ns bench.chassis
(:require [dev.onionpancakes.chassis.core :as c]
[dev.onionpancakes.chassis.compiler :as cc]))

(defmethod c/resolve-alias ::Item
[_ _ {item ::item :as attrs} content]
[:div.item (merge {:id (:uuid item)
:class (:type item)} attrs)
[:h2 (:name item)]
[:p (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p (:text item)]])

(defmethod c/resolve-alias ::ItemCompiled
[_ _ {item ::item :as attrs} content]
(cc/compile
[:div.item (merge {:id (:uuid item)
:class (:type item)} attrs)
[:h2 (:name item)]
[:p (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p (:text item)]]))

(defmethod c/resolve-alias ::Layout
[_ _ {title ::title :as attrs} content]
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title title]]
[:body
[:header
[:h1 title]]
[:main attrs content]
[:footer "Footer"]]])

(defmethod c/resolve-alias ::LayoutCompiled
[_ _ {title ::title :as attrs} content]
(cc/compile
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title title]]
[:body
[:header
[:h1 title]]
[:main attrs content]
[:footer "Footer"]]]))

(defn item-element
[item]
[:div.item {:id (:uuid item)
:class (:type item)}
[:h2 (:name item)]
[:p (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p (:text item)]])

(defn item-element-compiled
[item]
(cc/compile
[:div.item {:id (:uuid item)
:class (:type item)}
[:h2 (:name item)]
[:p (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p (:text item)]]))

(defn item-element-compiled-unambig
[item]
(cc/compile
[:div.item {:id (:uuid item)
:class (:type item)}
[:h2 nil (:name item)]
[:p nil (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p nil (:text item)]]))

(defn page
[data]
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title (:title data)]]
[:body
[:header
[:h1 (:title data)]]
[:main
(->> (:items data)
(map item-element)
(interpose [:hr]))]
[:footer "Footer"]]])

(defn page-doall
[data]
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title (:title data)]]
[:body
[:header
[:h1 (:title data)]]
[:main
(->> (:items data)
(map item-element)
(interpose [:hr])
(doall))]
[:footer "Footer"]]])

(defn page-alias
[data]
[::Layout {::title (:title data)}
(->> (:items data)
(map #(vector ::Item {::item %}))
(interpose [:hr]))])

(defn page-alias-compiled
[data]
(cc/compile
[::LayoutCompiled {::title (:title data)}
(->> (:items data)
(map #(cc/compile [::ItemCompiled {::item %}]))
(interpose (cc/compile [:hr])))]))

(defn page-compiled
[data]
(cc/compile
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title (:title data)]]
[:body
[:header
[:h1 (:title data)]]
[:main
(->> (:items data)
(map item-element-compiled)
(interpose (cc/compile [:hr])))]
[:footer "Footer"]]]))

(defn page-compiled-unambig
[data]
(cc/compile
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title nil (:title data)]]
[:body
[:header
[:h1 nil (:title data)]]
[:main nil
(->> (:items data)
(map item-element-compiled-unambig)
(interpose (cc/compile [:hr])))]
[:footer "Footer"]]]))

(defn chassis-page
[data]
(c/html [c/doctype-html5 (page data)]))

(defn chassis-page-alias
[data]
(c/html [c/doctype-html5 (page-alias data)]))

(defn chassis-page-alias-compiled
[data]
(c/html [c/doctype-html5 (page-alias-compiled data)]))

(defn chassis-page-compiled
[data]
(c/html [c/doctype-html5 (page-compiled data)]))

(defn chassis-page-compiled-unambig
[data]
(c/html [c/doctype-html5 (page-compiled-unambig data)]))

(defn chassis-page-print-writer
[data]
(let [out (java.io.ByteArrayOutputStream. 16384)
charset (java.nio.charset.Charset/forName "UTF-8")]
(with-open [wtr (java.io.PrintWriter. out false charset)]
(c/write-html wtr [c/doctype-html5 (page data)]))
out))

(defn chassis-page-print-stream
[data]
(let [out (java.io.ByteArrayOutputStream. 16384)]
(with-open [pout (java.io.PrintStream. out false "UTF-8")]
(c/write-html pout [c/doctype-html5 (page data)]))
out))

(defn chassis-page-output-stream-writer
[data]
(let [out (java.io.ByteArrayOutputStream. 16384)]
(with-open [pout (java.io.OutputStreamWriter. out "UTF-8")]
(c/write-html pout [c/doctype-html5 (page data)]))
out))
50 changes: 50 additions & 0 deletions dev/bench/enlive.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns bench.enlive
(:require [net.cgrand.enlive-html :as enlive]))

(defn item-element
[item]
[:div {:id (:uuid item)
:class (str "item " (:type item))}
[:h2 (:name item)]
[:p (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p (:text item)]])

(enlive/deftemplate item-element-template "enlive/item.html"
[item]
[:.item] (comp (enlive/add-class (:type item))
(enlive/set-attr :id (:uuid item)))
[:.name] (enlive/content (:name item))
[:.date] (enlive/content (str (:date item)))
[:a] (enlive/set-attr :href (str "/item/" (:uuid item)))
[:.description] (enlive/content (:text item)))

(enlive/deftemplate page-template-item-html "enlive/page.html"
[data]
[:head :title] (enlive/content (:title data))
[:body :header :h1] (enlive/content (:title data))
[:body :main] (enlive/content
(->> (:items data)
(map (comp enlive/html item-element))
(interpose (enlive/html [:hr]))))
[:body :footer] (enlive/content "Footer"))

(enlive/deftemplate page-template-item-template "enlive/page.html"
[data]
[:head :title] (enlive/content (:title data))
[:body :header :h1] (enlive/content (:title data))
[:body :main] (enlive/content
(->> (:items data)
(map item-element-template)
(interpose (enlive/html [:hr]))))
[:body :footer] (enlive/content "Footer"))

(defn enlive-page-item-html
[data]
(apply str (page-template-item-html data)))

(defn enlive-page-item-template
[data]
(apply str (page-template-item-template data)))
118 changes: 118 additions & 0 deletions dev/bench/hiccup.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
(ns bench.hiccup
(:require [hiccup2.core :as hiccup]
[hiccup.page :as hiccup.page]))

(defn item-element
[item]
[:div.item {:id (:uuid item)
:class (:type item)}
[:h2 (:name item)]
[:p (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p (:text item)]])

(defn item-element-compiled
[item]
(hiccup/html
[:div.item {:id (:uuid item)
:class (:type item)}
[:h2 (:name item)]
[:p (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p (:text item)]]))

(defn item-element-compiled-unambig
[item]
(hiccup/html
[:div.item {:id (:uuid item)
:class (:type item)}
[:h2 nil (:name item)]
[:p nil (:date item)]
[:p [:a.baz.buz {:href (str "/item/" (:uuid item))}
"See more details."]]
[:h3 "Description"]
[:p nil (:text item)]]))

(defn page
[data]
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title (:title data)]]
[:body
[:header
[:h1 (:title data)]]
[:main
(->> (:items data)
(map item-element)
(interpose (hiccup/html [:hr])))]
[:footer "Footer"]]])

(defn page-compiled
[data]
(hiccup/html
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title (:title data)]]
[:body
[:header
[:h1 (:title data)]]
[:main
(->> (:items data)
(map item-element-compiled)
(interpose (hiccup/html [:hr])))]
[:footer "Footer"]]]))

(defn page-compiled-unambig
[data]
(hiccup/html
[:html {:lang "en"}
[:head
[:link {:href "/foobar1" :rel "stylesheet"}]
[:link {:href "/foobar2" :rel "stylesheet"}]
[:link {:href "/foobar3" :rel "stylesheet"}]
[:link {:href "/foobar4" :rel "stylesheet"}]
[:title nil (:title data)]]
[:body
[:header
[:h1 nil (:title data)]]
[:main nil
(->> (:items data)
(map item-element-compiled-unambig)
(interpose (hiccup/html [:hr])))]
[:footer "Footer"]]]))

(defn hiccup-page
[data]
(str
(hiccup/html
{:mode :html}
(hiccup.page/doctype :html5)
(page data))))

(defn hiccup-page-compiled
[data]
(str
(hiccup/html
{:mode :html}
(hiccup.page/doctype :html5)
(page-compiled data))))

(defn hiccup-page-compiled-unambig
[data]
(str
(hiccup/html
{:mode :html}
(hiccup.page/doctype :html5)
(page-compiled-unambig data))))
Loading

0 comments on commit e05b655

Please sign in to comment.