#+TITLE: Estimating the end-to-end energy consumption of IoT devices along with their impact on Cloud and telecommunication infrastructures

#+EXPORT_EXCLUDE_TAGS: noexport
#+STARTUP: hideblocks
#+OPTIONS: H:5 author:nil email:nil creator:nil timestamp:nil skip:nil toc:nil ^:nil
#+LATEX_CLASS: IEEEtran
#+LATEX_HEADER: \usepackage{hyperref}
#+LATEX_HEADER: \usepackage{booktabs}
#+LATEX_HEADER: \usepackage{subfigure}
#+LATEX_HEADER: \usepackage{graphicx}
#+LATEX_HEADER: \IEEEoverridecommandlockouts 
#+LATEX_HEADER: \author{\IEEEauthorblockN{1\textsuperscript{st} Anne-Cécile Orgerie}
#+LATEX_HEADER: \IEEEauthorblockA{\textit{Univ Rennes, Inria, CNRS, IRISA, Rennes, France} \\
#+LATEX_HEADER: Rennes, France \\
#+LATEX_HEADER: anne-cecile.orgerie@irisa.fr}
#+LATEX_HEADER: \and
#+LATEX_HEADER: \IEEEauthorblockN{2\textsuperscript{nd} Loic Guegan}
#+LATEX_HEADER: \IEEEauthorblockA{\textit{Univ Rennes, Inria, CNRS, IRISA, Rennes, France} \\
#+LATEX_HEADER: Rennes, France \\
#+LATEX_HEADER: loic.guegan@irisa.fr}
#+LATEX_HEADER: }



#+BEGIN_EXPORT latex
\begin{abstract}
Information and Communication Technology takes a growing part in the worldwide energy consumption. One of the root causes of this increase lies in the multiplication of connected devices. Each object of the Internet-of-Things often does not consume much energy by itself. Yet, their number and the infrastructures they require to properly work have leverage. In this paper, we combine simulations and real measurements to study the energy impact of IoT devices. In particular, we analyze the energy consumption of Cloud and telecommunication infrastructures induced by the utilization of connected devices, and we propose an end-to-end energy consumption model for these devices. 
\end{abstract}

\begin{IEEEkeywords}
component, formatting, style, styling, insert
\end{IEEEkeywords}
#+END_EXPORT


* Introduction [2 col]
* Related Work [1 col]
* Use-Case [1 col]
** Application Characteristic

   #+BEGIN_COMMENT
   The IoT part is composed of an Access Point (AP), connected to several sensors using WIFI. In the
   system, the IoT part is considered as the part where the system data are created. In fact, the
   data life cycle start when the sensors records their respective local temperature at a frequency
   $f$ and the local timestamp. Then, these data are transmitted through the network along with an
   arbitrary sensor id of 128 bits. Finally, the AP is in charge to transmit the data to the cloud
   using the network part.

   The network part is considered as the medium that link the IoT part to the cloud. It is composed
   of several network switches and router and it is considered to be a wired network.

   #+END_COMMENT

      
** Cloud Infrastructure

* System Model [2 col]

  The system model is divided in two parts. First, the IoT and the Network part are models through
  simulations. Then, the Cloud part is model using real servers connected to watt-meters. In this way,
  it is possible to evaluate the end-to-end energy consumption of the system.

** IoT Part
   In the first place, the IoT part is composed of several sensors connected to an AP which forms a
   cell. It is model using the ns-3 network simulator. Thus, we setup between 5 and 15 sensors
   connected to the AP using WIFI 5GHz 802.11n. The node are placed randomly in a rectangle of 400m2
   around the AP which correspond to a typical real use case. All the nodes of the cell are setup
   with the default WIFI energy model provided by ns-3.  The different energy values used by the
   energy model are provided on Table \ref{tab:wifi-energy}. These energy were extracted from
   previous work\cite{halperin_demystifying_nodate,li_end--end_2018} on 802.11n. Note that we
   suppose that the energy source of the cell nodes are unlimited and thus every nodes can
   communicate until the end of all the simulations.

   As a scenario, sensors send to the AP packets of 192 bits that include: \textbf{1)} A 128 bits
   sensors id \textbf{2)} A 32 bits integer representing the temperature \textbf{3)} An integer
   timestamp representing the temperature sensing time. The data are transmitted immediately at each
   sensing interval $I$ varied from 1s to 60s. Finally, the AP is in charge of relaying data to the
   cloud using the network part.
   
   #+BEGIN_EXPORT latex
   \begin{table}[]
     \centering
     \caption{Simulations Energy Parameters}
     \label{tab:wifi-energy}
     \subtable[Wifi]{
       \begin{tabular}{@{}lr@{}}
         Parameter      & Value  \\ \midrule
         Supply Voltage & 3.3V   \\
         Tx             & 0.38A  \\
         Rx             & 0.313A \\
         Idle           & 0.273A \\ \bottomrule
     \end{tabular}}
     \hspace{0.3cm}
     \subtable[Network]{
       \label{tab:net-energy}
       \begin{tabular}{@{}lr@{}}
         Parameter  & Value \\ \midrule
         Idle       & 1J            \\ 
         Bytes (Tx/Rx)  & 3.4nJ           \\ 
         Pkt (Tx/Rx)    & 192.0nJ         \\ \bottomrule
       \end{tabular}
     }
   \end{table}
   #+END_EXPORT

