Skip to content

Instantly share code, notes, and snippets.

@holyjak
Last active November 4, 2018 12:00
Show Gist options
  • Save holyjak/c4a88135bc08703d6351754980537055 to your computer and use it in GitHub Desktop.
Save holyjak/c4a88135bc08703d6351754980537055 to your computer and use it in GitHub Desktop.
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