thekrakken / java-grok

Simple API that allows you to easily parse logs and other files
http://grok.nflabs.com/
Other
360 stars 151 forks source link

Gson are now static final fields to improve performance #46

Closed jrask closed 8 years ago

jrask commented 8 years ago

Instead of creating a new Gson instance for each invocation, they are not static final fields to avoid the overhead of creating them each time. The Gson instance is threadsafe so there should be no issues (anymore).

I did a naive performance test with the old and my suggested approach. I also did a test to compare with jackson but could not find any notable difference.

Time in milliseconds

Old: 4032 2547 2034 2146 2007 2025 1951 2041 2028 2021
New: 2350 1332 1247 1232 1263 1258 1259 1224 1295 1299

The difference is quite big, "almost" a second per 150.000 invocations.

    @Test
    public void naivePerfTest() throws GrokException {
        for( int i = 0; i < 10; i++) {
            runWithTimer();
        }
    }
    public void runWithTimer() throws GrokException {
        Grok grok = Grok.create("patterns/patterns", "%{URI}");
        long start = System.currentTimeMillis();
        for (int a = 0; a < 150000; a++) {
                Match m = grok.match("telnet://helloworld");
                m.captures();
                m.toJson();
        }
        System.out.println(System.currentTimeMillis() - start);
    }
anthonycorbacho commented 8 years ago

Nice improvement, thank you for the patch

jrask commented 8 years ago

@anthonycorbacho Is it possible to have a new version with this feature released? Would make my life a little bit better ;-)