github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/metrics/report/report_dockerfile/mem-in-cont.R (about)

     1  #!/usr/bin/env Rscript
     2  # Copyright (c) 2018 Intel Corporation
     3  #
     4  # SPDX-License-Identifier: Apache-2.0
     5  
     6  # Analyse the runtime component memory footprint data.
     7  
     8  library(ggplot2)					# ability to plot nicely.
     9  							# So we can plot multiple graphs
    10  library(gridExtra)					# together.
    11  suppressMessages(suppressWarnings(library(ggpubr)))	# for ggtexttable.
    12  suppressMessages(library(jsonlite))			# to load the data.
    13  
    14  testnames=c(
    15  	"memory-footprint-inside-container"
    16  )
    17  
    18  data=c()
    19  rstats=c()
    20  rstats_rows=c()
    21  rstats_cols=c()
    22  
    23  # For each set of results
    24  for (currentdir in resultdirs) {
    25  	dirstats=c()
    26  	# For the two different types of memory footprint measures
    27  	for (testname in testnames) {
    28  		# R seems not to like double path slashes '//' ?
    29  		fname=paste(inputdir, currentdir, testname, '.json', sep="")
    30  		if ( !file.exists(fname)) {
    31  			warning(paste("Skipping non-existent file: ", fname))
    32  			next
    33  		}
    34  
    35  		# Derive the name from the test result dirname
    36  		datasetname=basename(currentdir)
    37  
    38  		# Import the data
    39  		fdata=fromJSON(fname)
    40  		fdata=fdata[[testname]]
    41  		# Copy the average result into a shorter, more accesible name
    42  		fdata$requested=fdata$Results$memrequest$Result
    43  		fdata$total=fdata$Results$memtotal$Result
    44  		fdata$free=fdata$Results$memfree$Result
    45  		fdata$avail=fdata$Results$memavailable$Result
    46  
    47  		# And lets work out what % we have 'lost' between the amount requested
    48  		# and the total the container actually sees.
    49  		fdata$lost=fdata$requested - fdata$total
    50  		fdata$pctotal= 100 * (fdata$lost/ fdata$requested)
    51  
    52  		fdata$Runtime=rep(datasetname, length(fdata$Result) )
    53  
    54  		# Store away the bits we need
    55  		data=rbind(data, data.frame(
    56  			Result=fdata$requested,
    57  			Type="requested",
    58  			Runtime=fdata$Runtime ))
    59  			
    60  		data=rbind(data, data.frame(
    61  			Result=fdata$total,
    62  			Type="total",
    63  			Runtime=fdata$Runtime ))
    64  			
    65  		data=rbind(data, data.frame(
    66  			Result=fdata$free,
    67  			Type="free",
    68  			Runtime=fdata$Runtime ))
    69  
    70  		data=rbind(data, data.frame(
    71  			Result=fdata$avail,
    72  			Type="avail",
    73  			Runtime=fdata$Runtime ))
    74  
    75  		data=rbind(data, data.frame(
    76  			Result=fdata$lost,
    77  			Type="lost",
    78  			Runtime=fdata$Runtime ))
    79  
    80  		data=rbind(data, data.frame(
    81  			Result=fdata$pctotal,
    82  			Type="% consumed",
    83  			Runtime=fdata$Runtime ))
    84  
    85  		# Store away some stats for the text table
    86  		dirstats=rbind(dirstats, round(fdata$requested, digits=2) )
    87  		dirstats=rbind(dirstats, round(fdata$total, digits=2) )
    88  		dirstats=rbind(dirstats, round(fdata$free, digits=2) )
    89  		dirstats=rbind(dirstats, round(fdata$avail, digits=2) )
    90  		dirstats=rbind(dirstats, round(fdata$lost, digits=2) )
    91  		dirstats=rbind(dirstats, round(fdata$pctotal, digits=2) )
    92  	}
    93  	rstats=cbind(rstats, dirstats)
    94  	rstats_cols=append(rstats_cols, datasetname)
    95  }
    96  
    97  rstats_rows=c("Requested", "Total", "Free", "Avail", "Consumed", "% Consumed")
    98  
    99  unts=c("Kb", "Kb", "Kb", "Kb", "Kb", "%")
   100  rstats=cbind(rstats, unts)
   101  rstats_cols=append(rstats_cols, "Units")
   102  
   103  # If we have only 2 sets of results, then we can do some more
   104  # stats math for the text table
   105  if (length(resultdirs) == 2) {
   106  	# This is a touch hard wired - but we *know* we only have two
   107  	# datasets...
   108  	diff=c()
   109  	# Just the first three entries - meaningless for the pctotal entry
   110  	for (n in 1:5) {
   111  		difference = (as.double(rstats[n,2]) - as.double(rstats[n,1]))
   112  		val = 100 * (difference/as.double(rstats[n,1]))
   113  		diff=rbind(diff, round(val, digits=2))
   114  	}
   115  
   116  	# Add a blank entry for the other entries
   117  	diff=rbind(diff, "")
   118  	rstats=cbind(rstats, diff)
   119  	rstats_cols=append(rstats_cols, "Diff %")
   120  }
   121  
   122  # Build us a text table of numerical results
   123  stats_plot = suppressWarnings(ggtexttable(data.frame(rstats),
   124  	theme=ttheme(base_size=10),
   125  	rows=rstats_rows, cols=rstats_cols
   126  	))
   127  
   128  bardata <- subset(data, Type %in% c("requested", "total", "free", "avail"))
   129  # plot how samples varioed over  'time'
   130  barplot <- ggplot() +
   131  	geom_bar(data=bardata, aes(Type, Result, fill=Runtime), stat="identity", position="dodge") +
   132  	xlab("Measure") +
   133  	ylab("Size (Kb)") +
   134  	ggtitle("In-container memory statistics") +
   135  	ylim(0, NA) +
   136  	theme(axis.text.x=element_text(angle=90))
   137  
   138  master_plot = grid.arrange(
   139  	barplot,
   140  	stats_plot,
   141  	nrow=2,
   142  	ncol=1 )
   143