Thursday, October 01, 2009

A simple load test in Terracotta...

This is a response to the following blog in which the author wrote a micro-benchmark and got some pretty bad results using Terracotta http://zion-city.blogspot.com/2009/10/terracotta-as-distributed-dbms-bad-idea.html.

Since the commenting system on blogger doesn't allow code, I am posting the response on my blog with code attached for reference.

So my approach was to try replicate the author's implementation, to see what kind of performance a straightforward micro-benchmark might achieve.

Reader beware - micro-benchmarks are never a good idea, and not usually indicative of real-world performance. In this case, based on real-world results I have seen, my results appear to be a lower bound for the kind of performance one should expect since the test isn't concurrent and is running on a single machine - hardly the kind of environment a real world clustered app would exist in)

So, with that said, I wrote a simple load test against a ConcurrentHashMap, and put 100,000 objects into it.

My results show:
Avg TPS: ~3,000
Instantaneous TPS as high as: ~7,000

Here's the code:

import java.util.Date;
import java.util.Map;
import java.util.concurrent.*;

public class Main
{
  static Map<Integer, Foo> map = 
    new ConcurrentHashMap<Integer, Foo>();

  public static class Foo
  {
    public String name;
    public String name2;
    public String name3;

    public Foo(String name)
    {
      this.name = name;
      this.name2 = name + " 2";
      this.name3 = name + " 3";
    }
  }

  public static void main(String[] args)
  {
    long start = System.currentTimeMillis();

    for (int i = 0; i < 100000; i++) {
      map.put(i, new Foo(new Date().toString()));
    }
    System.out.println("elapsed: " + (System.currentTimeMillis() - start));
  }
}


And here's the tc-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tc:tc-config xmlns:tc="http://www.terracotta.org/config"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-5.xsd">
  <application>
    <dso>
      <instrumented-classes>
        <include>
          <class-expression>Main$Foo</class-expression>
        </include>
      </instrumented-classes>
      <roots>
        <root>
          <field-name>Main.map</field-name>
        </root>
      </roots>
    </dso>
  </application>
</tc:tc-config>


I took a screenshot of the dev console running during the test, to give you an idea of the instantaneous TPS achieved: