Skip to content

Commit

Permalink
(PE-37217) Add include-catalog-edges flag to benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
austb committed Nov 30, 2023
1 parent 1a019f1 commit 8a6c728
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
42 changes: 24 additions & 18 deletions src/puppetlabs/puppetdb/cli/benchmark.clj
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,13 @@

(def mutate-fns
"Functions that randomly change a wire-catalog format"
[catutils/add-random-resource-to-wire-catalog
catutils/mod-resource-in-wire-catalog
catutils/add-random-edge-to-wire-catalog
catutils/swap-edge-targets-in-wire-catalog])
(memoize
(fn [include-edges]
(concat [catutils/add-random-resource-to-wire-catalog
catutils/mod-resource-in-wire-catalog]
(when include-edges
[catutils/add-random-edge-to-wire-catalog
catutils/swap-edge-targets-in-wire-catalog])))))

(defn add-catalog-varying-fields
"This function adds the fields that change when there is a different
Expand All @@ -174,22 +177,22 @@

(defn rand-catalog-mutation
"Grabs one of the mutate-fns randomly and returns it"
[catalog]
[catalog include-edges]
(let [mutation-fn (comp add-catalog-varying-fields
walk/stringify-keys
(rand-nth mutate-fns))]
(rand-nth (mutate-fns include-edges)))]
(mutation-fn catalog)))

(defn update-catalog
"Slightly tweak the given catalog, returning a new catalog, `rand-percentage`
percent of the time."
[catalog rand-percentage uuid stamp]
[catalog include-edges rand-percentage uuid stamp]
(let [catalog' (assoc catalog
"producer_timestamp" stamp
"transaction_uuid" uuid
"producer" (random-producer))]
(if (< (rand 100) rand-percentage)
(rand-catalog-mutation catalog')
(rand-catalog-mutation catalog' include-edges)
catalog')))

(defn jitter
Expand Down Expand Up @@ -256,11 +259,11 @@
(defn update-host
"Perform a simulation step on host-map. Always update timestamps and uuids;
randomly mutate other data depending on rand-percentage. "
[{:keys [_host catalog report factset] :as state} rand-percentage get-timestamp]
[{:keys [_host catalog report factset] :as state} include-edges rand-percentage get-timestamp]
(let [stamp (get-timestamp)
uuid (kitchensink/uuid)]
(assoc state
:catalog (some-> catalog (update-catalog rand-percentage uuid stamp))
:catalog (some-> catalog (update-catalog include-edges rand-percentage uuid stamp))
:factset (some-> factset (update-factset rand-percentage stamp))
:report (some-> report (update-report uuid stamp)))))

Expand Down Expand Up @@ -321,7 +324,8 @@
:parse-fn #(Integer/parseInt %)]
["-o" "--offset N" "Zero based offset of cert numbers for use when running multiple instance of benchmark."
:default 0
:parse-fn #(Integer/parseInt %)]]
:parse-fn #(Integer/parseInt %)]
[nil "--include-catalog-edges" "Include catalog edges in the data submitted to PuppetDB"]]
post ["\n"
"The PERIOD (e.g. '3d') will typically be slightly in the future to account for\n"
"the time it takes to finish processing a set of historical records (so node-ttl\n"
Expand Down Expand Up @@ -519,15 +523,17 @@
(TempFileBuffer. storage-dir q)))

(defn random-hosts
[n offset pdb-host catalogs reports facts]
[n offset pdb-host include-edges catalogs reports facts]
(let [random-entity (fn [host entities]
(some-> entities
rand-nth
(assoc "certname" host)))]
(for [i (range n)]
(let [host (str "host-" (+ offset i))]
{:host host
:catalog (when-let [cat (random-entity host catalogs)]
:catalog (when-let [cat (if include-edges
(random-entity host catalogs)
(assoc (random-entity host catalogs) "edges" []))]
(update cat "resources"
(partial map #(update % "tags"
conj
Expand Down Expand Up @@ -561,7 +567,7 @@
uses numhosts and run-interval to run the simulation at a reasonable rate.
Close read-ch to terminate the background process."
[numhosts run-interval num-msgs end-commands-in rand-perc simulation-threads
write-ch read-ch]
write-ch read-ch include-edges]
(let [run-interval-minutes (time/in-minutes run-interval)
hosts-per-second (/ numhosts (* run-interval-minutes 60))
ms-per-message (/ 1000 hosts-per-second)
Expand All @@ -572,7 +578,7 @@
write-ch
(map (fn [host-state]
(let [deadline (+ (time/ephemeral-now-ns) (* ms-per-thread 1000000))
new-host (update-host host-state rand-perc progressing-timestamp-fn)]
new-host (update-host host-state include-edges rand-perc progressing-timestamp-fn)]
(when (and (not num-msgs)
(> deadline (time/ephemeral-now-ns)))
;; sleep until deadline
Expand Down Expand Up @@ -630,7 +636,7 @@
process and wait for it to stop cleanly. These functions return true if
shutdown happened cleanly, or false if there was a timeout."
[options]
(let [{:keys [config rand-perc numhosts nummsgs senders simulators end-commands-in offset]
(let [{:keys [config rand-perc numhosts nummsgs senders simulators end-commands-in offset include-catalog-edges]
:as options} options
_ (logutils/configure-logging! (get-in config [:global :logging-config]))
{:keys [catalogs reports facts]} (load-data-from-options options)
Expand Down Expand Up @@ -662,7 +668,7 @@

;; channels
initial-hosts-ch (async/to-chan!
(random-hosts numhosts offset pdb-host catalogs reports facts))
(random-hosts numhosts offset pdb-host include-catalog-edges catalogs reports facts))
mq-ch (chan (message-buffer temp-dir numhosts))
_ (register-shutdown-hook! #(async/close! mq-ch))

Expand Down Expand Up @@ -698,7 +704,7 @@
command-delay-scheduler
max-command-delay-ms)
_ (start-simulation-loop numhosts run-interval-minutes nummsgs end-commands-in rand-perc
simulators simulation-write-ch simulation-read-ch)
simulators simulation-write-ch simulation-read-ch include-catalog-edges)
join-fn (fn join-benchmark
([] (join-benchmark nil))
([timeout-ms]
Expand Down
27 changes: 26 additions & 1 deletion test/puppetlabs/puppetdb/cli/benchmark_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@
"--nummsgs" (str nummsgs))]
(is (= (* numhosts nummsgs 3) (count submitted)))))

(deftest toggle-catalog-edges
(testing "default: no catalog edges included"
(let [submitted (benchmark-nummsgs {}
"--config" "anything.ini"
"--numhosts" "2"
"--nummsgs" "3")
catalogs (filter #(= :catalog (:entity %))
submitted)]

(is (every? (fn [c] (= 0 (count (get-in c [:payload :edges]))))
catalogs))))

(testing "catalog edges included when requested"
(let [submitted (benchmark-nummsgs {}
"--config" "anything.ini"
"--numhosts" "2"
"--nummsgs" "3"
"--include-catalog-edges")
catalogs (filter #(= :catalog (:entity %))
submitted)]

(is (every? (fn [c] (< 0 (count (get-in c [:payload :edges]))))
catalogs))))
)

(deftest archive-flag-works
(let [export-out-file (.getPath (tu/temp-file "benchmark-test" ".tar.gz"))]
(svc-utils/call-with-single-quiet-pdb-instance
Expand Down Expand Up @@ -263,7 +288,7 @@
"title" "atypetitle"}
"relationship" "contains"}]
"job_id" nil}
mutated (benchmark/rand-catalog-mutation catalog)
mutated (benchmark/rand-catalog-mutation catalog true)
checked-keys (clojure.walk/walk (fn [[k _]] [k (string? k)]) identity mutated)
symbol-keys (filter (fn [[_ is-string]] (not is-string)) checked-keys)]
(is (empty? symbol-keys) "Mutating a catalog unexpectedly produced these keys as symbols instead of strings.")))

0 comments on commit 8a6c728

Please sign in to comment.