Java – How to measure Xtend translation time for each input file

How to measure Xtend translation time for each input file… here is a solution to the problem.

How to measure Xtend translation time for each input file

I’m building a DSL application in XTend, and I need to measure the time it takes translating every document I run in it.

I use the DSL feature to convert one data format (IFC) to another (TTL).

I

tried measuring it using System.currentTimeMillis() in the doGenerate method of the generator (myDslGenerator.xtend), but as far as I can tell, this only measures the time it takes to write to the output file, not the time it takes to parse the input file.

I don’t need the time it takes to load an Eclipse application, just the actual time (not CPU time) it takes to translate each file.

For tasks that take a few seconds (up to 20 seconds), my current (wrong) approach gives results between 0 and 500 milliseconds.

doGenerate was called from the ParallelBuilderParticipant class (handleChangedContents method) in the Eclipse dependencies, but I can’t trace it further to find where it was called. But maybe this method actually wraps parsing (Resource resource = contect.resource)?

Any insight would be great – thanks ????

Edit:
Try to provide more information here – hope it works.

Functional Eclipse code

Measuring the run time of the doGenerate method does give me the time it takes to write the “.ttl” file,

but the time it takes to read the “.ifc” file is not among them, as it is called on the doGenerate method. The doGenerate method is part of the XTend template, from AbstractGenerator.

Doing a usage search in the project, I can see that the doGenerate method is called from a class called ParallelBuilderPartieur.

Dependency: ParallelBuilderParticipant.handeChangedContents

If I measure the time

of this class instead, will I find all the time it takes to parse one file and write another? If so, what should I do because this is a dependent .class file – Eclipse does not allow me to edit it.

Solution

I

found a solution that was good enough for my needs, and since I spent a lot of time looking for solutions online and it took almost the same amount of time to make one myself, I thought I’d share it.

It may not be clear to me that I’m using a pre-built version of Xtend with Eclipse Oxygen. To measure the time it takes to parse files and write results, I connect to the lexical analyzer (InternalIfcBrickLexer.java) in the antlr package (org.xtext.ifcbrickconvert.parser.antlr.internal). In the constructor InternalIfcBrickLexer(CharStream input), I printed System.currentTimeMillis() to the console. In many cases, the lexical analyzer runs as the first process, including when you save.

In the generator (IfcBrickGenerator.xtend), I overridden the interface method void afterGenerate(Resource input, …) and printed System.currentTimeMillis again.

To avoid calculations, I created a static variable in the lexical analyzer and subtracted it from the current time in the afterGenerate method, so I can now complete lexical analysis, parsing, translation, and writing to a new file in milliseconds

Probably a little inaccurate because I don’t know if something can be called before the lexical analyzer, but from my initial tests it seems fairly accurate, and since the file I’m working on takes 20 – 2000 seconds to complete, a few hundred milliseconds isn’t particularly important.

A fallback to this approach is that it only needs to be run on one file – once you save the file and start processing, you can’t change any file – even if you don’t save it – otherwise you won’t get the right result. However, this is what I expected/wanted, so this is not a problem for me.

Thanks for the input – I hope someone finds this useful 🙂

Related Problems and Solutions