Wednesday, October 28, 2009

Benchmarking Java method performance

Very often there is a need to make a quick estimation of which of several method implementations performs better. A typical question is how many fractions of second it would take for a single invocation to complete. With Java (and any other JIT-based virtual machines) the estimation is especially difficult as certain number of initial invocations may be done in the interpreted mode with later switching to the compiled form plus some other optimizations may occur. Sometimes though the exact call duration may not be of much interest. We'd rather find out which implementation performs significantly better by comparing the performance in a simulated usage scenario. For that purpose I wrote a few classes that can facilitate that micro-benchmarking.

Wednesday, October 21, 2009

Parsing command-line options - part 4

This is the last part of the story. This time we will search for a way to make our annotation work.

Tuesday, October 20, 2009

A hint for indenting with XMLStreamWriter

Default implementation of javax.xml.stream.XMLStreamWriter does not support such output features as indentation and multi-line output. Correspondingly the resulting text is always a long one-line string which many of us would want to format in a pretty style for better reading. There is a solution bundled with Java 6 although it is sort of internal Sun facility which may have gone one beautiful day.:-) The class is named com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter, it is public, and it resides in rt.jar. Using it is a matter of couple of lines in your code:
        final XMLStreamWriter defaultWriter =
            XMLOutputFactory.newInstance
().createXMLStreamWriter(writer);
       
final IndentingXMLStreamWriter sw = new IndentingXMLStreamWriter(defaultWriter);
        sw.setIndentStep
("    ");

As shown above it allows you to vary the indent character sequence giving certain flexibility by that.
The only problem I can think of might be due to the end-of-line character internally hard-coded by '\n'. I would prefer having system-dependent or, better, a user-provided line terminator instead.
P.S. Just discovered that this class is only included into JRE not into JDK. That means one sure way to solve the original problem is to duplicate the class source in your application. There is nothing special in its logic as it routinely decorates an instance of XMLStreamWriter with the required functionality. The source code for JDK 6 can be downloaded from this page.

Monday, October 19, 2009

Parsing command-line options - part 3

So we have defined two interfaces - 1) the interface we want to use to access command-line values, - a real-world example of usage, and 2) the annotation we want to apply to the interface #1 to furnish it with option-specific information. It's time to start writing some code to bind them together.

Thursday, October 15, 2009

Parsing command-line options - part 2

To set off with the further activities I have to capture the list of requirements first. This list will let me grasp the overall scope of work and possibly start with a set of unit-tests.

Enterprise Data Fabric is what we need?

I have just watched this presentation on Coherence by Cameron Purdy, a vice president in Oracle. Well, I think it is the very right direction they have been moving at so far. I encountered similar but custom solutions at least in three big projects at different companies. I must admit that no other architecture can fit equally well to the modern business requirements where delivering the most recent values across the enterprise is important (this is true for almost every modern application).

Frankly, I feel incredibly envious about the work they have been doing in Tangosol(now Oracle) :-). Quick search in Google and Wikipedia yields that there is a term for the technology and it is Enterprise Data Fabric (EDF) which sounds good to me.I feel like writing my own implementation of EDF based on experience I have got with that kind of systems.

Nowadays the biggest problem is that integration of systems has typically been done in a totally chaotic way. Even while some enterprise applications are using Coherence (or GemStone GemFire, or similar), others are not and will never be able to due to major architectural flaws making them incompatible with EDF. There are still so many applications designed on top of a relational database, for example. Honestly, almost every such system is cr*ppiest legacy nightmare, - totally inflexible, hard to maintain, so far away from low latency data delivery - and thus very inefficient.

Parsing command-line options - part 1

I favor metaprogramming. Annotations in Java and Attributes in C# fit this technique very well, making your code concise and simple.

A good excerise where metaprogramming may help is mixed GNU/POSIX-style option processing for command-line utilities. A descriptive explanation for this use-case is given here.

A bug story

Ha-ha...Almost introduced undesired behavior for a Hibernate-managed class when noticed that on commit I forgot to uncomment the line:
//@org.hibernate.annotations.Entity(mutable = false)
Guess how I have fixed it on the first go?
//@org.hibernate.annotations.Entity(mutable = true)
:)))

Tuesday, October 13, 2009

Oracle WTF

Maximum identifier length is 30 (!!!) bytes... Are they crazy? That's nonsense. Of course, renaming Rollback Segments to Undo Segments was much more important.

Monday, October 12, 2009

Quick way to create an uppercase letter-only string from a long value.

Today I had to find a quick way to convert the current time in milliseconds into an uppercase letter-only ([A-Z]+) string with possibly minimal length. java.util.UUID did not seem to fit well to the purpose as it produces [0..9A..F]+ that requires extra conversion so I come up with the following:

    public static String makeUppercaseLetterOnlyStringFromCurrentTime() {
        final long ts = System.currentTimeMillis();
        // convert the value to range of 0..9a...z characters
        final String chars = Long.toString(ts, 26);
        final StringBuilder sb = new StringBuilder();
        for (byte b : chars.getBytes()) {
            final int ch = (intb;
            // for a digit subtract '0' and add to 'A', 

            // for a letter - subtract 'a' and and add to 'A'
            final int x = b + ((Character.isDigit(b)) 0x11 : -0x20);
            sb.append((charx);
        }
        return sb.toString();
    }

This for sure is not the fastest nor the most efficient approach yet quite straight-forward one. :-)

Monday, October 5, 2009

Why use Oracle?

I once had been extremely fascinated by Oracle RDBMS and was quite a good DBA at that time (in the sense I could properly set up and run an instance while being able to quickly solve most of the issues). If I'm not mistaken it was about 15 years ago.

Thursday, October 1, 2009

Posting code snippets...

Eh, that's going to be tough. I thought of ocassionally posting Java and C# code snippets with syntax coloring and nice formatting. Googling for the problem shows that's not a simple thing though. Got to search for a good solution...
This is what Java2Html Eclipse plugin produces when using inline fonts:
1     public static boolean equals(final Object a, final Object b) {
2         return a != null && b != null && a.equals(b);
3     }

Looks fine so far... Later I'll switch to customized stylesheets.

Never thought I'd start a blog

Namaste!