** Network Part
   The network part represents the network starting from the AP to the Cloud excluding the server.
   It is also model into ns-3. We consider the server to be 9 hops away from the AP with a typical
   round-trip latency of 100ms from the AP to the server. Each node from the AP to the Cloud is
   assume to be network switches with static and dynamic network energy consumption. ECOFEN
   \cite{orgerie_ecofen:_2011} is used to model the energy consumption of the network part. ECOFEN
   is a ns-3 network energy module for ns-3 dedicated to wired network energy estimation. It is
   based on an energy-per-bit model including static consumption by assuming a linear relation
   between the amount of data sent to the network interface and the power consumption. The different
   energy values used to instantiate the ECOFEN energy model for the network part are shown in Table
   \ref{tab:net-energy} and come from previous work \cite{cornea_studying_2014-1}.

** Cloud Part
   Finally, to measure the energy consumption of the server, we used real server from the
   large-scale test-beds Grid5000 (G5K). In fact, G5K has a cluster called Nova composed of several
   nodes which are connected to watt-meters. In this way, we can benefit from real energy
   measurements. The server used in the experiment is composed of Intel Xeon E5-2620 processor with
   64 GB of RAM and 600GB of disk space on a Linux based distribution. This server is configured to
   use KVM as virtualization mechanism. We deploy a classical Linux x86_64 distribution on the
   Virtual Machines (VM) along with a MySQL database. We different amount of allocated memory for
   the VM namely 1024MB/2048MB/4096MB to highlight its effects on the server energy consumption.

   The sensors requests are simulated using another server. This server is in charge to send hundred
   of requests to the VM in order to fill the database. Consequently, it is easy to vary the
   different requests characteristics namely: \textbf{1)} The number request, to virtually
   add/remove sensors \textbf{2)} The requests frequency.

* Evaluation [3 col]
** IoT/Network Consumption
   In a first place, we start by studying the impact of the sensors position on their energy
   consumption. To this end, we run several simulations in ns-3 with different sensors position. The
   results provided by Figure \ref{fig:sensorsPos} show that sensors position have a very low impact
   on the energy consumption and on the application delay. It has an impact of course but it is very
   limited. This due to the fact that in such a scenario with very small number of communications
   spread over the time, sensors don't have to contend for accessing to the Wifi channel.

   #+BEGIN_EXPORT latex
   \begin{figure}
     \centering
     \includegraphics[width=0.6\linewidth]{./plots/sensorsPosition-delayenergy.png}
     \caption{Effects of sensors position on the application delay and the sensors energy consumption in a cell of 9 sensors.}
     \label{fig:sensorsPos}
   \end{figure}
   #+END_EXPORT

    Previous work \cite{li_end--end_2018} on similar scenario shows that increasing application
    accuracy impact strongly the energy consumption in the context of data stream analysis. However,
    in how case, application accuracy is driven by the sensing frequency and thus the transmit
    frequency of the sensors. In this way, we vary the transmission frequency of the sensors from 1s
    to 60s. Figure \ref{fig:frequency} present the effects of the sensors transmission frequency on
    the IoT/Network part energy consumption. In case of small and sporadic network traffic, these
    results show that with a reasonable transmission frequency the energy consumption of the
    IoT/Network if almost not affected by the variation of this frequency.

    #+BEGIN_EXPORT latex
    \begin{figure}
      \centering
      \includegraphics[scale=0.45]{./plots/sendFrequency-energy.png}
      \caption{Sensors send interval and its influence on the IoT/Network part energy consumption.}  
      \label{fig:frequency}
    \end{figure}
    #+END_EXPORT
    

   The number of sensors is the dominant factor that leverage the energy consumption of the
   IoT/Network part. Therefore, we varied the number of sensors in the Wifi cell to analyze its
   impact. The figure \ref{fig:sensorsNumber} represents the energy consumption of each simulated
   part. It is clear that the energy consume by the network is the dominant part. However, since the
   number of sensors is increasing the energy consume by the network will become negligible face to
   the energy consume by the sensors. In fact, deploying new sensors in the cell do not introduce
   much network load. To this end, sensors energy consumption is dominant. 
   
    #+BEGIN_EXPORT latex
    \begin{figure}
      \centering
      \includegraphics[width=0.6\linewidth]{./plots/numberSensors-WIFINET.png}
      \caption{Analysis of the variation of the number of sensors on the IoT/Network part energy consumption.}
      \label{fig:sensorsNumber}
    \end{figure}
    #+END_EXPORT


