Skip to content

Instantly share code, notes, and snippets.

@plexus
Created December 19, 2019 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plexus/70ccf307581d51134f853b4eaf7d886e to your computer and use it in GitHub Desktop.
Save plexus/70ccf307581d51134f853b4eaf7d886e to your computer and use it in GitHub Desktop.
(ns com.nextjournal.test.kaocha-stats
(:require [kaocha.plugin :as plugin :refer [defplugin]]
[kaocha.testable :as testable]
[kaocha.hierarchy :as hierarchy]
[clojure.java.io :as io]
[java-time]
[clojure.edn :as edn])
(:import [java.io PushbackReader]))
(defn test-stats [result]
(for [testable (testable/test-seq result)
:when (hierarchy/leaf? testable)]
{:id (::testable/id testable)
:duration (:kaocha.plugin.profiling/duration testable)
:result (cond
(< 0 (:kaocha.result/error testable)) :error
(< 0 (:kaocha.result/fail testable)) :fail
(< 0 (:kaocha.result/pending testable)) :pending
(< 0 (:kaocha.result/pass testable)) :pass
:else :skip)}))
(defn fast-tests [last-run threshold]
(let [sorted-tests (sort-by :duration last-run)]
(->> sorted-tests
(map :duration)
(reductions + 0)
(zipmap sorted-tests)
(filter (fn [[_test cumulative-duration]]
(< cumulative-duration (* threshold 1e9))))
(map (comp :id first))
(set))))
(def STATS_DIR (io/file ".kaocha-stats"))
(defn read-edn-forms [f]
(with-open [in (PushbackReader. (io/reader f))]
(doall (take-while identity (repeatedly #(read in false nil))))))
(defn latest-stats-file []
(->> STATS_DIR
file-seq
(filter #(re-find #"20\d\d.*\.edn" (str %)))
sort
last))
(defn latest-stats []
(when-let [file (latest-stats-file)]
(read-edn-forms file)))
(defplugin :com.nextjournal.test/kaocha-stats
(post-run [result]
(.mkdir (io/file ".kaocha-stats"))
(let [start (:kaocha.plugin.profiling/start result)
fname (str ".kaocha-stats/" start ".edn")]
(with-open [out (io/writer (io/file fname))]
(binding [*out* out]
(run! prn (test-stats result)))))
result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment