Showing posts with label xtext. Show all posts
Showing posts with label xtext. Show all posts

Tuesday, September 03, 2013

Introduction to Xpect

Xpect is a framework that helps you to test, discuss, and explain Xtext languages.

Before I start, I'd like to tell a bit about me, since it gives a context on why I created Xpect. As you may know, I've been a committer at the Eclipse Xtext project since January 2009. Besides working on Xtext (and Xtend), I have been applying this technology in several customer projects: Project sizes varied from man days to man years and my role varied between designing a language and providing professional support for technical challenges.

All projects have in common that we wanted to develop Xtext languages. Sometimes we were free to design syntax and semantics from scratch and sometimes we were bound to a language specification. In most projects we needed to...

  • have automated tests for our language tooling.
  • discuss design and implementations decisions.
  • explain the language and tooling to fellow developers and prospective users.
  • allow our implementation to be reviewed regarding completeness and correctness. I.e. pass an acceptance test.
  • set up test stubs for fellow developers to implement tests.

To comply with these use cases I found myself writing plenty of JUnit tests and creating many example documents of the language, often with redundant content. I could not use the JUnit test to explain and discuss the language with others, since the Java code was distracting them from what I wanted to show. Furthermore, I was annoyed that I could not use the language's Xtext editor to craft test data, since the test data was embedded in string literals in the test's Java code. Also, when I wrote example documents, I found myself inserting comments to explain the language: "This expression will evaluate to xxx", "This statement causes error yyy", "This cross reference links to zzz", etc.

The idea arose to converge test cases and example documents: What if I could embed test expectations within comments inside my example documents? What if I could find a format for these comments so they're both human understandable and automatically verifiable by a test suite?

Xpect realizes this idea. The version introduced by this blog post or older versions are actively being used by in-house projects, several customer projects, and open source projects such as Xcore and Spray.

One File, Two Languages

What you can see in the screenshot above is a JUnit that has been executed and which passed. The test class is org.domainmodel.tests.validation.DMValidationTest and it has executed a file named test1.dmodel.xt. The file defines two test cases, one called warnings and one called errors. On the right hand side you can see the file's contents in an editor. The editor generically combines support for two languages: First, the language that is being tested. In the screenshot this is the Domainmodel language (*.dmodel) which ships with Xtext as an example language. Secondly, the editor supports the Xpect language (*.xt). The two languages do not interfere with each other since the Domainmodel language ignores text insides comments and the Xpect language ignores text that doesn't start with an "XPECT" keyword. The editor applies a greenish background color to Xpect syntax.

Let's take a closer look at the Xpect syntax. At the beginning of test1.dmodel.xt, there is a region called XPECT_SETUP. It holds a reference to the JUnit test that can run this file. Further down we find test cases such as:

// capitalized property names are discouraged

// XPECT warnings --> "Name should start with a lowercase" at "Property1"

Property1 : String

The first line is an optional title for the test. warnings references a JUnit test method (implemented in Java) and the part following the --> is the test expectation, which is passed as a parameter into the JUnit test method. The test method can then compare the test expectation with what it calculated to be the actual test value and pass or fail accordingly. For this example, the test expectation is composed of the error/warning message (Name should start with a lowercase) and the text that would be underlined by the red curly line in the editor (Property1). For this validation test, the XPECT statement collects all errors or warning occurring in the next line. A feature you might find very useful is, that the XPECT statement consumes error or warning markers: An expected error or warning will not be shown as an error marker in the Eclipse editor.

Synchronize Expectations and Implementation

The fact that Xpect uses textual expectations and embeds them into DSL documents opens the door for another awesome (IMHO) feature: Using the Eclipse comparison editor to inspect failed tests and to fix out-dated test expectations:

When one or more tests fail and you want to fix them, it is crucial to quickly get an overview over all failed tests. With Xpect you can not only see all failed test cases from one file in a single comparison editor, but there are also no assert statements which sometimes prevent execution of follow-up assert statements and thereby hide valuable hints on why the test failed. The comparison editor, as the name suggests, also lets you edit the test file.

Reusable Test Library

In Xtext projects, there are several scenarios where it is reasonable to have test coverage. The validation test I explained earlier in this article is just one of these scenarios. Xpect ships the Java-part for such tests as a library. There is also an example project that demonstrates their usage.

There are test for:

  • The parser and Abstract Syntax Tree (AST) structure (demo only, no library).
  • Code generators implemented via Xtext's IGenerator interface.
  • Validation: Test for absence, presence, message and location of errors and warnings.
  • Linking: Verify a cross reference resolved to the intended model element
  • Scoping: Verify the expected names are included or excluded from a cross references's scope.
  • ResourceDescriptions: Verify a document exports the intended model elements with proper names.
  • JvmModelInferrer: For languages using Xbase, test the inferred JVM model

