Skip to content

Commit

Permalink
Added sablono.benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
r0man committed Dec 22, 2013
1 parent 0ec4d0e commit e41505d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
12 changes: 8 additions & 4 deletions project.clj
Expand Up @@ -8,7 +8,9 @@
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/clojurescript "0.0-2127"]]
:profiles {:dev {:dependencies [[com.keminglabs/cljx "0.3.1"]]
:profiles {:dev {:dependencies [[com.keminglabs/cljx "0.3.1"]
[crate "0.2.3" :scope "dev"]
[prismatic/dommy "0.1.1"]]
:plugins [[com.cemerick/austin "0.1.3"]]
:repl-options {:nrepl-middleware [cljx.repl-middleware/wrap-cljx]}}}
:plugins [[com.cemerick/clojurescript.test "0.2.1"]
Expand All @@ -31,9 +33,11 @@
:source-paths ["test" "target/classes" "target/test-classes"]
:compiler {:output-to "target/test/sablono.js"
:output-dir "target/test"
:optimizations :advanced
:optimizations :whitespace
:pretty-print true
:preamble ["sablono/react-with-addons-0.8.0.min.js"]
:preamble ["jquery.js"
"phantomjs-shims.js"
"sablono/react-with-addons-0.8.0.min.js"]
:externs ["sablono/externs/react.js"]
:closure-warnings {:non-standard-jsdoc :off}}}
{:id "dev"
Expand All @@ -45,5 +49,5 @@
:source-map true
:externs ["sablono/externs/react.js"]
:closure-warnings {:non-standard-jsdoc :off}}}]
:test-commands {"phantom" ["phantomjs" :runner "test-resources/phantomjs-shims.js" "target/test/sablono.js"]}}
:test-commands {"phantom" ["phantomjs" :runner "target/test/sablono.js"]}}
:test-paths ["test" "target/test-classes"])
2 changes: 1 addition & 1 deletion test.sh
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
phantomjs test-resources/runner.js test-resources/phantomjs-shims.js target/test/sablono.js
phantomjs test-resources/runner.js target/test/sablono.js
5 changes: 5 additions & 0 deletions test/jquery.js

Large diffs are not rendered by default.

File renamed without changes.
91 changes: 91 additions & 0 deletions test/sablono/benchmark.cljs
@@ -0,0 +1,91 @@
(ns sablono.benchmark
(:require-macros [cemerick.cljs.test :refer [is deftest testing]]
[dommy.macros :refer [node]]
[sablono.core :refer [html]])
(:require [crate.core :as crate]
[dommy.template :as template]
[goog.dom :as gdom]
[sablono.test :refer [body]]))

;; From Prismatic's dommy

(defn dommy-template [datum]
(template/node
[:li [:a {:href (str "#show/" (:key datum))}
[:div.class1.class2 {:id (str "item" (:key datum))}
[:span.anchor (:name datum)]]]]))

(defn dommy-compiled [datum]
(node
[:li [:a {:href (str "#show/" (:key datum))}
[:div.class1.class2 {:id (str "item" (:key datum))}
[:span.anchor (:name datum)]]]]))

(defn crate-template [datum]
(crate/html
[:li [:a {:href (str "#show/" (:key datum))}]
[:div {:id (str "item" (:key datum))
:class ["class1" "class2"]}
[:span {:class "anchor"} (:name datum)]]]))

(defn sablono-template [datum]
(html
[:li [:a {:href (str "#show/" (:key datum))}]
[:div {:id (str "item" (:key datum))
:className ["class1" "class2"]}
[:span {:className "anchor"} (:name datum)]]]))

(defn jquery-template [datum]
(-> "<li>" js/jQuery
(.append
(-> "<a>" js/jQuery
(.attr "href" (str "#show/" (:key datum)))
(.addClass "anchor")
(.append (-> "<div>" js/jQuery
(.addClass "class1")
(.addClass "class2")
(.attr "id" (str "item" (:key datum)))
(.append (-> "<span>" js/jQuery (.text (:name datum))))))))))

(defn run-test [root data li-fn render-fn]
(let [now (js/Date.)]
(render-fn root (map li-fn data))
(/ (- (.getTime (js/Date.))
(.getTime now)) 1000)))

(defn gen-data []
(for [i (range 1e4)]
{:key (rand-int 1e6)
:name (str "product" i)}))

(defn render-append [root children]
(let [ul (goog.dom/createDom "ul")]
(doseq [child children]
(goog.dom/append ul child))
(goog.dom/append root ul)))

(defn render-react [root children]
(let [render-fn #(this-as this (html [:ul children]))
component (js/React.createClass #js {:render render-fn})]
(js/React.renderComponent (component) root)))

(defn time-test [data]
(for [[key li-fn render-fn]
(shuffle
[[:jquery jquery-template render-append]
[:crate crate-template render-append]
[:dommy dommy-template render-append]
[:dommy-compiled dommy-compiled render-append]
[:sablono sablono-template render-react]])]
(let [root (goog.dom/createDom "div")
_ (goog.dom/append (body) root)
secs (run-test root data li-fn render-fn)]
[key secs])))

(deftest perf-test []
(let [data (doall (gen-data))]
(prn (->> (for [i (range 3)]
(into {} (time-test data)))
(reduce (partial merge-with +))
(map (fn [[k v]] [k (/ v 3)]))
(into {})))))

0 comments on commit e41505d

Please sign in to comment.