Archive of articles classified as' "Development"

Back home

On Eclispe buddy class loading and performance problems

9/02/2010

At work we have been experiencing serious performance problems with the Eclipse feature we are developing. We have just solved the problem and I would like to share the chain of events that caused it. I will elaborate on this a little bit, but in a few words:

  • If you want to wait for jobs in Eclipse Junit Plugin tests, use this method instead of the proposed in the second edition of the book Eclipse: Building Commercial-Quality Plug-Ins.
  • The global buddy class loading policy can affect performance seriously. It is logical and they warn you about it. I can confirm it is true.

Waiting for jobs in Eclipse JUnit Plugin Tests

The Eclipse PDE provides a launcher that let you test your plugins using JUnit. It basically instantiates a new Eclipse workbench and, when it is up, the tests are run inside it. When writing tests, a common need is waiting for jobs to finish. After launching a job for testing its results, the job is run in a separate thread, so the test code will continue running if you don’t stop it.

The book Eclipse: Building Commercial-Quality Plug-Ins is an excellent resource for learning Eclipse plugin development. It proposes some utility methods for using with Eclipse Plugin tests. One of this methods is indeed waitForJobs():

public void waitForJobs() {
  while (Platform.getJobManager().currentJob() != null)
    delay(1000);
}

This implementation worked well for us until one day we decided to divide a plugin that had become too big. I started by creating a new plugin extracting some code of the plugin along with its tests. But the tests always failed launching a NoClassDefFoundError exception. I was totally convinced that this problem was related to the way we had configured class dependencies between plugins. We use Groovy and the exception was always launched from the groovy libraries referenced from groovy compiled code. I spend a lot of time trying to figure out what was going on. It was a colleague who pointed out that it seemed that the test code was not waiting for the job under test to finish. And he was right. After searching in google I found a more robust implementation of the waitForJobs() method:

public static void waitForJobs() {
  long start = System.currentTimeMillis();
  while (!Job.getJobManager().isIdle()) {
    delay(500);
    if ( (System.currentTimeMillis()-start) > MAX_IDLE )
      throw new RuntimeException("A long running task detected");
  }
}

Using this approach solved the problem. This would had nothing to do with performance if I hadn’t messed up with the class loading configuration trying to solve it. Basically I made many of our plugins to use a global buddy class loading policy and, once the problem was solved, I didn’t undo it. And, while we perceived a poor performance in our plugin, it wasn’t until recently that we discovered the cause was precisely that global policy (we thought it was a problem with the development installation of the system our product connects to). And this brings me to the next section.

Eclipse Buddy Class Loading

The Eclipse core platform runs on a sophisticated runtime for Java components (Equinox, an OSGI implementation). This runtime provides a powerful architecture for installing and running Java modules (bundles or plugins). For doing so, it redefines completely the class loading system of the Standard Java Platform. When running inside Eclipse, each bundle has its own class loader. Bundles declare in their manifests how do they relate to other bundles, so their class loaders know how to locate external classes.

When a bundle A depends on a bundle B it can access its exported classes with no problem. The problem comes when it is B who wants to load classes from A dynamically, but it can’t depend on it (because it would create a circular dependence, or just because it doesn’t make sense). For example, the Log4j bundle can’t depend on a bundle that provides a log4.properties configuration file but it still wants to locate it in execution time. We have a number of scenarios like this in our application, where some bundles need to load classes/resources from other bundles they don’t know in execution time.

In these situations Eclipse Buddy Class Loading come to the rescue (another approach, better but more complex, is to use extension points for allowing plugins to be extended in execution time). Buddy policies allow one bundle to declare that it needs to load classes from other bundles (buddies). When a bundle class loader fails to locate some class, it starts searching for it in the declared buddies. The system can configure a number of policies that determine the way classes are searched:

  • dependent: search in bundles that directly or indirectly depend on the bundle.
  • required: search in bundles that explicitly declare themselves as buddies of the caller bundle.
  • global: search in all the bundles.

There are others policies available but I think the first two are the most common. Buddy policies are configured in the MANIFEST.MF file of the bundle that needs to load the classes. For example, in the manifest of the org.apache.log4j plugin you can find this:

Eclipse-BuddyPolicy: registered

Since it uses a registered policy, if you want your bundle to provide a properties file for log4j, you should declare your bundle as a buddy of Log4J.

Eclipse-RegisterBuddy: org.apache.log4j

With a dependent policy this last step is not necessary. As it is obvious, registered is more efficient than dependent, and this one is more efficient than global. In a product like our feature, which is big but not huge, when we changed from global to dependent we observed a huge performance boost, specially the first time the product functions were launched (which is when classes are loaded).

When you try to solve a mysterious bug for long time enough, you are so satisfied when yo do it that you don’t spend too much time blaming yourself for having produced it. And that was my state after this one…

No Comments

Using macros to create custom example groups in RSpec

16/01/2010

I am involved in a personal project where I am developing a web application with Rails. I am using RSpec and Cucumber, and I am following an outside-in, behavior-driven development approach, as recommended in the excellent RSpec Book. In this post I would like to share a problem I found and how I solved it.

It started when I tried to refactor some duplicated code in my controllers specs. It was related to the authentication system. Specs should test their behavior in both authenticated and insecure contexts, to be sure that anything happens if an anonymous user sends a POST action to your WorldDestroyerController. For example:

describe UserSessionsController, "DELETE destroy" do
  should_require_login :delete, :destroy

  context "authenticated user" do
    before(:each) do
      login_as_user
    end

    it "should destroy the user session" do
      @current_user_session.should_receive(:destroy)
      delete :destroy
    end

    it "should redirect to the login page" do
      delete :destroy
      response.should redirect_to login_path
    end  
  end
end

The next fragment was recurrent in all my examples.

context "authenticated user" do
  before(:each) do
    login_as_user
  end
 
  ...

For every example in the example group, it basically stubs the controller method that validates sessions in order to get a valid one when invoked (I am using authlogic). I wanted to create a macro context_authenticated so I could code the previous example like this:

describe UserSessionsController, "DELETE destroy" do
  should_require_login :delete, :destroy

  context_authenticated do
    it "should destroy the user session" do
      @current_user_session.should_receive(:destroy)
      delete :destroy
    end

    it "should redirect to the login page" do
      delete :destroy
      response.should redirect_to login_path
    end  
  end
end

My first attempt was to write a macro that included the before block and yielded to the block provided in the invocation:

def context_authenticated
  context "authenticated user" do
    before(:each) do
      login_as_user
    end
    yield #Wrong
  end
end

The examples failed because the before(:each) fragment was not being invoked before executing them. To discover the problem I had to investigate a little bit about how the RSpec DSL works. Basically:

  • Each context (alias for describe) creates a new ExampleGroup subclass.
  • Each it (alias for example) creates a new instance of the current ExampleGroup subclass where it is invoked.

So the problem was related to the very nature of closures. In Ruby, a block of code (proc or lambda) maintains the bindings in effect for that closure. The bindings contains not only variable references, but also the self reference itself.

In my case, what I was doing was to submit a closure with the it examples to the macro. The execution context of this closure was the one where it was defined: the ExampleGroup subclass created by describe UserSessionsController.... However, inside the macro, a new ExampleGroup subclass was created, and the before block was associated with that example group. That was the reason why the the before code was not getting executed before the submitted examples. Once I understood this, the solution was simple:

def context_authenticated(&example_group_block)
  example_group_class = context "authenticated user" do
    before(:each) do
      login_as_user
    end
  end
  example_group_class.class_eval &example_group_block
end

What the previous code does is to change the evaluation context of the closure, so it get executed inside the ExampleGroup class created by the macro.

No Comments

Evaluating code dynamically in Groovy (differences with Ruby)

10/10/2009

This week I found out that there are important differences between Ruby and Groovy when it comes to evaluate code dynamically. In order to write the testing infrastructure of a DSL we were developing, we wanted to invoke the code contained in isolated files in the context of the caller code. We were using Groovy, and I wrongly assumed that it would be as easy as with Ruby.

Imagine you have a file named say.script with the next contents:

say "Hello"

In this DSL, you can say things that are visualized in the STDOUT. Executing this script with Ruby is very simple: just invoke eval() with the code to evaluate it in a context where the say() method exists.

def say(message)
  puts message
end

eval File.read('say.script')

If you want to evaluate the code in the context of some object (for example, an expression builder), you can write something like:

class Context
  def say(message)
    puts message
  end
end

context = Context.new
code = File.read('say.script')
context.instance_eval code

However, in Groovy, the GroovyShell.evaluate() method is not equivalent to Ruby’s eval() method. While both mechanisms can share state between the caller and the invoked code through the concept of bindings, in Groovy the evaluate() method is always resolved in the context of a Script object. This means that the implicit invocation context is not shared. If you try to execute this code:

def say(message){
  println message
}

def shell = new GroovyShell()
def code = new File("say.script").text

shell.evaluate(code) //WRONG: in the script object the say() method is not defined

You will obtain something like:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: Script1.say() is applicable for argument types: (java.lang.String) values: {"Hello"}

A workaround for this situation is to convert the script code into a closure. This way, the DSL code is evaluated but not executed. With the closure object you can make the invocation in the context you prefer. In the last example, code bellow will make it:

def say(message){
  println message
}

def shell = new GroovyShell()
def code = new File("say.script").text

code = "{->${code}}"
def closure = shell.evaluate(code)
closure.delegate=this
closure()

In the example, we wrap the code to execute with a closure syntax. In this way, when we call evaluate we obtain a closure object. With this closure object we can modify its delegate (invocation context) and then call it.

Finally, if we wanted to execute the code using a specific invocation context (something like Ruby’s instance_eval), we just have to modify the delegate object:

class Context{
  def say(message){
    println message
  }
}

def shell = new GroovyShell()
def code = new File("say.script").text
code = "{->${code}}"

def context = new Context()

def closure = shell.evaluate(code)
closure.delegate = context
closure()

I don’t know of a better approach for solving this problem with Groovy. Initially I thought there will be a direct way, like in Ruby. But after hours of searching for it we didn’t find anything (which doesn’t mean it doesn’t exist, of course).

No Comments

The Composed Method implementation pattern

28/06/2009

Infoq is one of my favorite sources for reading general-purpose stuff on software development. Some time ago they published an excellent presentation: 10 Ways to Improve Your Code by Neal Ford. I just loved it. The first practice it focused on is the composed method implementation pattern, one of the most fundamental development practices I can think of.

It was formally described a lot of years ago, as a pattern, in Kent Beck’s book Smalltalk Best Practice Patterns. I havent’t read this book yet (it is arriving in my next Amazon’s order). Sincerely I don’t know anything about Smalltalk but people usually say that this book is recommendable for anyone interested in good programming practices. I’m sure I will enjoy it. I expect it to be better than Implementation Patterns, also by Kent Beck. While not a bad book, I thought it was going to be much better. In this book, it is said that you should:

Compose methods out of calls to other methods, each of which is at roughly the same level of abstraction

I don’t know why but I tended to call this implementation pattern as factorized code. I think it has to do with the fact that the first time I realized about its importance was reading Martin Fowler’s Refactoring book. It was a lot of years ago but I remember that in some section (I guess it was in the extract method refactoring pattern) the book said that when you have in your code something like this:

void someMethod(){
  //do some stuff
  line 1
  line 2
  line 3

  //calculate some other stuff
  line 4
  line 5
  line 6

  //do final stuff
  line 7
  line 8
  line 9
}

You should refactor to something like this:

void someMethod(){
  doSomeStuff()
  calculateSomeOtherStuff()
  doFinalStuff()
}

void doSomeStuff(){
  line 1
  line 2
  line 3
}

void calculateSomeOtherStuff(){
  line 4
  line 5
  line 6 
}

void doFinalStuff(){
  line 7
  line 8
  line 9 
}

It said that, instead of having a sequence of comments and lines of code, you should extract lines of code to methods with proper names. This way you could also remove comments, as they were redundant. If I remember something of the Refactoring book, is how impressed I was about Fowler’s coding style (the catalog is full of Java code samples).

In my first year at the University, we were taught about procedural programming. I remember being told that when you designed a program, you had to divide high-level routines into calls to lower-level ones. By doing this recursively, you obtained a tree, where leafs represented primitive routines.

Watching the way many people code it seems that this fundamental principle has simply being lost. It seems like if with objects, you only have to take care of carefully design the public contract of objects and their relations. While this aspect is very important, you have to pay a lot of attention to the design of internal code.

Although this pattern may seem very simple, applying it correctly is not as simple as saying you should divide methods into small steps. I think there are three fundamental properties that should rule methods design:

  • Cohesion
  • Clear interfaces
  • Symmetry

Methods should be highly cohesive. This means that methods should focus in a single responsibility or functionality. This property comes from the software quality field, usually applied to modules or classes. Applying it when designing methods implies that you shouldn’t have methods that do many things from the point of view of the client that is using it. Who is the client depends on the abstraction level where the code is being invoked. Could be another class o just a private method inside in the same one. A method that receives five parameters is usually a bad smell.

