Skip to content

Clojure 1.9 CLI

Justin Conklin edited this page Aug 24, 2021 · 5 revisions

Running via the new Clojure 1.9 clojure and clj tools is generally the same as running in an unmanaged environment. We'll expand that example with a simple project below.

First we create our project directory structure.

$ mkdir -p demo-project/{classes,src/demo}
$ cd demo-project

And our deps.edn file in the root demo-project directory:

{:paths ["src" "classes"]
 :deps {jmh-clojure {:mvn/version "0.4.1"}}}

The classes directory is added to the class path via the :paths section. Alternatively, you could use an alias and :extra-paths. We keep it simple here.

Note that, like the previously mentioned example, it is vital that the classes directory exists before running or else it will not be found on the class path.

Now we'll populate our src directory. First, our core namespace in src/demo/core.clj:

(ns demo.core)

(defn spin []
  (Thread/sleep 100))

And the main runner namespace in src/demo/run.clj:

(ns demo.run
  (:require [jmh.core :as jmh]
            [clojure.edn :as edn]))

(def bench-env
  (-> "benchmarks.edn" slurp edn/read-string))

(defn -main [& [arg]]
  (let [bench-opts (edn/read-string arg)]
    (prn (jmh/run bench-env bench-opts))))

For this simple example we put the runner in our src directory. Normally, it would probably be placed in an auxiliary scripts directory or similar.

The above file reads our benchmarks from the arbitrarily chosen benchmarks.edn file. We place it in our root demo-project directory along side our deps.edn file:

{:benchmarks [demo.core/spin]}

Finally, to run our benchmarks.

$ clj -M -m demo.run '{:type :quick}'
# => ({:fn demo.core/spin #_...})

That should be a sufficient start to branch out from. Please see the Deps and CLI docs for more information.

Also, see the companion task library which can be used with tools.deps to provides some additional convenience features like sorting, table output, and more.

Clone this wiki locally