There will be more tests in future versions of Xpect.

Support for Standalone and Workspace Tests

UI-independent parts of Xtext, such as the parser, can operate standalone (i.e. without OSGi and Eclipse Workspace). The same is true for Xpect. For capabilities where Xtext does not require OSGi or an Eclipse Workspace, Xpect does not do so either. Consequently, Xpect tests can be executed as plain JUnit test or Plug-In JUnit tests.

Since both scenarios require different kinds of setups, both can be configured separately in the XPECT_SETUP section. When executed as plain JUnit test, the ResourceSet-configuration is used and for Plug-In JUnit tests, the Workspace-Configuration is used.

The screenshot also illustrates how an additional file can be loaded so that it is included in the current ResourceSet or Workspace during test execution.

Suites: Combine Tests in the Same File

When explaining a specific concept of a language, it is helpful to look at the language concept from all sides: This is the valid syntax, these scenarios are disallowed, this is how cross references resolve, this is how it is executed, etc. So far, testers were required to create a new test file for every technical aspect. With Xpect test suites, however, it is possible to combine the Java-parts of various tests into a single test suite.

The test suite XtextTests combines several tests so that in CombiningMultipleTests.dmodel.xt test methods from all these tests can be used. This allows to group tests by language concept.

Works with CI Builds

Xpect tests have proven to run fine with Maven, Tycho, Surefire, Buckminster and Jenkins. No special integration is necessary since Xpect tests run as JUnit tests.

Source & Download

You can find a link to the update site at www.xpect-tests.org. The source code and an issue tracker are on the github project page.

Thursday, April 25, 2013

Probes or How to Analyze Application State in a Profiler

Disclaimer: I'm not affiliated with Yourkit, but I am grateful for their free licenses for open source projects. I've been using Yourkit for my work on Xtext and Xtend.


Introduction

Improving the performance of an application can be a challenging thing to do. As a developer, you usually have some immediate ideas of what are the performance bottlenecks. However, as Ed said in his presentation at EclipseCon North America 2013, "Don't trust yourself, don't trust your friends, and don't even trust me."

I fully approve his message. Why? Experience shows there is truth in this statement.

If trust doesn't work, we need facts: We need to measure. We need to measure how much time it takes to execute our program for specific tasks. Two tools probably come to your mind as you read this:

  • java.lang.System.nanoSeconds(): This simple but useful method gives you access to a decently precise time stamp at any time. You can call this method from your own code and calculate and report the time for what every your desire. But there's a catch: You need to modify the code that you want to inspect, which you sometimes can't do because the code is part of a library/framework or you just don't want to pollute the code.
  • A Profiler (such as Yourkit): This powerful tool lets you measure measure the execution time of every since method invocation. Sounds awesome? Yes, but there are some problems with it:
    • The "lost in the jungle syndrome": The result of a profiling session are a bunch of trees representing all states of the call stack your application had during the profiling session. And just like in a jungle, these trees are incredibly huge. Sometimes the profiler can point out some performance hot spots, but I think that only works well if you have some very obvious performance problems. So you end up spending a lot of time climbing through the trees, considering for every branch whether its existence and time consumption is justified.
    • Observing a system changes it: A profiler usually uses a java agent, which is a hook in the JVM that allows to manipulate class files before they're executed. This way the profiler can inject byte code for performance measurements into the to-be-analyzed classes. Surely this injected code is small and fast, but it can have an impact on how the Java JIT can optimize the byte code. Example: A method that could be in-lined before may not be in-lined anymore. I don't mind if an application runs slower during a profiling session, but I do mind if the times measured during profiling are disproportional to the actual times.
    • Parameter values are not available: To understand what the code is doing, you sometimes need to know the values of the method parameters. Consider questions such as: Does a certain value occur so often that introducing an early-exit to the method would improve the speed? Does the same value occur because the method was accidentally called from within a loop? Would it help to cache the result of the method?

Probes to the Rescue