** Cloud Energy Consumption
   In this End-To-End energy consumption study, cloud account for a huge part of the overall energy
   consumption. According a report \cite{shehabi_united_2016-1} on United States data center energy
   usage, the average Power Usage Effectiveness (PUE) of an hyperscale data center is 1.2. Thus, in
   our analysis, all energy measurement on cloud server will account for this PUE.

   In a first place, we analyse the impact of the VM allocated memory on the server energy
   consumption. Figure \ref{fig:vmSize} depict the server energy consumption according to the VM
   allocated memory for 20 sensors sending data every 10s. Note that red horizontal line represent
   the average energy consumption for sample of energy value. We can see that at each sensing
   interval, server face to peaks of energy consumption. However, VM allocated memory do not
   influence energy consumption. In fact, simple database requests do not need any particular huge
   memory access. Thus, remaining experiments are based on VM allocated memory of 1024MB.

   #+BEGIN_EXPORT latex
   \begin{figure}
     \centering
     \includegraphics[width=0.8\linewidth]{./plots/vmSize-cloud.png}
     \caption{VM size impact on the server energy consumption using 20 sensors sending data every 10s}
     \label{fig:vmSize}
   \end{figure}
   #+END_EXPORT


   Next, we study the effects of increasing the number of sensors on the server energy consumption.
   Figure \ref{fig:sensorsNumber-server} present the results of the average server energy
   consumption when varying the number of sensors from 20 to 500 while Figure
   \ref{fig:sensorsNumber-WPS} present the average server energy cost per sensors according to the
   number of sensors. These shows a clear linear relation between the number of sensors and the
   server energy consumption. Moreover, we can see that the more sensors we have per server, the
   more energy we can save. In fact, since the idle server energy consumption is high, it is more
   energy efficient to maximze the number of sensors per server. As showed on Figure
   \ref{fig:sensorsNumber-WPS}, a significant amount of energy can be save when passing from 20
   sensors to 300 per server.

   #+BEGIN_EXPORT latex
   \begin{figure}
     \centering
     \subfigure[Average server energy consumption]{
       \includegraphics[width=0.4\linewidth]{./plots/sensorsNumberLine-cloud.png}
       \label{fig:sensorsNumber-server}
     }
     \hspace{0.5cm}
     \subfigure[Average sensors energy cost on server]{
       \includegraphics[width=0.4\linewidth]{./plots/WPS-cloud.png}
       \label{fig:sensorsNumber-WPS}
     }
     \caption{Server energy consumption for sensors sending data every 10s}
     \label{fig:sensorsNumber-cloud}
   \end{figure}
   #+END_EXPORT

   A last parameter can leverage server energy consumption namely sensors send frequency. In
   addition to increasing the application accuracy, sensors send frequency increase network traffic
   and database accesses. 

   #+BEGIN_EXPORT latex
   \begin{figure}
  
     \caption{Freq}
  
   \end{figure}
   #+END_EXPORT
   
   
   
** End-To-End Consumption
* Discussion [1 col]
* Conclusion [1 col]
* References [1 col]
\bibliographystyle{IEEEtran}
\bibliography{references}


* Data Provenance :noexport:
  :PROPERTIES:
  :header-args: :eval never-export
  :END:
** Data Analysis (R Scripts)
*** Utils
**** R
      RUtils is intended to load logs (data.csv) and providing
      simple plot function for them.

      #+NAME: RUtils
      #+BEGIN_SRC R :eval never
        library("tidyverse")

        # Fell free to update the following
        labels=c(time="Time (s)",sensorsSendInterval="Sensors Send Interval (s)", sensorsNumber="Number of sensors")
        PUE=1.2
        ns3SimTime=1800
        g5kSimTime=300

        loadData=function(path){
          data=read_csv(path)
          if("sensorsEnergy"%in%colnames(data)){ # If it is ns3 logs
            data=data%>%mutate(sensorsEnergy=sensorsEnergy/ns3SimTime)  # Convert to watts
            data=data%>%mutate(networkEnergy=networkEnergy/ns3SimTime)
            data=data%>%mutate(totalEnergy=totalEnergy/ns3SimTime)
          }
          else{ # Log from g5k
            data=data%>%mutate(energy=energy*PUE) # Take into account PUE
            data=data%>%filter(time<=g5kSimTime) # Remove extrats values (theorical sim time != real sim time)
          }
        }

                # Get label according to varName
        getLabel=function(varName){
          if(is.na(labels[varName])){
            return(varName)
          }
          return(labels[varName])
        }

        applyTheme=function(plot,...){
          palette<- c("#00AFBB", "#E7B800", "#FC4E07","#0abb00")
          plot=plot+
            theme_bw(...)+
              scale_fill_manual(values=palette)+
                scale_colour_manual(values=palette)
          return(plot)
        }
      #+END_SRC