Methods should have clear interfaces. In Writing Solid Code Steve Maguire says that the signature of all methods (functions in those days) should be clear enough in order to understand what the method is supposed to do, without any other kind of documentation. Signature means the name of the method as well as of the parameters it receives and returns (and their types). Achieving this involves many things but without cohesion is just impossible.

The book shows a good example of how a method should not being designed: the C realloc() function. It just does many things with a very obscure interface, which you won’t understand without the documentation. The book also contains a very representative example from the physical word, the candy machine interface problem: when the candy machine offers numbers for both selecting products and displaying their prices. Methods should not offer a candy machine interface.

The composition of methods should also be symmetric. The steps a method is divided in should be at the same level of abstraction. Abstraction is a construction that a person does in order to understand something. Jumping from one level of abstraction to another requires mental effort. If you have to keep jumping between different levels of abstraction in order to understand some code, it will be far more difficult to understand.

In conclusion, while this pattern will improve the quality of your software, I use it because for me it is easier to program this way. It is just the principle of divide and conquer working. I usually code methods following a top-down approach. Writing the top level methods calling lower-level methods before they exist. This way I can directly project the structure of steps that I, like a human, have in my head. Other people prefer to write the code without decomposing it first, and then refactorize to extract methods (I think this approach is far less effective).

Anyway I think that if you apply this practice exhaustively, along with using proper names for naming identifiers, your code will be like a 400% better.

No Comments

Generating JUnit Test Cases Dynamically

18/09/2008

In this post I’m going to talk about generating JUnit Test Cases Dynamically. In order to define tests, most JUnit’s users place their testing code inside test methods that

  • Start with test
  • Don’t have arguments

Given a TestCase object, JUnit TestSuites can dynamically extract test methods that follow these conventions and run them on the fly. However, this behavior isn’t enough in some cases. The problem of this approach is that, from the developer’s point of view, tests must be written statically.

Imagine you have a directory with many text files. For each file, you want to do the same kind of testing. Since the test result depends on each file, what you really want is to generate dynamically test cases. Of course you can have a single test method, and do all the testing there. The main problem of this approach is that Test Runners won’t be able to collect results properly: a single fail with one file would result as a failure. You won’t notice which tests are valid and which aren’t.

Generating tests cases dynamically is very simple. JUnit’s design itself is simple. This is one of the main reasons of why this framework is so successful. JUnit was created by Kent Beck and Erich Gamma. It represents an excellent oportunity to see GOF design patterns in action (in the same way that JHotDraw, another framework created by Erich Gamma as a design patterns exercise). In JUnit, a Test Case is, surprisingly, a Test Case. A Test Suite is a composite of Test Case. You can create a Test Suite as a normal object and, programmatically, populate it with as many instances of a test case as you want.

Let’s see a very simple example. Imagine you have the following structure of folders:

Folder Structure for the sample

For each folder, you want to create a Test Suite. For each file, you want to test that its extension must be .txt. In this way, you are going to create a parallel structure using test suites and test cases.

Below, the test suite is shown. It receives a directory in the constructor. It goes through all the contained files and subdirectories. For each subdirectory, a new TestSuite is created and added to the composite (recursively). For each file, a new TestCase is created and added in the same way. The actual testing is going to be done in these test case instances.

public class TextFileTestSuite extends TestSuite {
  public TextFileTestSuite(File directory) {
    super(directory.getName());
    for (File file : directory.listFiles()) {
      if (file.isDirectory())
        addTest(new TextFileTestSuite(file));
      else
        addTest(new TextFileTestCase(file));
    }
  }
}

The test case’s implementation is very straighforward. It receives the concrete file to test in the constructor. It also overrides the runTest() method, where the real testing code is placed.

 public class TextFileTestCase extends TestCase {
  private File file;
 
  public TextFileTestCase(File file) {
    super();
    this.file = file;
    setName(file.getName());
  }
 
  protected void runTest() throws Throwable {
    assertTrue(file.getName().endsWith(".txt"));
  }
 }

And that’s all. You can now check the results on a Test Runner:

 public class AllTests {
  private static final String TEST_FOLDER = "testdata";
 
  public static Test suite() {
    TestSuite suite = new TestSuite(
        "Test for net.jorgemanrubia.junitdinamically.sample");
    //$JUnit-BEGIN$
    suite.addTest(new TextFileTestSuite(new File(TEST_FOLDER)));
    //$JUnit-END$
    return suite;
  }
 }

With the exposed structure of folders, you will obtain something like this:

Test Results using Eclipse's JUnit Test Runner

No Comments