Skip to content

Instantly share code, notes, and snippets.

@escherize
Created January 17, 2024 22:32
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 escherize/0362dc876bdf0e7445b9baa89caa5a7a to your computer and use it in GitHub Desktop.
Save escherize/0362dc876bdf0e7445b9baa89caa5a7a to your computer and use it in GitHub Desktop.
(ns dev.nocommit.massive-perm-graph
(:require [clojure.string :as str]
[criterium.core :as criterium]
[metabase.models.permissions :as perms]
[metabase.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.util.log :as log]
[toucan2.core :as t2]))
(comment
;; create groups:
(defn create-perm-groups! []
(doseq [name (map #(format "Group %d" %) (range 50))]
(log/warn "Creating" name "...")
(t2/insert-returning-instances! :model/PermissionsGroup :name name)))
(defn create-dbs! [path]
;; create "databases" (all db instances point to our mysql db:)
(doseq [name (map #(format "My DB %d" %) (range 200))]
(log/warn "Creating" name "...")
(with-out-str
(mt/user-http-request :crowberto :post 200 "database"
{:name name,
:engine "sqlite",
:details {:db path, :advanced-options false},
:is_full_sync true,
:is_on_demand false,
:schedules {},
:auto_run_queries true,
:cache_ttl nil}))
1))
;; partially fill in the graph:
(defn fill! []
(let [pg (perms/data-perms-graph)
admin-group-id (:id (perms-group/admin))
admin-perms (dissoc (get-in pg [:groups admin-group-id]) 13371337)
group-id (rand-nth (t2/select-fn-vec :id :model/PermissionsGroup))]
(mt/user-http-request :crowberto :put 200 "permissions/graph"
{:revision (:revision pg)
:groups {group-id admin-perms}})
{:filled group-id}))
(defn compare! []
[(str/trim (with-out-str (time (with-out-str (mt/user-http-request :crowberto :get 200 "permissions/graph") :ok))))
(str/trim (with-out-str (time (with-out-str (mt/user-http-request :crowberto :get 200 "permissions/graph/db/8") :ok))))])
(defn setup! []
;; make a mydb.db file in metabase repo dir
;; for me it's:
(def path "/Users/bcm/dv/mb/metabase/mydb.db")
(time (create-perm-groups!))
(time (create-dbs! path)))
(setup!)
(compare!) ;; 8x
;; call fill! a few times, and see compare! output again.
(compare!) ;; 50x
["\"Elapsed time: 347.461458 msecs\""
"\"Elapsed time: 5.301666 msecs\""]
(fill!)
(fill!)
(fill!)
(compare!) ;;91x
;; => ["\"Elapsed time: 551.3195 msecs\"" "\"Elapsed time: 6.215791 msecs\""]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment