Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Incanter - prepare usage data
(ns clj-charting.usage-chart-preparation
(:require
[incanter.core :refer :all]
[incanter.stats :as s]
[incanter.io :as io]))
(defn- resolve-unit-suffix
"Replace values such as 333k, 800m, 1.2g with the corresponding value in bytes"
[val-suffixed]
(if-let [[_ val unit] (and
(string? val-suffixed)
(re-find #"(\d+)([kmg])" val-suffixed))]
(let [order (case unit
"k" 1
"m" 2
"g" 3)
scale (apply * (take order (repeat 1024)))]
(* (Integer/parseInt val) scale))))
(defn read-usage-data
"Read usage data in the form `sec.ns memory_with_scale_suffix CPU_percentage` into a dataset with
`ms memory_in_bytes CPU_percentage`"
[file]
(let [data (io/read-dataset
file
:delim \space)]
(-> data
;; Memory: from 300m or 1g to a number:
(transform-col
:col1
resolve-unit-suffix)
;; CPU: From <sec>.<nano> to <ms>:
(transform-col
:col0
#(long (* 1000 %))))))
(defn moving-window-means
"Given very scattered data, produce a similar sequence of 'moving window mean' where we
replace each point by the mean of it and the preceding/following `radius` points.
"
[radius col]
(let [x' (concat (repeat radius nil) col)
parts (drop-last radius (partition (inc (* 2 radius)) 1 x'))
means (map #(-> (remove nil? %) s/mean long)
parts)]
means))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.