**** Bash
***** Plots -> PDF
     Merge all plots in plots/ folder into a pdf file.
     #+NAME: plotToPDF
     #+BEGIN_SRC bash :results output :noweb yes
       orgFile="plots/plots.org"
       <<singleRun>> # To get all default arguments

       # Write helper function
       function write {
           echo "$1" >> $orgFile
       }

       echo "#+TITLE: Analysis" > $orgFile
       write "#+LATEX_HEADER: \usepackage{fullpage}"
       write "#+OPTIONS: toc:nil"
       # Default arguments
       write '\begin{center}'
       write '\begin{tabular}{lr}'
       write 'Parameters & Values\\'
       write '\hline'
       write "sensorsPktSize & ${sensorsPktSize} bytes\\\\"
       write "sensorsSendInterval & ${sensorsSendInterval}s\\\\"
       write "sensorsNumber & ${sensorsNumber}\\\\"
       write "nbHop & ${nbHop}\\\\"
       write "linksBandwidth & ${linksBandwidth}Mbps\\\\"
       write "linksLatency & ${linksLatency}ms\\\\"
       write '\end{tabular}'
       write '\newline'
       write '\end{center}'

       for plot in $(find plots/ -type f -name "*.png")
       do
           write "\includegraphics[width=0.5\linewidth]{$(basename ${plot})}"
       done

       # Export to pdf
       emacs $orgFile --batch -f org-latex-export-to-pdf --kill  
     #+END_SRC

