Instrumentation from the command line

Big Hint

Within about ten lines of command line coding you may be enable to perform live code coverage reporting.

Introduction

Emma is an open source code coverage tool. In our case, Emma enables code coverage by instrumenting the target code. The instrumentation process involves Emma looking at the class or jar files and adding its own layer between the JVM and the code normally called.

During the Instrumenting process, metadata is created per class. This metadata is stored in files locally. In our situation, we generate one meta file per webapp.
The instrumented code has an extra port open, which can accept commands from Emma such as return coverage information or reset coverage information that resides in memory.

To generate a full report Emma requires:

  1. the code has been compiled with the debug option on
  2. the source code is available,
  3. session data is available in a local file,
  4. a relevant metadata file exists.

See also: QA-helper tool configuration

Command line recipe

Prerequisite

The highest version (build 2.1.5320) emma jar file needs to be downloaded from Emma home. The jar needs to be placed in {$JAVA_HOME}/jre/lib/ext or added to the classpath of the following commands. This is also true for the JAVA_HOME seen by the Sakai server.

Javac compiles the source code with debug option on
Note: Running maven pack-demo compiles with debug on by default

The source code is available for report generation
Note: This simple Perl script copies all the source code from Sakai source into one directory
structure instead of the source being scattered under the java/src directories.

This approach allows report generation for all reports to point to the same source code directory and avoids extra work.

$root=When you have your Sakai source code
$target=Where you want your condensed tree
$filter=The directory pattern to search for

Example source code condenser
#!/usr/bin/perl
use File::Copy;
use File::Find;

my $root="/home/alan/temp/coverage/src/trunk";
my $target="/home/alan/temp/coverage/src/condensed_trunk";
my $filter="src/java/";
my @parts;

print "Copying and condensing source ready for htmlzing\n";
mkdir("$target", 0755) || die "Cannot mkdir $target: $!";
find (\&wanted, $root);

sub wanted{
    @parts=split(/$filter/,$File::Find::name);
    if ((-d $_)&&($parts[1] ne "")){
        if (!(-d "$target/$parts[1]" )){
             mkdir("$target/$parts[1]", 0755) || die "Cannot mkdir $target/$parts[1]: $!";
        }
    }else{
        if ($_=~/.java$/){
             copy($File::Find::name,"$target/$parts[1]");
        }
    }
}

Instrument code

Once emma.jar sits in the appropriate directory then the following command should bring up the help information for EMMA:

{$JAVA_HOME}/bin/java emma

Resulting output
emma usage: emma <command> command options,
  where <command> is one of:

   run     application runner same as 'emmarun' tool;
   instr   offline instrumentation processor;
   ctl     remote control processor;

   merge   offline data file merge processor.
   report  offline report generator;

  use '<command> -h' to see usage help for a given command

EMMA v2.1, build 5320 (stable)

Note 1: Any older version than that mentioned in the results will not function correctly for our needs.
Note 2: I have written some example only Perl code that looks for Web-INF/classes directories and
then generates a bash script that runs Emma and generates a metadata file, one per Webapp

Assuming you are running from a *NIX environment and the now standard bash environment and the target Sakai server has been shutdown. Otherwise, you will need to expand the variables and add the full paths.

java emma instr -ix - -ip {$SAKAI_HOME}/webapps/{$EMMA_INST}/WEB-INF/classes -m overwrite
-out= {$EMMA_REPORT_HOME}/{$EMMA_INST}.em

Variable

Description

SAKAI_HOME

File location of the root of your Sakai Server

EMMA_REPORT_HOME

File location for root dir for report generation

EMMA_INST

Webapp to instrument. E.G.: sakai-login-tool

EMMA_SRC_DIR

Location of condensed source code as mentioned in the $target variable in the example Perl code

Result: The code for the sakai-login-tool is now instrumented and a metadata file sakai-login-tool.em has been created in the report directory.

Generate report

Restart your server.
Near the beginning of the logging you will see an entry mentioning Emma is alive and listening at a given port number.

Collect session data and reset in memory data:
Note: We are running two commands coverage.get and coverage.reset

java emma ctl -connect localhost:47653 -command coverage.get,{$EMMA_REP0RT_HOME}/coverage.ec \
-command coverage.reset

Note: 1 The port number is 47653 is default and may be changed
Note: 2 If you want to start with fresh information remember to delete {$EMMA_REP0RT_HOME}/coverage.ec
Note: 3 In theory you may contact other instrumented systems not residing locally

Generate the actual report:

mkdir {$EMMA_REP0RT_HOME}/{$EMMA_INST}
java emma report -r html -sp {$EMMA_SRC_DIR} -in {$EMMA_REP0RT_HOME}/{$EMMA_INST}.em/coverage_demo.em -in \
  -Dreport.html.out.file={$EMMA_REP0RT_HOME}/{$EMMA_INST}/index.html