Yourkit allows you to have so-called Probes. Here are some facts in a nutshell:

  • A probe is a Java class that hooks into the invocation of methods of the to-be-analyzed code. Such hooks can be registered to be invoked before or after a method has been invoked.
  • Installing pre-/post-invocation hooks into methods may sounds familiar to you from Aspect Oriented Programming (AOP). It's indeed the same kind of hook, but unlike in AOP you don't need to a special compiler: Yourkit registers the probes at runtime via its Java agent. Therefore, probes don't change your build and are applicable for already compiled libraries.
  • You can use probes independently of CPU profiling (sampling/tracing). A probe is active once it has been registered and you can choose not to enable CPU profiling at the same time. This may safe you from getting the "Observing a system changes it" bias.
  • Yourkit ships with a set of predefined probes for standard events, such as Tread lifecycles, file system I/O, etc. You can as well implement your own probes.
  • In a probe's implementation, you can access the hooked method's parameter- and return values.
  • Youkit encourages you to build a Java data structure for your measured data that resembles a relational data base. If you follow that pattern, Yourkit has a nice graphical user interface to explore your measured data.

Probing EMF ResourceSets and Resources

As an example and for further illustration, I've build a small probe that tracks instantiation of EMF ResourceSets and invocation of EMF Resources. In the terminology of the Eclipse Modeling Framework (EMF) a Resource usually represents a file from the file system. A resource furthermore provides implementations to load a file into memory as an object tree (aka model) or persist a tree back to disk.

The following screenshot shows an Eclipse IDE with two open editors. One editor shows an *.xtend file and the other one a *.genmodel. Even though both use EMF under the hood, they have little in common:

  1. The *.xtend text-editor shows a file persisted as plain text; It's Resource.load() implementation delegates to an ANTLR parser.
  2. The *.genmodel tree-editor shows the structure of a file that is persisted as XML. Here, an XML parser is used.

The Yourkit probe was registered while opening both editors. The following screenshot shows the measured data:

  • In the tree view on the left hand side you can see all available probes. The probe "EMF ResourceSets" has been developed by me, all others ship with Yourkit. The checkbox does not register/unregister the probe. The checkbox shows/hides the data measured by the probe in the views on the right-hand side of the tree-view.
  • The table on the top on the right-hand side shows a list of all ResourceSets that have been instantiated. The AdapterFactoryEditingDomainResourceSet is the one used by the *.genmodel editor. As you can see, there was more going on in my Eclipse instance before I took the screenshot. A click on the column labeled "stack trace" would reveal how a specific ResourceSet has been instantiated.
  • The table on the bottom right-hand side shows all invocations of Resource.load() for the ResourceSet that is selected in the table above. Here we can see that tracking of application state is possible: The column "uri" displays the file name of the file that has been loaded. The "wall time" is the time in milliseconds that was spent executing the load() method.

Implementing a Probe

You can find the full source code of this example here on github.

Before we start implementing a probe, some considerations:

  • The code that the probe hooks into needs to have the probe implementation on the classpath. Furthermore, it is helpful if the probe has the code it hooks into on the classpath, since otherwise the probe implementation would need to use Java reflection to inspect the to-be-analyzed object. This sounds tricky since it is in fact a cyclic dependency between the probe and the inspected code. Luckily Eclipse has a simple mechanism to contribute to a plugin's classapth: Fragments. A fragment is a bundle with class files, own dependencies and extension points that at runtime is integrated into the classpath of a chosen host-plugin. We choose org.eclipse.emf.ecore to be that host-plugin.
  • Probes need to be registered: We could do that by changing the JVM's startup arguments, but in an Eclipse environment it seems more convenient to use the extension point org.eclipse.ui.startup.
  • The probe implementation uses Java API provided by Yourkit. This API ca be found in /lib/yjp.jar inside the Yourkit installation.

The next screenshot shows the the implementation of the probe. It is a java class with static members to

  • define the data structures for storing the measured values (TABLE_RESOURCESET, TABLE_RESOURCE).
  • intercept calls to ResourceSet constructors (ResourceSetInit) and Resource#load() invocations.
  • map ResourceSet-Objects to Table-Row-IDs (rs2row and getResourceSetRow()).

Now that you've had an overview over the probe implementation, here is finally the core, the actual hooks: Each method-hook is a static Java class that can implement the static methods onEnter() and onReturn(). Annotations are used for further configuration:

  • @MethodPattern specifies the signature of the to-be-intercepted method.
  • @This gives access to the object on which the method has been called.
  • @OnEnterResult gives access to the return value of the onEnter method.
  • @Param(1) gives access to the first parameter of the intercepted method.
  • etc.

Conclusion

  1. Probes can deliver concise and precise performance measurements for an application or a group of applications that are based on the same frameworks/libraries.
  2. Probes have the potential to define reasonable performance metrics for specific applications that can be shared across a team of developers.
  3. Probes cause significantly less side-effects compared to general profiling.
  4. Probes can make use of more information than a profiler, such as application state, parameter values and return values.
  5. Implementing probes is some effort. This may be of less significance when probes can be reused. However, in scenarios were reuse in not intended - for example the explorative search for performance issues - their use may be too much effort.