***** CSVs -> data.csv (G5K)
     Merge all energy file into one (and add additional fields).

     #+NAME: G5K-mergeCSV
     #+BEGIN_SRC sh
       #!/bin/bash

       whichLog="last"

       logsLocation="logs/g5k"
       whichLog="${logsLocation}/${whichLog}"



       logFile="$(dirname $(readlink -f $0))"/$whichLog/simLogs.txt
       dataFile=$(dirname "$logFile")/data.csv


       getValue () {
           line=$(echo "$1" | grep "Simulation para"|sed "s/Simulation parameters: //g")
           key=$2
           echo "$line"|awk 'BEGIN{RS=" ";FS=":"}"'$key'"==$1{gsub("\n","",$0);print $2}'
       }

       ##### Add extract info to energy #####
       IFS=$'\n'
       for cmd in $(cat $logFile|grep "Simulation parameters")
       do
           nodeName=$(getValue $cmd serverNodeName)
           from=$(getValue $cmd simStart)
           to=$(getValue $cmd simEnd)
           vmSize=$(getValue $cmd vmSize)
           nbSensors=$(getValue $cmd nbSensors)
           simKey=$(getValue $cmd simKey)
           sendInterval=$(getValue $cmd sensorsSendInterval)
           csvFile="$whichLog/${simKey}_${vmSize}VMSIZE_${nbSensors}NBSENSORS_${from}${to}.csv"
           csvFileIDLE="$whichLog/${simKey}_${vmSize}VMSIZE_${nbSensors}NBSENSORS_${from}${to}_IDLE.csv"
           tmpFile=${csvFile}_tmp
           echo ts,energy,simKey,vmSize,nbSensors,time,state,sendInterval > $tmpFile
           minTs=$(tail -n+2 $csvFile|awk -F"," 'BEGIN{min=0}$1<min||min==0{min=$1}END{print(min)}') # To compute ts field
           minTsIDLE=$(tail -n+2 $csvFileIDLE|awk -F"," 'BEGIN{min=0}$1<min||min==0{min=$1}END{print(min)}') # To compute ts field
           tail -n+2 ${csvFile} | awk -F"," '{print $0",'$simKey','$vmSize','$nbSensors',"$1-'$minTs'",sim,"'$sendInterval'}' >> $tmpFile
           tail -n+2 ${csvFileIDLE} | awk -F"," '{print $0",'$simKey','$vmSize','$nbSensors',"$1-'$minTsIDLE'",IDLE,"'$sendInterval'}' >> $tmpFile
       done


       ##### Fill dataFile #####
       echo ts,energy,simKey,vmSize,nbSensors,time,state,sendInterval > $dataFile
       for tmpFile in $(find ${whichLog}/*_tmp -type f)
       do
           tail -n+2 $tmpFile >> $dataFile
           rm $tmpFile # Pay attention to this line :D
       done
     #+END_SRC

     #+RESULTS: G5K-mergeCSV

     #+RESULTS: mergeCSV



***** Log -> data.csv (ns3)
      logToCSV extract usefull data from logs and put them into logs/data.csv.

      #+NAME: NS3-logToCSV
      #+BEGIN_SRC bash :results output
        logsFolder="./logs/ns3/last/"
        csvOutput="$logsFolder/data.csv"

        # First save csv header line
        aLog=$(find $logsFolder -type f -name "*.org"|head -n 1)
        metrics=$(cat $aLog|grep "\-METRICSLINE\-"|sed "s/-METRICSLINE-//g")
        echo $metrics | awk '{for(i=1;i<=NF;i++){split($i,elem,":");printf(elem[1]);if(i<NF)printf(",");else{print("")}}}' > $csvOutput

        # Second save all values
        for logFile in $(find $logsFolder -type f -name "*.org")
        do
            metrics=$(cat $logFile|grep "\-METRICSLINE\-"|sed "s/-METRICSLINE-//g")
            echo $metrics | awk '{for(i=1;i<=NF;i++){split($i,elem,":");printf(elem[2]);if(i<NF)printf(",");else{print("")}}}' >> $csvOutput
        done
      #+END_SRC

      #+RESULTS: NS3-logToCSV



   
   

*** Plot Scripts
**** Random R Scripts

       Watt per sensor on server
    #+BEGIN_SRC R :noweb yes :results output
      <<RUtils>>
      # Load data
      data=loadData("./logs/g5k/last/data.csv")

      data=data%>%filter(state=="sim",simKey=="nbSensors")%>%ungroup()


      data=data%>%group_by(nbSensors)%>%mutate(avgEnergy=mean(energy))%>%distinct()%>%ungroup()
      data=data%>%distinct(nbSensors,.keep_all=TRUE)
      data=data%>%mutate(WPS=(avgEnergy/nbSensors))

      print(data%>%select(WPS,nbSensors))
    #+END_SRC



     Impact of vm size
     #+BEGIN_SRC R :results  graphics :file plots/vmSizeBar-cloud.png
       library("tidyverse")
       PUE=1.2
       # Load data
       data=read_csv("./logs/g5k/last/data.csv")

       data=data%>%filter(state=="sim",simKey=="vmSize")%>%mutate(energy=PUE*energy)%>%filter(time<=300)

       data=data%>%group_by(vmSize)%>%mutate(energy=mean(energy))%>%slice(1L)%>%ungroup()
       data=data%>%mutate(vmSize=as.character(vmSize))

       ggplot(data) + geom_bar(aes(x=vmSize,y=energy),stat="identity")+expand_limits(y=c(75,100))+ylab("Server Energy Consumption (W)")+
       xlab("Simulation Time (s)")+scale_y_log10()


       ggsave("plots/vmSizeBar-cloud.png",dpi=90,height=3,width=6)  
     #+END_SRC

     #+RESULTS:
     [[file:plots/vmSizeBar-cloud.png]]



     #+NAME: ssiNet
     #+BEGIN_SRC R :noweb yes :results graphics :file plots/sensorsSendInterval-net.png
       <<NS3-RUtils>>
       # Load Data
        data=read_csv("logs/ns3/last/data.csv")

       data%>%filter(simKey=="SENDINTERVAL",sensorsNumber==20) %>% ggplot(aes(x=sensorsSendInterval,y=networkEnergy))+xlab(getLabel("sensorsSendInterval"))+ylab(getLabel("networkEnergy"))+
       geom_line()+labs(title="For 20 sensors")
       ggsave("plots/sensorsSendInterval-net.png",dpi=80)
     #+END_SRC


     Effect of the number of sensors on the application delay
     #+BEGIN_SRC R :noweb yes :results graphics :file plots/delay-nbsensors.png
       <<NS3-RUtils>>
       # Load Data
        data=read_csv("logs/ns3/last/data.csv")

       data%>%filter(simKey=="NBSENSORS") %>% ggplot(aes(y=avgDelay,x=sensorsNumber))+xlab(getLabel("sensorsNumber"))+ylab(getLabel("avgDelay"))+
       geom_line()+labs(title="For 20 sensors")
       ggsave("plots/delay-nbsensors.png",dpi=80)
     #+END_SRC

     #+RESULTS:
     [[file:plots/delay-nbsensors.png]]


   
     #+NAME: ssiWifi
     #+BEGIN_SRC R :noweb yes :results graphics :file plots/sensorsSendInterval-wifi.png
       <<NS3-RUtils>>
       data=read_csv("logs/ns3/last/data.csv")
       data%>%filter(simKey=="SENDINTERVAL",sensorsNumber==20) %>% ggplot(aes(x=sensorsSendInterval,y=sensorsEnergy))+xlab(getLabel("sensorsSendInterval"))+ylab(getLabel("sensorsEnergy"))+
       geom_line() +     geom_line()+labs(title="For 20 sensors")
       ggsave("plots/sensorsSendInterval-wifi.png",dpi=80)
     #+END_SRC


      #+BEGIN_SRC R :results graphics  :noweb yes  :file plot-final.png :session *R*
        <<NS3-RUtils>>
        simTime=1800

        # Network
        data=read_csv("logs/ns3/last/data.csv")
        data=data%>%filter(simKey=="NBSENSORS")
        dataC5=data%>%filter(sensorsNumber==5)%>% mutate(energy=networkEnergy/simTime) %>%select(energy,sensorsNumber)
        dataC10=data%>%filter(sensorsNumber==10)%>%mutate(energy=networkEnergy/simTime) %>%select(energy,sensorsNumber)
        dataNet=rbind(dataC5,dataC10)%>%mutate(type="Network")

        # Network
        dataS5=data%>%filter(sensorsNumber==5)%>% mutate(energy=sensorsEnergy/simTime) %>%select(energy,sensorsNumber) 
        dataS10=data%>%filter(sensorsNumber==10)%>%mutate(energy=sensorsEnergy/simTime) %>%select(energy,sensorsNumber) 
        dataS=rbind(dataS5,dataS10)%>%mutate(type="Sensors")
      
        data=rbind(dataNet,dataS)%>%mutate(sensorsNumber=as.character(sensorsNumber))

        ggplot(data)+geom_bar(aes(x=sensorsNumber,y=energy,fill=type),stat="identity")+xlab("Sensors Number")+ylab("Power Consumption (W)")+guides(fill=guide_legend(title="Part"))
        ggsave("plot-final.png",dpi=80)

      #+END_SRC
    
**** Plot In Paper

    Figure 
    #+BEGIN_SRC R :noweb yes :results graphics :file plots/sendFrequency-energy.png
      <<RUtils>>

      data=loadData("logs/ns3/last/data.csv")
      data=data%>%filter(simKey=="SENDINTERVAL",sensorsNumber==15) 

      p=ggplot(data,aes(y=totalEnergy,x=sensorsSendInterval))+
        xlab(getLabel("sensorsSendInterval"))+ylab(getLabel("Sensors And Network\nEnergy Consumption (W)"))+
        geom_line()+geom_point()+expand_limits(y=c(0,50))+
        guides(color=guide_legend(title="Curves"))+
        theme(axis.title.y.right = element_text(margin = margin(t = 0, r = -13, b = 0, l = 7)))
        p=applyTheme(p)

      ggsave("plots/sendFrequency-energy.png",dpi=100, width=3, height=2.8)
    #+END_SRC

    #+RESULTS:
    [[file:plots/sendFrequency-energy.png]]



    Figure Sensors Position ~ Energy/Delay
    #+BEGIN_SRC R :noweb yes :results graphics :file plots/sensorsPosition-delayenergy.png
      <<RUtils>>
      tr=171 # Offset to center delay plot
      data=loadData("logs/ns3/last/data.csv")

      data=data%>%filter(simKey=="SENSORSPOS",sensorsNumber==9)

      p=ggplot(data,aes(y=sensorsEnergy,x=positionSeed,color="Energy"))+xlab(getLabel("Sensors Position Seed"))+ylab(getLabel("Sensors Energy Consumption (W)"))+
      geom_line()+geom_point()+geom_line(aes(y=(avgDelay-tr),color="Delay"))+geom_point(aes(y=(avgDelay-tr),color="Delay"))+expand_limits(y=c(0,15))+
      scale_y_continuous(sec.axis = sec_axis(~.+tr, name = "Application Delay (s)")) +
      guides(color=guide_legend(title="Curves"))

      p=applyTheme(p)      
      p=p+theme(axis.title.y.right = element_text(margin = margin(t = 0, r = -8, b = 0, l = 10)))



      ggsave("plots/sensorsPosition-delayenergy.png",dpi=80, width=4, height=3.2)
    #+END_SRC

    #+RESULTS:
    [[file:plots/sensorsPosition-delayenergy.png]]
    



    #+BEGIN_SRC R :noweb yes :results graphics  :file plots/numberSensors-WIFINET.png :session *R*
      <<RUtils>>

      data=loadData("logs/ns3/last/data.csv")
      data=data%>%filter(simKey=="NBSENSORS")
      dataW=data%>%mutate(energy=sensorsEnergy)%>% mutate(type="Sensors") %>% select(sensorsNumber,energy,type) 
      dataN=data%>%mutate(energy=networkEnergy)%>% mutate(type="Network") %>% select(sensorsNumber,energy,type)

      data=rbind(dataN,dataW)
      data=data%>%mutate(sensorsNumber=as.character(sensorsNumber))
      data=data%>%mutate(sensorsNumber=fct_reorder(sensorsNumber,as.numeric(sensorsNumber)))
      data=data%>%filter(sensorsNumber%in%c(2,4,6,8,10))

      p=ggplot(data)+geom_bar(aes(x=sensorsNumber,y=energy,fill=type),position="identity",stat="identity")+
      xlab(getLabel("sensorsNumber"))+ ylab("Energy Consumption (W)") + guides(fill=guide_legend(title="")) +coord_flip()
      p=applyTheme(p)+theme(text = element_text(size=15))

      size=5
     ggsave("plots/numberSensors-WIFINET.png",dpi=90,width=size,height=size-1)
    #+END_SRC

    #+RESULTS:
    [[file:plots/numberSensors-WIFINET.png]]






  Final plot: Energy cloud, network and sensors
  #+BEGIN_SRC R :noweb yes :results graphics :file plots/final.png :session *R*
    library("tidyverse")

    # Load data
    data=loadData("./logs/g5k/last/data.csv")
    data=data%>%filter(state=="sim",simKey=="nbSensors")

    # Cloud
    data10=data%>%filter(nbSensors==20)%>%mutate(energy=mean(energy)) %>% slice(1L)
    data100=data%>%filter(nbSensors==100)%>%mutate(energy=mean(energy)) %>% slice(1L)
    data300=data%>%filter(nbSensors==300)%>%mutate(energy=mean(energy)) %>% slice(1L)
    dataCloud=rbind(data10,data100,data300)%>%mutate(sensorsNumber=nbSensors)%>%mutate(type="Cloud")%>%select(sensorsNumber,energy,type)

    approx=function(data1, data2,nbSensors){
      x1=data1$sensorsNumber
      y1=data1$energy

      x2=data2$sensorsNumber
      y2=data2$energy

      a=((y2-y1)/(x2-x1))
      b=y1-a*x1

      return(a*nbSensors+b)

    }


    simTime=1800

    # Network
    data=read_csv("./logs/ns3/last/data.csv")
    data=data%>%filter(simKey=="NBSENSORS")
    dataC5=data%>%filter(sensorsNumber==5)%>% mutate(energy=networkEnergy/simTime) %>%select(energy,sensorsNumber)
    dataC10=data%>%filter(sensorsNumber==10)%>%mutate(energy=networkEnergy/simTime) %>%select(energy,sensorsNumber)
    dataNet=rbind(dataC5,dataC10)%>%mutate(type="Network")

    # Sensors
    dataS5=data%>%filter(sensorsNumber==5)%>% mutate(energy=sensorsEnergy/simTime) %>%select(energy,sensorsNumber) 
    dataS10=data%>%filter(sensorsNumber==10)%>%mutate(energy=sensorsEnergy/simTime) %>%select(energy,sensorsNumber) 
    dataS=rbind(dataS5,dataS10)%>%mutate(type="Sensors")

    fakeNetS=tibble(
      sensorsNumber=c(20,100,300,20,100,300),
      energy=c(dataC10$energy,approx(dataC5,dataC10,100),approx(dataC5,dataC10,300),dataS10$energy,approx(dataS5,dataS10,100),approx(dataS5,dataS10,300)),
      type=c("Network","Network","Network","Sensors","Sensors","Sensors")
    )

    fakeNetS=fakeNetS%>%mutate(sensorsNumber=as.character(sensorsNumber))
    dataCloud=dataCloud%>%mutate(sensorsNumber=as.character(sensorsNumber))
    data=rbind(fakeNetS,dataCloud)%>%mutate(sensorsNumber=as.character(sensorsNumber))
    data=data%>%mutate(sensorsNumber=fct_reorder(sensorsNumber,as.numeric(sensorsNumber)))

    p=ggplot(data)+geom_bar(position="dodge2",colour="black",aes(x=sensorsNumber,y=energy,fill=type),stat="identity")+ 
    xlab("Sensors Number")+ylab("Power Consumption (W)")+guides(fill=guide_legend(title="Part"))
    p=applyTheme(p)
    ggsave("plots/final.png",dpi=80)

  #+END_SRC

  #+RESULTS:
  [[file:plots/final.png]]




  Impact of vm size
  #+BEGIN_SRC R :noweb yes :results graphics :noweb yes :file plots/vmSize-cloud.png
    <<RUtils>>

    # Load data
    data=loadData("./logs/g5k/last/data.csv")

    data=data%>%filter(state=="sim",simKey=="vmSize")%>%filter(time<=300)

    data=data%>%mutate(vmSize=paste0(vmSize," MB"))
    data=data%>%group_by(vmSize)%>%mutate(avgEnergy=mean(energy))%>%ungroup()
    p=ggplot(data,aes(x=time, y=energy)) + geom_line()+facet_wrap(~vmSize)+geom_hline(aes(yintercept=avgEnergy),color="Red",size=1.0)+expand_limits(y=c(0,40))+ylab("Server Energy Consumption (W)")+
    xlab("Simulation Time (s)")

    p=applyTheme(p)

    ggsave("plots/vmSize-cloud.png",dpi=90,height=3,width=6)  
  #+END_SRC

  #+RESULTS:
  [[file:plots/vmSize-cloud.png]]


  Impact of sensors number
  #+BEGIN_SRC R :noweb yes :results  graphics :file plots/sensorsNumber-cloud.png
    <<RUtils>>
    # Load data
    data=loadData("./logs/g5k/last/data.csv")

    data=data%>%filter(state=="sim",simKey=="nbSensors")%>%ungroup()

    data=data%>%mutate(nbSensorsSort=nbSensors)
    data=data%>%mutate(nbSensors=paste0(nbSensors," Sensors"))
    data$nbSensors=fct_reorder(data$nbSensors, data$nbSensorsSort)

    data=data%>%group_by(nbSensors)%>%mutate(avgEnergy=mean(energy))%>%ungroup()
    p=ggplot(data,aes(x=time, y=energy)) + geom_line()+facet_wrap(~nbSensors)+expand_limits(y=c(0,40))+ylab("Server Energy Consumption (W)")+
    xlab("Simulation Time (s)")+geom_hline(aes(yintercept=avgEnergy),color="Red",size=1.0)

    p=applyTheme(p)
    ggsave("plots/sensorsNumber-cloud.png",dpi=90,height=3,width=6)  
  #+END_SRC

  #+RESULTS:
  [[file:plots/sensorsNumber-cloud.png]]


  
    #+BEGIN_SRC R :noweb yes :results graphics :file plots/sensorsNumberLine-cloud.png :session *R:2*
          <<RUtils>>
          # Load data
          data=loadData("./logs/g5k/last/data.csv")

          data=data%>%filter(state=="sim",simKey=="nbSensors")%>%ungroup()


          data=data%>%group_by(nbSensors)%>%mutate(avgEnergy=mean(energy))%>%distinct()%>%ungroup()
          data=data%>%distinct(nbSensors,.keep_all=TRUE)
          data=data%>%mutate(WPS=(avgEnergy/nbSensors))

          p=ggplot(data,aes(x=nbSensors, y=avgEnergy)) + geom_point() +geom_line()+
          xlab(getLabel("sensorsNumber"))+ylab("Average Server Energy (W)")

          p=applyTheme(p)+theme(text = element_text(size=14))+ expand_limits(y=108)
          ggsave("plots/sensorsNumberLine-cloud.png",dpi=90,height=4,width=4)
  #+END_SRC

  #+RESULTS:
  [[file:plots/sensorsNumberLine-cloud.png]]


  #+BEGIN_SRC R :noweb yes :results graphics :file plots/WPS-cloud.png :session *R:2*
          <<RUtils>>
          # Load data
          data=loadData("./logs/g5k/last/data.csv")

          data=data%>%filter(state=="sim",simKey=="nbSensors")%>%ungroup()


          data=data%>%group_by(nbSensors)%>%mutate(avgEnergy=mean(energy))%>%distinct()%>%ungroup()
          data=data%>%distinct(nbSensors,.keep_all=TRUE)
          data=data%>%mutate(WPS=(avgEnergy/nbSensors))

          oldNb=data$nbSensors
          data=data%>%mutate(nbSensors=as.character(nbSensors))
          data$nbSensors=fct_reorder(data$nbSensors,oldNb)
          p=ggplot(data,aes(x=nbSensors, y=WPS)) + geom_bar(stat="identity")+
          xlab(getLabel("sensorsNumber"))+ylab("Server energy cost per sensors (W)")

          p=applyTheme(p)+theme(text = element_text(size=14))+        theme(axis.title.y = element_text(margin = margin(t = 0, r = 8, b = 0, l = 0)))
          ggsave("plots/WPS-cloud.png",dpi=90,height=4,width=4)
  #+END_SRC

  #+RESULTS:
  [[file:plots/WPS-cloud.png]]






  #+BEGIN_SRC R :noweb yes :results  graphics :file plots/sendInterval-cloud.png
    <<RUtils>>
    # Load data
    data=loadData("./logs/g5k/last/data.csv")

    data=data%>%filter(state=="sim",simKey=="sendInterval")%>%ungroup()

    oldSendInterval=data$sendInterval
    data=data%>%mutate(sendInterval=paste0(sendInterval,"s"))
    data$sendInterval=fct_reorder(data$sendInterval,oldSendInterval)

    data=data%>%group_by(sendInterval)%>%mutate(avgEnergy=mean(energy))%>%ungroup()
    p=ggplot(data,aes(x=time, y=energy)) + geom_line()+facet_wrap(~sendInterval)+expand_limits(y=c(0,40))+ylab("Server Energy Consumption (W)")+
    xlab("Simulation Time (s)")+geom_hline(aes(yintercept=avgEnergy),color="Red",size=1.0)

    p=applyTheme(p)
    ggsave("plots/sendInterval-cloud.png",dpi=90,height=3,width=6)  
  #+END_SRC

  #+RESULTS:
  [[file:plots/sendInterval-cloud.png]]




* Emacs settings :noexport:
  # Local Variables:
  # eval:    (unless (boundp 'org-latex-classes) (setq org-latex-classes nil))
  # eval:    (add-to-list 'org-latex-classes
  #                       '("IEEEtran" "\\documentclass[conference]{IEEEtran}\n \[NO-DEFAULT-PACKAGES]\n \[EXTRA]\n"  ("\\section{%s}" . "\\section*{%s}") ("\\subsection{%s}" . "\\subsection*{%s}")                       ("\\subsubsection{%s}" . "\\subsubsection*{%s}")                       ("\\paragraph{%s}" . "\\paragraph*{%s}")                       ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
  # End: