”R” code kata

This started as a ‘R’ code kata. I came across some Nmon reports from a AIX machine.

There is an nmon analyzer too available in IBM’s website.

The goal here is to learn to write ‘R’ code to ‘grep’ for lines that have information for CPU’s like this

CPU01,T0001,7.6,28.9,1.3,62.1
CPU02,T0001,4.9,6.5,1.1,87.5
CPU03,T0001,2.4,2.1,0.4,95.1
CPU04,T0001,2.9,1.4,0.8,94.9

Not only that. I also want to draw graphs for individual CPU’s and find if there are correlations between different CPU utilizations. This type of analysis is described in some papers published by ‘Computer Measurement Group’. I don’t have the links now but I plan to post more about this along with the graphs.

At this time this is not a serious performance planning measure. But it should be possible to use ‘R’ code to create a good nmon analyzer report.

This initial version of the code executes but it is not complete.

YAML configuration

path:
 input : D:\R\R-3.0.0\bin\MACHINE_130525_0000.csv
 output : D:\R\R-3.0.0\bin

Main code


library(yaml)
library(stringr)

# Set to load the configuration file.
# It might be set elsewhere also.
this.dir <- dirname(parent.frame(2)$ofile) 
setwd(this.dir) 

# Read nmon report and filter CPU utilization
filelist.read <- function(){
	config = yaml.load_file("config.yml")
	print(config$path$input)
	output <-(config$path$output)
	nmon <-file(config$path$input, "r")
	fileConn<-file(paste(output,"\\output.txt", sep = ""),"w")

	files <- NULL

	while(length(line <- readLines(nmon, 1)) > 0) {
		files <- line.filter( line )
		if (length(files) != 0) {
			writeLines(files, fileConn)
			#print(files)
			files <- NULL
		}
	}
	close(nmon)
	close(fileConn)
}

#filter based on a regular expression
line.filter <- function(line){
	filteredline <- grep("^CPU", line, value = TRUE)
	return (filteredline)
}


#Write each CPU's utilization data into a 
#separate file
filelist.array <- function(n){
  cpufile <- list()
  length(cpufile) <- n
  for (i in 1:n) {
    cpufile[[i]] <- paste("output", i, ".txt", sep = "") 
    print(cpufile[i])	
  }
}
#Write each CPU's utilization into a 
#separate file
filelist.array <- function(n){
         cpufile <- list()
         length(cpufile) <- n
         for (i in 1:n) {
            cpufile[[i]] <- paste("output", i, sep = "")
            print(cpufile[i])
         }
}

RUnit

library(RUnit)

#Sample test
test.filelist.array <- function() {
	filelist.array(3)
}

RUnit test runner

library(RUnit)

# Set to load sources and test code
# properly.
this.dir <- dirname(parent.frame(2)$ofile) 
setwd(this.dir) 

source('nmon.R')
source('unitTests/nmontestcase.R')
test.filelist.array()

Parse JSP using BeautifulSoup

I had to parse a tangle of JSP’s to identify how many HTML controls were calling JavaScript
functions that make AJAX calls back to the application.

So if a ‘key press’ event is fired when a user ‘tabs out’ or presses ‘Enter’ on a
textbox then I wanted the scan to find that.

<html:text maxlength="30" onblur="blurAction(this)" onfocus="displayFieldMsg(this)" onkeydown="keyDownEvents(this)" onkeypress="keyPressEvents(this)" onkeyup="convertUCase(this)" property="txtExtCredit" size="40" style="text-align:left;" styleclass="inputfld"></html:text>

My python skills are rudimentary but this code is able to scan and show a list of ‘html:text’ Struts tags. PyDev eclipse plugin comes in handy for python development.

The code can be further enhanced for more complex scans which I plan to do.


from bs4 import BeautifulSoup
import fnmatch
import sys
import re
import os
import glob

class Parse:

    def __init__(self):
        print 'parsing'
        self.parse()
        #self.folderwalk()

    def parse(self):
        try:
            path = "D:\\path"

            for infile in glob.glob(os.path.join(path, "*.jsp")):
                markup = (infile)
                print markup
                soup = BeautifulSoup(open(markup, "r").read())
            
                data=soup.findAll(re.compile('^html:text'),attrs={'onkeypress':re.compile('^keyPressEvents')})
                for i in data:
                    print i
                     
        except IOError as e:
            print "I/O error({0}): {1}".format(e.errno, e.strerror)
        except:
            print "Unexpected error:", sys.exc_info()[0]
            print "Unexpected error:", markup


    # Not used at this time
    def folderwalk(self):

        rootdir = "D:\\path"
        folderlist =0, []
        
        #Pattern to be matched
        includes = ['*.jsp']
        
        try:
            for root, subFolders, files in os.walk(rootdir):
                for extensions in includes:
                    for filename in fnmatch.filter(files, extensions):
                        print filename
                        #folderlist.append()
        except IOError as e:
            print "I/O error({0}): {1}".format(e.errno, e.strerror)
        except:
            print "Unexpected error:", sys.exc_info()[0]

    
if __name__ == '__main__':
    instance = Parse()

IntelliJ IDEA anonymous class as Java 8 lambda

I recently started coding lambdas. This is partly to prevent my Java skills from getting rusted.

I found that IntelliJ IDEA has this interesting feature that allows an inner class to be shown as a lambda.

 public class Employee {

           public Employee( String name,int age){
               this.age = age;}

           interface CheckAge {boolean test(Employee p);
                 int age;
                 public int getAge() {
                       return 36;
                 }
 
           static void printEmployees( List<Employee> m, CheckAge ca ){
             for( Employee m1 : m ){
                System.out.println( ca.test( m1 ));
             } 
           }

           public static void main( String... argv ){

                List<Employee> employees= new ArrayList<Employee>();

                employees.add( new Employee( "Test", 29));

                printEmployees( employees,new CheckAge() {
                     public boolean test(Employee p) {
                         return p.getAge() >= 18;
                                  p.getAge() <= 35;}
                });
   }
 }


Lambda

The printEmployees method has an anonymous inner class and the screenshot shows
IntelliJ IDEA’s interpretation.

The product company I am working for is very far behind this type of technology curve and only technology evangelism might help.