% performance class

The performance model tool is developed in the package {\ttfamily it.polito.policyorchestration.impl.\linebreak optimization.performance} and it allows representing the performance of a filtering point in the\break deploy architecture. In the thesis' economy this class is useful to map each filtering point with a\break specific value that could be used to optimize the distribution of the rules all over the deployment.

Performance tool produce the output as the following workflow:

\begin{itemize}
	\item 
	reads the filtering point parameters;
	\item
	setups of the internal parameters (e.g. \code{coefficient, maxRules});
	\item
	for each filtering point saves the number of the rules injected;
	\item
	creates the function of the performance based on the previous parameters
	\item
	computes the performance function through \code{getPerformance()} method and gives a number or an expression as output.
\end{itemize}


\subsection{Tool architecture}

The tool architecture is made up of an interface and an abstract class that implements the previous interface.
It is extended by other three classes and there is an object factory to get the instances, the structure of the classes are the following:

\begin{itemize}
	\item
	\code{class PerformanceToolFactory}: it is the class that handles the creation of the\break {\ttfamily PerformanceTool instances}, based on the metric chosen in the input files. It has no instance variables except the one that identify the metric used, but it defines an enum \code{public enum deviceType} that allows identifying what kind of filtering point the instance refers to. The Object Factory has the following methods:
	\begin{itemize}
		\item 
		\code{public PerformanceToolFactory(PerformanceTypeElement metric)}\newline
		constructor method that save inside the Object the metric used to evaluate the\break performance;	
		\item
		\code{public Performance getInstance(deviceType device, double offset, double coefficient, int nMaxRules)}\newline
		this method allows to create and to get a Performance instance in which it needs to be specified the type of the device, the maximum number of rules that are allowed to be injected, the offset and the coefficient. 
		These last two variables are related to the performance function, the former is the value of the performance when there are no rules and the second one is the rate between the performance after \code{100} rules and the performance with no rules.
	\end{itemize}

	\item 
	\code{interface PerformanceTool}: it is the interface implemented by all the Performance\break Objects, the following methods are available:
	\begin{itemize}
		\item 
		\code{double getPerformance()}\newline
		it returns the number that represents the current performance of the filtering point;
		\item
		\code{double getPerformance(int numRules)}\newline
		it returns the number that represents the future performance of the filtering points after additional \code{numRules} rules;
		\item
		\code{ArithExpr getPerformance(Context ctx, ArithExpr numRules)}\newline
		it returns the arithmetic expression that is compute based on the arithmetic expression in input;
		\item
		\code{void addNumRules(int numRules)}\newline
		it adds \code{numRules} rules at the Performance instance;
		\item
		\code{boolean isFull()}\newline
		it returns a boolean value that represents if the filtering point cannot accept any additional rule;
		\item
		\code{int remainsNumRules()}\newline
		it returns the possible number of rules that could be hosted by the Performance Object;
		\item
		\code{deviceType getDeviceType()}\newline
		it returns the type of the filtering point;
		\item
		\code{int compareDevice(Performance p)}\newline
		it compares the current instance to the \code{p} instance;
		\item
		\code{int getMaxNumRules()}\newline
		it returns the maximum number of rules that the device could hosts.
	\end{itemize}

	\item
	\code{abstract class PerformanceType}:	this class is an abstract class, it defines all the instance variables in common between all the specific performance type. In this class are implemented all the function that allows the framework to interact with the Performance Object.
	The instance variables are:
	\begin{itemize}
		\item 
		\code{protected deviceType device}\newline
		it identifies the type of the filtering point, this allow filling with default value the following variables when they are not set in input files;
		\item 
		\code{protected double coefficient}\newline
		it represents how fast the performance degrade based on the number of rules;
		\item 
		\code{protected double offset}\newline
		it represents the initial performance of the filtering point when there are no rules;
		\item 
		\code{protected int maxRules}\newline
		it is the maximum number of rules that is possible to host;
		\item 
		\code{protected int currentRules}\newline
		it is the current number of rules that are hosted in the filtering point;
		\item 
		\code{private static int MAXRULES}\newline
		it is a static variable that fix the maximum default number to \code{100000} rules.
	\end{itemize}
		
	This class has no possibility to represent a specific metric, because it handles only shared methods expressed in the interface, so it is needed to extend this class to implements the performance function.
	\begin{itemize}
		\item 
		\code{class PerformanceLatency}: this class it represent an evaluation of the performance based on the latency in the communication. It defines default values for coefficient and offset, one for each \code{deviceType} and a linear function expressed in the two following methods:
		\begin{itemize}
			\item 
			\textit{@Override} \code{public double getPerformance()}\newline
			\(\mathcal{P} = coefficient / 100 * currentRules + offset\);
			\item
			\textit{@Override} \code{public double getPerformance(int numRules)}\newline
			\(\mathcal{P} = coefficient / 100 * (numRules+currentRules) + offset\);
			\item
			\textit{@Override} {\ttfamily public ArithExpr getPerformance(Context ctx, ArithExpr\newline numRules)}\newline
		\end{itemize}
	
		\item
		\code{class PerformanceCPU}: this class it represent an evaluation of the performance based on the CPU consumption related to the number of the rules injected. As the previous class the performance is represented by a linear function that rises with the increase of number of rules.
		These are the methods implemented:
		\begin{itemize}
			\item 
			\textit{@Override} \code{public double getPerformance()}\newline
			\(\mathcal{P} = coefficient / 100 * currentRules + offset\);
			\item
			\textit{@Override} \code{public double getPerformance(int numRules)}\newline
			\(\mathcal{P} = coefficient / 100 * (numRules+currentRules) + offset\);
			\item
			\textit{@Override} {\ttfamily public ArithExpr getPerformance(Context ctx, ArithExpr\newline numRules)}\newline
		\end{itemize}
	
		\item
		\code{class PerformanceBandwidth}: this class it represent the performance evaluation based on the bandwidth in the communication. Unlike the previous models it\break expresses an exponential function to represent the correct variations and so it needs another instance variable \code{bound} that is the infinite value that the function trend to reach with an infinite number of rules.
		The following methods are implemented:
		\begin{itemize}
			\item 
			\textit{@Override} \code{public double getPerformance()}\newline
			\(\mathcal{P} = (offset-bound)*exp(-coefficient/10000*currentRules)+bound\);
			\item
			\textit{@Override} \code{public double getPerformance(int numRules)}\newline
			\(\mathcal{P} = (offset-bound)*exp(-coefficient/10000*(numRules+currentRules))+bound\);
			\item
			\textit{@Override} {\ttfamily public ArithExpr getPerformance(Context ctx, ArithExpr\newline numRules)}\newline
		\end{itemize}
	\end{itemize}
\end{itemize}
\pagebreak

\subsection{Modify the tool}

It is possible to add different Performance evaluation, besides the ones considered for this work. Currently it supports the latency evaluation, the CPU consumption and the bandwidth, but it is possible adding classes that could evaluate others metrics as RAM and electricity consumption and so on. This is possible by the implementation of a class that extends \code{PerformanceType} class, adding to the XML schema a new element in the enum \code{PerformanceTypeElement} and adding in \code{PerformanceFactory}. \code{getInstance} the return case in the switch that choose the performance instance as

\begin{lstlisting}[style=Java, numbers=none]
case RAM_CONSUMPTION:
return new PerformanceRAM(device, offset, coefficient, nMaxRules); 
break;
\end{lstlisting}
