valmikroy
2/6/2018 - 7:07 PM

R snippets

R programming snippets

haproxy <- read.csv("some.csv")
options(digits.secs = 3)
haproxy$date<-as.POSIXct(haproxy$timestamp-28800,origin="1970-01-01",tz="UTC")
head(haproxy,10)


# find out qps , count of records per time series 
# per second split 
haproxy_qps <- data.frame(table(cut(haproxy_date, breaks="sec")))

# plot it 
plot(haproxy_qps,type="l")


# filter data based on filed value 
haproxy_pl <- subset(haproxy,type=='idx')


# plot qps
plot(table(cut(haproxy_ts$date, breaks="sec")),type="l")


# uniq values in col
unique(haproxy$pid)

# loop through pids 
for (p in unique(haproxy$pid) ) { print (summary(subset(haproxy,pid==p)$type) ) }

#doing math of col vals and adding new col with results
haproxy$req_lat <- (haproxy$Tt - haproxy$Ti)

plot(haproxy$date,haproxy$req_lat,type="l")


# percentage of requests with various HTTP responses
 x <- data.frame(table(hp_before$status))
 # add column names
 colnames(x) <- c('status','freq')
 # do the math 
 x$perc <- x$freq / sum(x$freq) *100
 
 
 # rename existing column name
library(data.table)
setnames(d, old = c('a','d'), new = c('anew','dnew'))




# various quantiles
with_numa_Ta = with_numa$Ta
quantile(with_numa_Ta, c(.10,.50,.75,.90,.99,.999))
#  10%   50%   75%   90%   99% 99.9% 
#   4     7    14   265   398  1577