Skip to content

Instantly share code, notes, and snippets.

@brdloush
Last active August 30, 2021 20:37
Show Gist options
  • Save brdloush/7970b2b1d45379ac3d30eee2470c02f1 to your computer and use it in GitHub Desktop.
Save brdloush/7970b2b1d45379ac3d30eee2470c02f1 to your computer and use it in GitHub Desktop.
Babashka script for turning JSON test report into a CSV file containing 100 slowest tests and their basic stats
;; babashka script for turning JSON test report into a simple CSV file containing 100 slowest tests
;; and their basic stats (duration, number of methods, avg per-method duration).
;;
;; (JSON report can typically be downloaded by appending `/api/json` to the testReport page URL, for example:
;; `https://jenkins.somecompany.io/job/SOME-PROJECT-build/job/master/4892/testReport/api/json`
;;
;; Usage:
;;
;; 1) download the json file
;; 2) execute `bb simplify_junit_json_testreport.clj input.json output.csv`
;; 3) ?
;; 4) profit
(ns simplify-junit-json-testreport
(:require [cheshire.core :refer [parse-string]]
[clojure.string :as str]))
(defn total-duration [cases]
(->> cases
(map :duration)
(reduce +)))
(defn simplify-cases [cases]
(->> cases
(map #(select-keys % [:className :name :duration]))))
(defn process-json [json-path output-csv-path]
(time
(do (println "Parsing JSON input from" json-path)
(let [parsed (parse-string (slurp json-path) true)
cases (->> parsed
:suites
(map :cases)
flatten)
csv-rows
(->> cases
(group-by :className)
(map (fn ([[className cases]]
(let [duration (total-duration cases)
num-cases (count cases)]
[{:className className
:duration duration
:cases num-cases
:avg-duration-per-case (/ duration num-cases)}
(simplify-cases cases)]))))
(sort-by (fn [[{:keys [duration]} _]]
duration)
>)
(take 100)
(map first)
(map (fn [{:keys [className duration cases avg-duration-per-case]}]
(str duration ";" cases ";" avg-duration-per-case ";\"" className "\""))))]
(println "Writing CSV output to" output-csv-path)
(spit output-csv-path
(->> (conj csv-rows "\"class_duration\";\"num_tests\";\"avg_duration_per_test\";\"class_name\"")
(str/join "\n")))))))
(if-let [[json-path csv-path] *command-line-args*]
(process-json json-path csv-path)
(println "Usage: bb simplify_junit_json_testreport.clj <path-to-input-json> <path-to-output-csv>"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment