penpot/backend/dev/user.clj
Álvaro Tejero-Cantero a421cf0b60
✨ Add a warm JVM for backend tests (#12808)
Keep a test JVM running for fast backend test and evaluation cycles.
Expose the commands as MCP developer tools and report nREPL failures,
timeouts, and stale sessions reliably.

Start the lockout tests with the test configuration and document the
warm test workflow. The original changes were authored by Álvaro Tejero
Cantero.

AI-assisted-by: claude-opus-5-5
AI-assisted-by: gpt-6.1-terra
2026-11-26 17:06:24 +01:00

170 lines
4.9 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns user
(:require
[app.binfile.common :as bfc]
[app.common.data :as d]
[app.common.debug :as debug]
[app.common.exceptions :as ex]
[app.common.files.helpers :as cfh]
[app.common.fressian :as fres]
[app.common.geom.matrix :as gmt]
[app.common.json :as json]
[app.common.logging :as l]
[app.common.perf :as perf]
[app.common.pprint :as pp]
[app.common.schema :as sm]
[app.common.schema.desc-js-like :as smdj]
[app.common.schema.desc-native :as smdn]
[app.common.schema.generators :as sg]
[app.common.schema.openapi :as oapi]
[app.common.spec :as us]
[app.common.time :as ct]
[app.common.transit :as t]
[app.common.types.file :as ctf]
[app.common.uuid :as uuid]
[app.common.uri :as u]
[app.config :as cf]
[app.db :as db]
[app.main :as main]
[app.srepl.helpers :as h]
[app.srepl.main :refer :all]
[app.util.blob :as blob]
[clj-async-profiler.core :as prof]
[clojure.contrib.humanize :as hum]
[clojure.datafy :refer [datafy]]
[clojure.java.io :as io]
[clojure.pprint :refer [pprint print-table]]
[clojure.repl :refer :all]
[clojure.spec.alpha :as s]
[clojure.stacktrace :as trace]
[clojure.test :as test]
[clojure.test.check.generators :as tgen]
[clojure.tools.namespace.repl :as repl]
[clojure.walk :refer [macroexpand-all]]
[criterium.core :as crit]
[cuerdas.core :as str]
[datoteka.fs :as fs]
[integrant.core :as ig]
[malli.core :as m]
[malli.dev.pretty :as mdp]
[malli.error :as me]
[malli.generator :as mg]
[malli.registry :as mr]
[malli.transform :as mt]
[malli.util :as mu]
[promesa.exec :as px]))
(repl/disable-reload! (find-ns 'integrant.core))
(repl/disable-reload! (find-ns 'app.common.debug))
(set! *warn-on-reflection* true)
(add-tap #'debug/tap-handler)
;; --- Benchmarking Tools
(defmacro run-quick-bench
[& exprs]
`(crit/with-progress-reporting (crit/quick-bench (do ~@exprs) :verbose)))
(defmacro run-quick-bench'
[& exprs]
`(crit/quick-bench (do ~@exprs)))
(defmacro run-bench
[& exprs]
`(crit/with-progress-reporting (crit/bench (do ~@exprs) :verbose)))
(defmacro run-bench'
[& exprs]
`(crit/bench (do ~@exprs)))
;; --- Development Stuff
(def ^:private all-tests #"^backend-tests.*-test$")
(defn- run-tests
"Reload the changed namespaces, then run the tests `o` names: a regex
over namespace names, a namespace symbol, or a var symbol. Returns
the `clojure.test` counters, and throws when the reload fails."
([] (run-tests all-tests))
([o]
(let [result (repl/refresh)]
(when (instance? Throwable result)
(throw result)))
(cond
(instance? java.util.regex.Pattern o)
(test/run-all-tests o)
(symbol? o)
(if-let [sns (namespace o)]
(do (require (symbol sns))
(binding [test/*report-counters* (ref test/*initial-report-counters*)]
(test/test-vars [(resolve o)])
@test/*report-counters*))
(test/test-ns o)))))
(defn run-tests!
"Run `run-tests` on each target in turn, or on every backend test
when there is none, and throw when a test fails or errors. A caller
over nREPL, such as `scripts/test-repl`, then gets a non-zero exit."
[& targets]
(let [summary (->> (or (seq targets) [all-tests])
(mapv #(select-keys (run-tests %) [:test :pass :fail :error]))
(apply merge-with +))]
(when (pos? (+ (:fail summary 0) (:error summary 0)))
(throw (ex-info (format "%d failures, %d errors in %d tests"
(:fail summary 0) (:error summary 0) (:test summary 0))
summary)))
summary))
(defn- start
[]
(try
(main/start)
(catch Throwable cause
(ex/print-throwable cause))))
(defn- stop
[]
(main/stop))
(defn restart
[]
(main/restart))
(defn restart-all
[]
(main/restart-all))
;; (defn compression-bench
;; [data]
;; (let [humanize (fn [v] (hum/filesize v :binary true :format " %.4f "))
;; v1 (time (humanize (alength (blob/encode data {:version 1}))))
;; v3 (time (humanize (alength (blob/encode data {:version 3}))))
;; v4 (time (humanize (alength (blob/encode data {:version 4}))))
;; v5 (time (humanize (alength (blob/encode data {:version 5}))))
;; v6 (time (humanize (alength (blob/encode data {:version 6}))))
;; ]
;; (print-table
;; [{
;; :v1 v1
;; :v3 v3
;; :v4 v4
;; :v5 v5
;; :v6 v6
;; }])))
(defn calculate-frames
[{:keys [data]}]
(->> (vals (:pages-index data))
(mapcat (comp vals :objects))
(filter cfh/is-direct-child-of-root?)
(filter cfh/frame-shape?)
(count)))