Skip to content

Commit

Permalink
[core] refactor benchmark setup (as bench.core ns), add cljs benchmarks,
Browse files Browse the repository at this point in the history
update cljs dep
  • Loading branch information
postspectacular committed Mar 16, 2015
1 parent 6598437 commit 3d5c313
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 79 deletions.
68 changes: 68 additions & 0 deletions geom-core/bench/core.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#+SETUPFILE: ../../src/setup.org

* Contents :toc_4_gh:
- [[#shared-cljcljs-benchmark-helpers][Shared CLJ/CLJS benchmark helpers]]
- [[#clojure][Clojure]]
- [[#clojurescript][Clojurescript]]

* Shared CLJ/CLJS benchmark helpers

** Clojure

These macros acts as wrappers for perforate's =defgoal= & =defcase=
macros in order to allow for the same syntax of both the CLJ & CLJS
versions of benchmark specs.

#+BEGIN_SRC clojure :tangle ../babel/benchmarks/thi/ng/geom/bench/core.clj :noweb yes :mkdirp yes :padline no
(ns thi.ng.geom.bench.core
(:require
[perforate.core :as perf]))

(defmacro defgoal
[goal doc]
`(perf/defgoal ~(symbol (name goal)) ~doc))

(defmacro defcase
[goal id args form]
(let [form (last `(list ~@form))]
`(perf/defcase ~(symbol (name goal)) ~id ~args ~form)))
#+END_SRC

** Clojurescript

#+BEGIN_SRC clojure :tangle ../babel/benchmarks/thi/ng/geom/bench/core.cljs :noweb yes :mkdirp yes :padline no
(ns thi.ng.geom.bench.core
(:require
[cljs.nodejs :as nodejs]))

(def Benchmark
(nodejs/require "benchmark"))

(def goals (atom {}))

(defn defgoal
[id & _]
(swap! goals assoc id
(.. (.Suite Benchmark)
(on "cycle" (fn [e] (.log js/console (js/String (.-target e)))))
(on "complete"
(fn [e]
(this-as
*this*
(let [fastest (-> *this*
(.filter "fastest")
(.pluck "name")
(aget 0))]
(.log js/console "Fastest is" fastest))))))))

(defn defcase
[goal id _ f]
(swap! goals update-in [goal] #(.add % (name id) f)))

(defn run-goals
[]
(doseq [[id goal] @goals]
(.log js/console "Goal:" (name id))
(.run goal))
(.exit js/process))
#+END_SRC
169 changes: 93 additions & 76 deletions geom-core/bench/vector.org
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [[#constants][Constants]]
- [[#vec2][Vec2]]
- [[#vec3][Vec3]]
- [[#complete-namespace-definition][Complete namespace definition]]
- [[#complete-namespace-definitions][Complete namespace definitions]]

* Vector benchmarks

Expand All @@ -30,132 +30,149 @@
** Vec2

#+BEGIN_SRC clojure :noweb-ref bench
(defgoal vec2-ops "vec2-ops")
(defgoal :vec2-ops "vec2-ops")

(defcase vec2-ops :add-v
[] (g/+ A2 B2))
(defcase :vec2-ops :add-v
[] #(g/+ A2 B2))

(defcase vec2-ops :add-vv
[] (g/+ A2 B2 C2))
(defcase :vec2-ops :add-vv
[] #(g/+ A2 B2 C2))

(defcase vec2-ops :add-n
[] (g/+ A2 N))
(defcase :vec2-ops :add-n
[] #(g/+ A2 N))

(defcase vec2-ops :add-nn
[] (g/+ A2 N M))
(defcase :vec2-ops :add-nn
[] #(g/+ A2 N M))

(defcase vec2-ops :add-s
[] (g/+ A2 [N M]))
(defcase :vec2-ops :add-s
[] #(g/+ A2 [N M]))

(defcase vec2-ops :scale-v
[] (g/* A2 B2))
(defcase :vec2-ops :scale-v
[] #(g/* A2 B2))

(defcase vec2-ops :scale-n
[] (g/* A2 N))
(defcase :vec2-ops :scale-n
[] #(g/* A2 N))

(defcase vec2-ops :madd
[] (g/madd A2 N B2))
(defcase :vec2-ops :madd
[] #(g/madd A2 N B2))

(defcase vec2-ops :madd-op2
[] (g/* (g/+ A2 N) B2))
(defcase :vec2-ops :madd-op2
[] #(g/* (g/+ A2 N) B2))

(defcase vec2-ops :dot
[] (g/dot A2 B2))
(defcase :vec2-ops :dot
[] #(g/dot A2 B2))

(defcase vec2-ops :normalize
[] (g/normalize A2))
(defcase :vec2-ops :normalize
[] #(g/normalize A2))

(defcase vec2-ops :mag
[] (g/mag A2))
(defcase :vec2-ops :mag
[] #(g/mag A2))

(defcase vec2-ops :mag-squared
[] (g/mag-squared A2))
(defcase :vec2-ops :mag-squared
[] #(g/mag-squared A2))

(defcase vec2-ops :mix
[] (g/mix A2 B2 0.5))
(defcase :vec2-ops :mix
[] #(g/mix A2 B2 0.5))

(defcase vec2-ops :mix-bi
[] (g/mix A2 B2 C2 D2 0.5 0.5))
(defcase :vec2-ops :mix-bi
[] #(g/mix A2 B2 C2 D2 0.5 0.5))

(defcase vec2-ops :rotate
[] (g/rotate A2 HALF_PI))
(defcase :vec2-ops :rotate
[] #(g/rotate A2 HALF_PI))
#+END_SRC

** Vec3

#+BEGIN_SRC clojure :noweb-ref bench
(defgoal vec3-ops "vec3-ops")
(defgoal :vec3-ops "vec3-ops")

(defcase vec3-ops :add-v
[] (g/+ A3 B3))
(defcase :vec3-ops :add-v
[] #(g/+ A3 B3))

(defcase vec3-ops :add-vv
[] (g/+ A3 B3 C3))
(defcase :vec3-ops :add-vv
[] #(g/+ A3 B3 C3))

(defcase vec3-ops :add-n
[] (g/+ A3 N))
(defcase :vec3-ops :add-n
[] #(g/+ A3 N))

(defcase vec3-ops :add-nnn
[] (g/+ A3 N M O))
(defcase :vec3-ops :add-nnn
[] #(g/+ A3 N M O))

(defcase vec3-ops :add-s
[] (g/+ A3 [N M O]))
(defcase :vec3-ops :add-s
[] #(g/+ A3 [N M O]))

(defcase vec3-ops :scale-v
[] (g/* A3 B3))
(defcase :vec3-ops :scale-v
[] #(g/* A3 B3))

(defcase vec3-ops :scale-n
[] (g/* A3 N))
(defcase :vec3-ops :scale-n
[] #(g/* A3 N))

(defcase vec3-ops :madd
[] (g/madd A3 N B3))
(defcase :vec3-ops :madd
[] #(g/madd A3 N B3))

(defcase vec3-ops :madd-op2
[] (g/* (g/+ A3 N) B3))
(defcase :vec3-ops :madd-op2
[] #(g/* (g/+ A3 N) B3))

(defcase vec3-ops :dot
[] (g/dot A3 B3))
(defcase :vec3-ops :dot
[] #(g/dot A3 B3))

(defcase vec3-ops :cross
[] (g/cross A3 B3))
(defcase :vec3-ops :cross
[] #(g/cross A3 B3))

(defcase vec3-ops :ortho-normal
[] (gu/ortho-normal A3 B3 C3))
(defcase :vec3-ops :ortho-normal
[] #(gu/ortho-normal A3 B3 C3))

(defcase vec3-ops :normalize
[] (g/normalize A3))
(defcase :vec3-ops :normalize
[] #(g/normalize A3))

(defcase vec3-ops :mag
[] (g/mag A3))
(defcase :vec3-ops :mag
[] #(g/mag A3))

(defcase vec3-ops :mag-squared
[] (g/mag-squared A3))
(defcase :vec3-ops :mag-squared
[] #(g/mag-squared A3))

(defcase vec3-ops :mix
[] (g/mix A3 B3 0.5))
(defcase :vec3-ops :mix
[] #(g/mix A3 B3 0.5))

(defcase vec3-ops :mix-bi
[] (g/mix A3 B3 C3 D3 0.5 0.5))
(defcase :vec3-ops :mix-bi
[] #(g/mix A3 B3 C3 D3 0.5 0.5))

(defcase vec3-ops :rotate-x
[] (g/rotate-x A3 HALF_PI))
(defcase :vec3-ops :rotate-x
[] #(g/rotate-x A3 HALF_PI))

(defcase vec3-ops :rotate-axis
[] (g/rotate-around-axis A3 v/V3Y HALF_PI))
(defcase :vec3-ops :rotate-axis
[] #(g/rotate-around-axis A3 v/V3Y HALF_PI))
#+END_SRC

** Complete namespace definition
** Complete namespace definitions

#+BEGIN_SRC clojure :tangle ../babel/benchmarks/thi/ng/geom/bench/core/vector.clj :noweb yes :mkdirp yes :padline no
(ns thi.ng.geom.bench.core.vector
(:require
[perforate.core :refer :all]
[perforate.core]
[thi.ng.geom.core :as g]
[thi.ng.geom.core.utils :as gu]
[thi.ng.geom.core.vector :as v :refer [vec2 vec3]]
[thi.ng.common.math.core :as m :refer [*eps* HALF_PI PI]]))
[thi.ng.common.math.core :as m :refer [*eps* HALF_PI PI]]
[thi.ng.geom.bench.core :refer [defgoal defcase]]))

<<helpers>>

<<bench>>
#+END_SRC

#+BEGIN_SRC clojure :tangle ../babel/benchmarks/thi/ng/geom/bench/core/vector.cljs :noweb yes :mkdirp yes :padline no
(ns thi.ng.geom.bench.core.vector
(:require
[thi.ng.geom.core :as g]
[thi.ng.geom.core.utils :as gu]
[thi.ng.geom.core.vector :as v :refer [vec2 vec3]]
[thi.ng.common.math.core :as m :refer [*eps* HALF_PI PI]]
[thi.ng.geom.bench.core :refer [defcase defgoal run-goals]]))

<<helpers>>

<<bench>>

(run-goals)
#+END_SRC
11 changes: 9 additions & 2 deletions geom-core/src/index.org
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,17 @@ target/geom-core-<<conf-version()>>.js
:output-path "<<conf-gen-test-path>>"
:rules :cljs}]}

:cljsbuild {:builds [{:source-paths ["<<conf-gen-source-path>>" "<<conf-gen-test-path>>"]
:id "simple"
:cljsbuild {:builds [{:id "simple"
:source-paths ["<<conf-gen-source-path>>" "<<conf-gen-test-path>>"]
:compiler {:output-to "<<cljs-artefact-path>>"
:optimizations :whitespace
:pretty-print true}}
{:id "bench"
:source-paths ["<<conf-gen-source-path>>" "<<conf-gen-test-path>>" "benchmarks"]
:notify-command ["node" "target/cljs/benchmark.js"]
:compiler {:target :nodejs
:output-to "target/cljs/benchmark.js"
:optimizations :simple
:pretty-print true}}]
:test-commands {"unit-tests" ["phantomjs" :runner "<<cljs-artefact-path>>"]}}

Expand Down
2 changes: 1 addition & 1 deletion src/config.org
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ target/test-classes
**** [[https://github.com/clojure/clojurescript][ClojureScript]]
#+NAME: dep-cljs
#+BEGIN_SRC clojure
[org.clojure/clojurescript "0.0-2913"]
[org.clojure/clojurescript "0.0-3117"]
#+END_SRC

**** [[https://github.com/thi-ng/common/][thi.ng/common]]
Expand Down

0 comments on commit 3d5c313

Please sign in to comment.