Wednesday, August 31, 2011

The Exchanger and GC-less Java

Overview

The Exchanger class is very efficient at passing work between thread and recycling the objects used. AFAIK, It is also one of the least used Concurrency classes.
If you don't need GC less logging using an ArrayBlockingQueue is much simpler.

Exchanger class

The Exchanger class is useful for passing data back and forth between two threads. e.g. Producer/Consumer. It has the property of naturally recycling the data structures used to pass the work and supports GC-less sharing of work in an efficient manner.
Here is an example, passing logs to a background logger.
Work (a log entry) is batched into LogEntries and passed to a background thread which later passes it back to the thread so it can add more work. Provided the background thread is always finished before the batch is full, it is almost transparent. Increasing the size of the batch reduces how often the batch is full but increase the number of unprocessed entries waiting at any one time. Calling flush() can push out the data.
The key line is the following which exchanges the batch in the current thread with the batch in the other thread. The producer fills up the batch while the consumer is emptying it.
The exchange when it occurs typically takes 1-4 micro-seconds. In this case, once every 64 lines.

entries = logEntriesExchanger.exchange(entries);

How does this compare to the LMAX disruptor pattern

This approach has similar principles to the Disruptor. No GC using recycled, pre-allocated buffers and lock free operations (The Exchanger not completely lock free and doesn't busy wait, but it could)
Two keys difference are:
  • there is only one producer/consumer in this case, the disruptor supports multiple consumers. 
  • this approach re-uses a much smaller buffer efficiently. If you are using ByteBuffer (as I have in the past) an optimal size might be 32 KB.  The disruptor library was designed to exploit large amounts of memory on the assumption it is relative cheap and can use many GBs. e.g. it was design for servers with 144 GB.  I am sure it works well on much smaller servers. ;)
Thank you @Doug, for reminding me to mention the Disruptor pattern.

If you have dozen logs files (for different purposes) and you want to minimise memory foot print and you prefer the consuming thread to be blocking rather than busy waiting which consumes 100% of a thread (which adds a small latency of up to 10 us) then the Exchanger is better suited.

Exchanger example

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BackgroundLogger implements Runnable {
  static final int ENTRIES = 64;

  static class LogEntry {
    long time;
    int level;
    final StringBuilder text = new StringBuilder();
  }

  static class LogEntries {
    final LogEntry[] lines = new LogEntry[ENTRIES];
    int used = 0;
  }

  private final ExecutorService executor = Executors.newSingleThreadExecutor();
  final Exchanger<LogEntries> logEntriesExchanger = new Exchanger<LogEntries>();
  LogEntries entries = new LogEntries();

  BackgroundLogger() {
    executor.submit(this);
  }

  public StringBuilder log(int level) {
    try {
      if (entries.used == ENTRIES)
        entries = logEntriesExchanger.exchange(entries);
      LogEntry le = entries.lines[entries.used++];
      le.time = System.currentTimeMillis();
      le.level = level;
      return le.text;

    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
  }

  public void flush() throws InterruptedException {
    if(entries.used > 0)
        entries = logEntriesExchanger.exchange(entries);
  }

  public void stop() {
    try {
      flush();
    } catch (InterruptedException e) {
      e.printStackTrace(); // use standard logging.
    }
    executor.shutdownNow();
  }

  @Override
  public void run() {
    LogEntries entries = new LogEntries();
    try {
      while (!Thread.interrupted()) {
        entries = logEntriesExchanger.exchange(entries);
            for (int i = 0; i < entries.used; i++) {
              bgLog(entries.lines[i]);
              entries.lines[i].text.delete(0, entries.lines[i].text.length());
        }
        entries.used = 0;
      }
    } catch (InterruptedException ignored) {

    } finally {
      System.out.println("Warn: logger stopping."); // use standard logging.
    }
  }

  private void bgLog(LogEntry line) {
    // log the entry to a file.
  }
}

Sunday, August 28, 2011

Java Insanity: Two methods with the same super.method()

Overview

Recently I wrote an article on how you could write multiple methods with the same name and parameters by using generics. I wondered if you can have multiple methods with the same super.method()

Generics and signatures

The way the Java compiler works, the generic is effectively part of the signature. However, due to type erasure, the JVM doesn't care so how can this work? The answer is that for the JVM the return type is part of the signature. This means if you have two method with different generic signatures and return types they can have the same name and parameter types.

For the purposes of determining a parent super.method(), they share the same method. So if you have two methods overriding a method, which one is actually called. The answer is neither, polymorphism breaks and the parent still gets called.
static class A {
    public Number method() {
        System.out.println("Inside: Number A.method()");
        return 0;
    }
}

static class B extends A {
    public <T extends Integer> T method() {
        System.out.println("Inside: Integer B.method()");
        super.method();
        return (T) (Integer) 0;
    }
    public <T extends Long> T method() {
        System.out.println("Inside: Long B.method()");
        super.method();
        return (T) (Long) 0L;
    }

}
public static void main(String... args) {
    B b = new B();
    b.<Integer>method();
    System.out.println("=============");
    b.<Long>method();
    System.out.println("=============");
    A a = b;
    a.method();
}

prints
Inside: Integer B.method()
Inside: Number A.method()
=============
Inside: Long B.method()
Inside: Number A.method()
=============
Inside: Number A.method()

The byte code

For anyone who doubts this even compiles here is the byte code for your interest.

Main byte code

$ javap -c -classpath . Main
Compiled from "Main.java"
public class Main extends java.lang.Object{
public Main();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   new     #2; //class Main$B
   3:   dup
   4:   invokespecial   #3; //Method Main$B."":()V
   7:   astore_1
   8:   aload_1
   9:   invokevirtual   #4; //Method Main$B.method:()Ljava/lang/Integer;
   12:  pop
   13:  getstatic       #5; //Field java/lang/System.out:Ljava/io/PrintStream;
   16:  ldc     #6; //String =============
   18:  invokevirtual   #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   21:  aload_1
   22:  invokevirtual   #8; //Method Main$B.method:()Ljava/lang/Long;
   25:  pop
   26:  getstatic       #5; //Field java/lang/System.out:Ljava/io/PrintStream;
   29:  ldc     #6; //String =============
   31:  invokevirtual   #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   34:  aload_1
   35:  astore_2
   36:  aload_2
   37:  invokevirtual   #9; //Method Main$A.method:()Ljava/lang/Number;
   40:  pop
   41:  return

}

Main.A byte code

$ javap -c -classpath . Main\$A
Compiled from "Main.java"
class Main$A extends java.lang.Object{
Main$A();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   return

public java.lang.Number method();
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc     #3; //String Inside: Number A.method()
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   iconst_0
   9:   invokestatic    #5; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
   12:  areturn

}

Main.B byte code

You can see on line 9 of each method that they call the same parent class.
$ javap -c -classpath . Main\$B
Compiled from "Main.java"
class Main$B extends Main$A{
Main$B();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method Main$A."":()V
   4:   return

public java.lang.Integer method();
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc     #3; //String Inside: Integer B.method()
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   aload_0
   9:   invokespecial   #5; //Method Main$A.method:()Ljava/lang/Number;
   12:  pop
   13:  iconst_0
   14:  invokestatic    #6; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
   17:  areturn

public java.lang.Long method();
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc     #7; //String Inside: Long B.method()
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   aload_0
   9:   invokespecial   #5; //Method Main$A.method:()Ljava/lang/Number;
   12:  pop
   13:  lconst_0
   14:  invokestatic    #8; //Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
   17:  areturn

}

Friday, August 26, 2011

Self Signed SSL certificate creation sing keytool!

First, we will generate a keystore that has a key pair (public and private key) along with a self-signed certificate.
1) keytool -genkey -alias SecureServer -keyalg RSA -keystore ServerKeystore

Next, we will examine the contents of the generated Server Keystore, which is accomplished by the following command.
2) keytool -list -v  -keystore ServerKeystore

The next step is to create a self-signed certificate and this is accomplished by executing the following commands.
3) keytool -export -alias SecureServer -keystore ServerKeystore -rfc -file Server.cer



Just to see what the certificate looks like, we'll print to the console with the following:
4) cat Server.cer

import this certificate into a truststore, which then can be used by the client.
5) keytool -import -alias SecureServer -file Server.cer -keystore ClientTruststore

To verify the contents of the truststore that we created, we issue the following command
6) keytool -list -v  -keystore ClientTruststore


    * Setting Keystore - Programmatically:
      System.setProperty("javax.net.ssl.keyStore", "./resources/Server_Keystore"); System.setProperty("javax.net.ssl.keyStorePassword", "password");
    * Setting Keystore - Command Line:
      java -Djavax.net.ssl.keyStore=./resources/Server_Keystore -Djavax.net.ssl.keyStorePassword=password com.article.jn.securermi.CreditCardAuthServer
    * Setting Truststore - Programmatically:
      System.setProperty("javax.net.ssl.trustStore", "./resources/Client_Truststore");
    * Setting Truststore - Command Line:
      java -Djavax.net.ssl.trustStore= ./resources/Client_Truststore

Thursday, August 25, 2011

Queues with Threads to be avoided.

Warning: Contains some ranting. ;)

It baffles me why so many developers use Queue and Thread explicitly when there is an ExecutorService built in which does both much more elegantly. This has been part of Java 5.0 since 2004 and was available as a third party library many years before that.

There are specific cases where using a Queue and Thread is the best choice but that doesn't appear to be the reason it is done most of the time.

Is it a lack of understanding of these core libraries? Is it because that is how it is done in other languages and people are bringing the patterns they know to Java?

I wonder if developers get some satisfaction doing things the hard way. There is a greater sense of achievement when you have written some really complex code?

Any other theories welcome. ;)

Examples of developers doing things the hard way.
  • Using a text editor for development instead of an IDE
  • Using double checked locking lazy Singleton instead of an enum with one instance.
  • Using primitive wrappers when they mean to use primitives.
  • Trying to guess why a program doesn't work as expected instead of using a debugger
  • Trying to guess why your program doesn't perform instead of using a profiler which will measure it.

Wednesday, August 24, 2011

Using ExecutorService to run many background tasks.

Overview

Java has had support for thread pools for a many years, but using them is still black art for many. Part of the problem is that misusing a thread pool will make a program run slower not faster and cause the program to crash or even hang the system.

I often see people trying to use a queue, or wait/notify and thread(s) when an ExecutorService does all this for you.

Choosing the right thread pool

For different situations, different pools are useful. Sometimes it makes sense to have more than one to take advantage of their different characteristics.
Thread pool typeHow to createusesdisadvantages
Single threaded poolExecutors. newSingleThreadExecutor();Useful to event processing e.g. logs always performs tasks in order and minimises overheadOnly one thread
Fixed size poolExecutors. newFixedThreadPool (n_threads);Useful for using many cores, possibly all cores in your systemAlways has the same number of threads even if not used.
Cached thread poolExecutors. newCachedThreadPool();A good alternative to creating new threads for each task by re-cycling threadsThe number of threads is unlimited and incorrect use can bring a system to its knees.
Single threaded scheduling pool Executors. newSingleThreadScheduledExecutor (n_threads);Useful to event processing some with delaysOnly one thread
Multi threaded scheduling poolExecutors. newScheduledThreadPool();Fixed size pool for recurring and delay eventsAlways has the maximum number threads

Performance tips

The cost of a creating task in a pool can be significant and the smaller you make the task, the greater the overhead to real work being done.

Performing a loop across multiple threads

I have added an example program for the use of Executor.

The first example is single threaded. This should be the baseline of any performance test as there is no point using multiple threads if the task will be slower.

The second example shows that even with an optimal number of threads, making the tasks too small will make the task take much longer.

The third example uses as little tasks as possible which will still keep all the cores busy. This is usually optimal for a CPU intensive task.

The examples print
Single threaded: Time to geomean 1,000,000 values was 7.163 msecs. 
Too many tasks: Time to geomean 1,000,000 values was 601.613 msecs. 
One task per thread: Time to geomean 1,000,000 values was 2.349 msecs. 

Code: ExecutorTestMain

Thursday, August 18, 2011

How to Map Dates with Hibernate – Use joda-time

This is always a question – how to map temporal data in our hibernate entities – whether to use java.util.Date, java.util.Calendar or simply long. The correct answer is: neither of these. Use joda-time – the de-facto Java datetime API. Using it throughout the whole project is a no-brainer, but how to use it with hibernate – you can’t use @Temporal. Hibernate supports custom types, so there’s a little project that solves the issue – joda-time – hibernate support.
The user guide is pretty clear:

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime fromDate;
However, there’s one issue with that library – it uses static final fields from hibernate, which requires recompilation if the version of hibernate changes (see here). For that reason you may need to extend the PersistentDateTime class, override the 2 methods that use the static final TIMESTAMP field, and copy the exact same code from the superclass.
Using joda-time throughout the whole project will save tons of headaches. So I strongly suggest the above mechanism to use joda-time in your hibernate entities as well.

More uses for varargs

Overview

Varargs have many uses from simplifying code. However, they are not always used as often as they could be.



Use in reflections

Calling a method via reflections is fairly ugly without varargs.

ClassLoader cl = Thread.currentThread().getContextClassLoader();
Method defineClass = cl.getClass().getDeclaredMethod("defineClass", 
    new Class[] { String.class, byte[].class, int.class, int.class});
defineClass.setAccessible(true);
defineClass.invoke(cl, 
    new Object[] { myNewClassName, myByteCode, 0, myByteCode.length });

With varargs, the code appears cleaner.
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Method defineClass = cl.getClass().getDeclaredMethod(
    "defineClass", String.class, byte[].class, int.class, int.class);
defineClass.setAccessible(true);
defineClass.invoke(cl, myNewClassName, myByteCode, 0, myByteCode.length);

Use of varargs for optional arguments

System.out.printf use varargs which might be optional.
System.out.printf("Hello%n"); // no args to the varargs.
System.out.printf("Hello %s%n", name); // one argument to the varargs.
System.out.printf("Hello %s you are %d years old.%n", name, age); // two arguments

When varargs must have a minimum of one entry

The minimum number of entries for a varargs is 0, so if you need to have at least 1, you can add a parameter.
public static int average(int num, int... nums) {
    long total = num;
    for(int n: nums) total += n;
    return total/(nums.length+1);
}
This method will fail at runtime when no arguments are provided.
int a = average(); // fails to compile, instead of failing at runtime.

Use of varargs to build a List

A commonly used method is Arrays.asList(T...) The first example uses a varargs to build an array, the second example takes and array and passes it one as expected.
List<Integer> ints = Arrays.asList(1,2,3,4,5);
List<String> strings = Arrays.asList("one,two,three,four,five".split(","));

Use of varargs to build a Map

You can also use varargs to help build a map.
public static <K, V> Map<K, V> mapOf(K key, V value, Object... alteratingsKeysAndValues) {
    Map<K, V> map = new LinkedHashMap<K, V>();
    map.put(key, value);
    for(int i = 0; i < alteratingsKeysAndValues.length; i += 2)
        map.put((K) alteratingsKeysAndValues[i], 
                (V) alteratingsKeysAndValues[i+1]);
    return map;
}

Map<String, Integer> smallNumbers = mapOf("zero", 0, "one", 1, "two", 2);

Wednesday, August 10, 2011

What is called before main.

Overview

The starting point for core java applications is the main(String[]) method. But is there any code which is called before this method and do we even need it?

The class has to be initialised

Before calling main, the static block for the class is called to initialise the class.

public class Main {
    static {
        System.out.println("Called first.");
    }
    public static void main(String... args) {
        System.out.println("Hello world.");
    }
}
prints
Called first.
Hello world.

Can we avoid having a main()

Normally, if you don't have a main() method, you will get an error. However if your program exits before calling main() no error is produced.
public class Main {
    static {
        System.out.println("Hello world.");
        System.exit(0);
    }
}
prints
Hello world.

The premain method

If you have Java agents, those agents can have a premain method which is called first. Instrument package

public static void premain(String agentArgs, Instrumentation inst);
The Instrumentation class gives the agent access to each class' byte code after it is read and before it is linked, giving the agent the option to change byte code.

One interesting feature of the Instrumentation class is the getObjectSize() which will give you the size of an object.

Tuesday, August 9, 2011

Top 15+ Best Practices for Writing Super Readable Code!

1 - Commenting & Documentation


IDE’s (Integrated Development Environment) have come a long way in the past few years. This made commenting your code more useful than ever. Following certain standards in your comments allows IDE’s and other tools to utilize them in different ways.
Consider this example:
The comments I added at the function definition can be previewed whenever I use that function, even from other files.
Here is another example where I call a function from a third party library:
In these particular examples, the type of commenting (or documentation) used is based on PHPDoc, and the IDE is Aptana.


2 - Consistent Indentation

I assume you already know that you should indent your code. However, it’s also worth noting that it is a good idea to keep your indentation style consistent.
There are more than one way of indenting code.
Style 1:
function foo() {
	if ($maybe) {
		do_it_now();
		again();
	} else {
		abort_mission();
	}
	finalize();
}
Style 2:

function foo()
{
	if ($maybe)
	{
		do_it_now();
		again();
	}
	else
	{
		abort_mission();
	}
	finalize();
}
Style 3:
function foo()
{	if ($maybe)
	{	do_it_now();
		again();
	}
	else
	{	abort_mission();
	}
	finalize();
}
I used to code in style #2 but recently switched to #1. But that is only a matter of preference. There is no “best” style that everyone should be following. Actually, the best style, is a consistent style. If you are part of a team or if you are contributing code to a project, you should follow the existing style that is being used in that project.
The indentation styles are not always completely distinct from one another. Sometimes, they mix different rules. For example, in PEAR Coding Standards, the opening bracket "{" goes on the same line as control structures, but they go to the next line after function definitions.

PEAR Style:
function foo()
{                     // placed on the next line
    if ($maybe) {     // placed on the same line
        do_it_now();
        again();
    } else {
        abort_mission();
    }
    finalize();
}
Also note that they are using four spaces instead of tabs for indentations.
Here is a Wikipedia article with samples of different indent styles.

3 - Avoid Obvious Comments

Commenting your code is fantastic; however, it can be overdone or just be plain redundant. Take this example:

// get the country code
$country_code = get_country_code($_SERVER['REMOTE_ADDR']);

// if country code is US
if ($country_code == 'US') {

	// display the form input for state
	echo form_input_state();
}
When the text is that obvious, it’s really not productive to repeat it within comments.
If you must comment on that code, you can simply combine it to a single line instead:
// display state selection for US users
$country_code = get_country_code($_SERVER['REMOTE_ADDR']);
if ($country_code == 'US') {
	echo form_input_state();
}

4 - Code Grouping

More often than not, certain tasks require a few lines of code. It is a good idea to keep these tasks within separate blocks of code, with some spaces between them.

Here is a simplified example:
// get list of forums
$forums = array();
$r = mysql_query("SELECT id, name, description FROM forums");
while ($d = mysql_fetch_assoc($r)) {
	$forums []= $d;
}

// load the templates
load_template('header');
load_template('forum_list',$forums);
load_template('footer');
Adding a comment at the beginning of each block of code also emphasizes the visual separation.

5 - Consistent Naming Scheme

PHP itself is sometimes guilty of not following consistent naming schemes:
  • strpos() vs. str_split()
  • imagetypes() vs. image_type_to_extension()
First of all, the names should have word boundaries. There are two popular options:
  • camelCase: First letter of each word is capitalized, except the first word.
  • underscores: Underscores between words, like: mysql_real_escape_string().
Having different options creates a situation similar to the indent styles, as I mentioned earlier. If an existing project follows a certain convention, you should go with that. Also, some language platforms tend to use a certain naming scheme. For instance, in Java, most code uses camelCase names, while in PHP, the majority of uses underscores.

These can also be mixed. Some developers prefer to use underscores for procedural functions, and class names, but use camelCase for class method names:
class Foo_Bar {

	public function someDummyMethod() {

	}

}

function procedural_function_name() {

}
So again, there is no obvious “best” style. Just being consistent.

6 - DRY Principle

DRY stands for Don’t Repeat Yourself. Also known as DIE: Duplication is Evil.

The principle states:
“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”
The purpose for most applications (or computers in general) is to automate repetitive tasks. This principle should be maintained in all code, even web applications. The same piece of code should not be repeated over and over again.
For example, most web applications consist of many pages. It’s highly likely that these pages will contain common elements. Headers and footers are usually best candidates for this. It’s not a good idea to keep copy pasting these headers and footers into every page. Here is Jeffrey Way explaining how to create templates in CodeIgniter.
$this->load->view('includes/header');

$this->load->view($main_content);

$this->load->view('includes/footer');

7 - Avoid Deep Nesting

Too many levels of nesting can make code harder to read and follow.
function do_stuff() {

// ...

	if (is_writable($folder)) {

		if ($fp = fopen($file_path,'w')) {

			if ($stuff = get_some_stuff()) {

				if (fwrite($fp,$stuff)) {

					// ...

				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}
For the sake of readability, it is usually possible to make changes to your code to reduce the level of nesting:
function do_stuff() {

// ...

	if (!is_writable($folder)) {
		return false;
	}

	if (!$fp = fopen($file_path,'w')) {
		return false;
	}

	if (!$stuff = get_some_stuff()) {
		return false;
	}

	if (fwrite($fp,$stuff)) {
		// ...
	} else {
		return false;
	}
}


8 - Limit Line Length

Our eyes are more comfortable when reading tall and narrow columns of text. This is precisely the reason why newspaper articles look like this:
It is a good practice to avoid writing horizontally long lines of code.
// bad
$my_email->set_from('test@email.com')->add_to('programming@gmail.com')->set_subject('Methods Chained')->set_body('Some long message')->send();

// good
$my_email
	->set_from('test@email.com')
	->add_to('programming@gmail.com')
	->set_subject('Methods Chained')
	->set_body('Some long message')
	->send();

// bad
$query = "SELECT id, username, first_name, last_name, status FROM users LEFT JOIN user_posts USING(users.id, user_posts.user_id) WHERE post_id = '123'";

// good
$query = "SELECT id, username, first_name, last_name, status
	FROM users
	LEFT JOIN user_posts USING(users.id, user_posts.user_id)
	WHERE post_id = '123'";
Also, if anyone intends to read the code from a terminal window, such as Vim users, it is a good idea to to limit the line length to around 80 characters.


9 - File and Folder Organization

Technically, you could write an entire application code within a single file. But that would prove to be a nightmare to read and maintain.
During my first programming projects, I knew about the idea of creating “include files.” However, I was not yet even remotely organized. I created an “inc” folder, with two files in it: db.php and functions.php. As the applications grew, the functions file also became huge and unmaintainable.

One of the best approaches is to either use a framework, or imitate their folder structure. Here is what CodeIgniter looks like:

10 - Consistent Temporary Names

Normally, the variables should be descriptive and contain one or more words. But, this doesn’t necessarily apply to temporary variables. They can be as short as a single character.
It is a good practice to use consistent names for your temporary variables that have the same kind of role. Here are a few examples that I tend use in my code:
// $i for loop counters
for ($i = 0; $i < 100; $i++) {

	// $j for the nested loop counters
	for ($j = 0; $j < 100; $j++) {

	}
}

// $ret for return variables
function foo() {
	$ret['bar'] = get_bar();
	$ret['stuff'] = get_stuff();

	return $ret;
}

// $k and $v in foreach
foreach ($some_array as $k => $v) {

}

// $q, $r and $d for mysql
$q = "SELECT * FROM table";
$r = mysql_query($q);
while ($d = mysql_fetch_assocr($r)) {

}

// $fp for file pointers
$fp = fopen('file.txt','w');

11 - Capitalize SQL Special Words

Database interaction is a big part of most web applications. If you are writing raw SQL queries, it is a good idea to keep them readable as well.
Even though SQL special words and function names are case insensitive, it is common practice to capitalize them to distinguish them from your table and column names.
SELECT id, username FROM user;

UPDATE user SET last_login = NOW()
WHERE id = '123'

SELECT id, username FROM user u
LEFT JOIN user_address ua ON(u.id = ua.user_id)
WHERE ua.state = 'NY'
GROUP BY u.id
ORDER BY u.username
LIMIT 0,20


12 - Separation of Code and Data

This is another principle that applies to almost all programming languages in all environments. In the case of web development, the “data” usually implies HTML output.
When PHP was first released many years ago, it was primarily seen as a template engine. It was common to have big HTML files with a few lines of PHP code in between. However, things have changed over the years and websites became more and more dynamic and functional. The code is now a huge part of web applications, and it is no longer a good practice to combine it with the HTML.
You can either apply the principle to your application by yourself, or you can use a third party tool (template engines, frameworks or CMS’s) and follow their conventions.
Popular PHP Frameworks:
Popular Template Engines:
Popular Content Management Systems

13 - Alternate Syntax Inside Templates

You may choose not to use a fancy template engine, and instead go with plain inline PHP in your template files. This does not necessarily violate the “Separation of Code and Data,” as long as the inline code is directly related to the output, and is readable. In this case you should consider using the alternate syntax for control structures.
Here is an example:

<div class="user_controls">
	<?php if ($user = Current_User::user()): ?>
		Hello, <em><?php echo $user->username; ?></em> <br/>
		<?php echo anchor('logout', 'Logout'); ?>
	<?php else: ?>
		<?php echo anchor('login','Login'); ?> |
		<?php echo anchor('signup', 'Register'); ?>
	<?php endif; ?>
</div>

<h1>My Message Board</h1>

<?php foreach($categories as $category): ?>

	<div class="category">

		<h2><?php echo $category->title; ?></h2>

		<?php foreach($category->Forums as $forum): ?>

			<div class="forum">

				<h3>
					<?php echo anchor('forums/'.$forum->id, $forum->title) ?>
					(<?php echo $forum->Threads->count(); ?> threads)
				</h3>

				<div class="description">
					<?php echo $forum->description; ?>
				</div>

			</div>

		<?php endforeach; ?>

	</div>

<?php endforeach; ?>
This lets you avoid lots of curly braces. Also, the code looks and feels similar to the way HTML is structured and indented.

14 - Object Oriented vs. Procedural

Object oriented programming can help you create well structured code. But that does not mean you need to abandon procedural programming completely. Actually creating a mix of both styles can be good.
Objects should be used for representing data, usually residing in a database.
class User {

	public $username;
	public $first_name;
	public $last_name;
	public $email;

	public function __construct() {
		// ...
	}

	public function create() {
		// ...
	}

	public function save() {
		// ...
	}

	public function delete() {
		// ...
	}

}
Procedural functions may be used for specific tasks that can be performed independently.

function capitalize($string) {

	$ret = strtoupper($string[0]);
	$ret .= strtolower(substr($string,1));
	return $ret;

}

Object-Oriented Programming in PHP

Take your skills to the next level with this Premium video course.

15 - Read Open Source Code


Open Source projects are built with the input of many developers. These projects need to maintain a high level of code readability so that the team can work together as efficiently as possible. Therefore, it is a good idea to browse through the source code of these projects to observe what these developers are doing.

16 - Code Refactoring

When you “refactor,” you make changes to the code without changing any of its functionality. You can think of it like a “clean up,” for the sake of improving readability and quality.
This doesn’t include bug fixes or the addition of any new functionality. You might refactor code that you have written the day before, while it’s still fresh in your head, so that it is more readable and reusable when you may potentially look at it two months from now. As the motto says: “refactor early, refactor often.”

You may apply any of the “best practices” of code readability during the refactoring process.
I hope you enjoyed this article! Any that I missed? Let me know via the comments.

Comparing Collections speeds..!

The speed of different collections tends to depend on their usage pattern however it can be useful to have an idea of their relative performance.

The test:

In this test I compare both single thread and thread safe collections of different sizes.

Adding is from lowest to highest values. Removal is from start and end,
finishing with the middle value. This disadvantages ArrayList and
LinkedList relatively equally.


CollectionSizeAddingIteratingRemoving
ArrayList100,00030.114.567,359
ArrayList10,0008.311.26,323
ArrayList1,0006.49.4585
ArrayList1006.89.592.9
ArrayList109.713.236.6
LinkedList100,00046.441.4139,326
LinkedList10,0009.330.313,413
LinkedList1,0008.727.61,025
LinkedList1007.819.8110
LinkedList1011.230.338.7
HashSet100,00028.611569.5
HashSet10,00025.732458.6
HashSet1,00026.02,52159.2
HashSet10030.024,49064.6
HashSet1051.7244,045131
LinkedHashSet100,00035.910369.2
LinkedHashSet10,00030.310165.0
LinkedHashSet1,00029.210065.8
LinkedHashSet10031.598.860.6
LinkedHashSet1045.411069.8
TreeSet100,000159106187
TreeSet10,00099.677.6111
TreeSet1,00078.557.593.7
TreeSet10054.047.983.7
TreeSet1030.251.049.4
newSetFromMap IdentityHashMap100,00074.1216153
newSetFromMap IdentityHashMap10,00065.5370126
newSetFromMap IdentityHashMap1,00060.41,644115
newSetFromMap IdentityHashMap10021.114,21648.9
newSetFromMap IdentityHashMap1046.7140,162109
newSetFromMap WeakHashMap100,00052.414887.4
newSetFromMap WeakHashMap10,00033.521771.2
newSetFromMap WeakHashMap1,00032.91,08872.2
newSetFromMap WeakHashMap10032.19,71764.3
newSetFromMap WeakHashMap1048.098,76993.0

Thread Safe Collections
synchronized ArrayList100,00022.610198,735
synchronized ArrayList10,00012.110110,084
synchronized ArrayList1,00011.599.51,023
synchronized ArrayList10011.4101144
synchronized ArrayList1014.011154.0
Vector100,00023.929864,333
Vector10,00020.43725,919
Vector1,00019.4372612
Vector10019.3375145
Vector1022.939797.5
synchronized LinkedList100,00037.6100132,660
synchronized LinkedList10,00019.299.613,049
synchronized LinkedList1,00022.299.11,005
synchronized LinkedList10023.7102134
synchronized LinkedList1026.111562.6
synchronized HashSet100,00050.113895.7
synchronized HashSet10,00043.034793.1
synchronized HashSet1,00042.72,55595.3
synchronized HashSet10047.024,60897.4
synchronized HashSet1085.7245,343204
synchronized LinkedHashSet100,00048.610393.1
synchronized LinkedHashSet10,00045.610188.1
synchronized LinkedHashSet1,00048.710694.6
synchronized LinkedHashSet10048.110186.8
synchronized LinkedHashSet1057.811493.4
synchronized TreeSet100,000116105179
synchronized TreeSet10,00074.175.4122
synchronized TreeSet1,00063.553.9103
synchronized TreeSet10050.846.290.4
synchronized TreeSet1029.149.157.6
CopyOnWriteArrayList100,00035,35465.8165,197
CopyOnWriteArrayList10,0002,11597.736,269
CopyOnWriteArrayList1,00021775.11,828
CopyOnWriteArrayList10060.063.4227
CopyOnWriteArrayList1048.271.0103
CopyOnWriteArraySet100,000106,11663.3165,708
CopyOnWriteArraySet10,00028,73691.229,820
CopyOnWriteArraySet1,0001,14375.11,852
CopyOnWriteArraySet10013463.4233
CopyOnWriteArraySet1055.373.0109
newSetFromMap ConcurrentHashMap100,00070.6291149
newSetFromMap ConcurrentHashMap10,00057.2485120
newSetFromMap ConcurrentHashMap1,00053.42,869121
newSetFromMap ConcurrentHashMap10054.724,150140
newSetFromMap ConcurrentHashMap1068.793,921197
newSetFromMap ConcurrentSkipListMap100,00013596.8365
newSetFromMap ConcurrentSkipListMap10,00011395.1285
newSetFromMap ConcurrentSkipListMap1,00097.894.7234
newSetFromMap ConcurrentSkipListMap10083.395.2192
newSetFromMap ConcurrentSkipListMap1064.2112151

THe code that performs the above given test:

/*
 * MailTodoNew - AddIterateRemoveTest.java, Aug 9, 2011 5:23:37 PM
 *
 * Copyright 2011 MindTree Ltd, Inc. All rights reserved.
 * MindTree proprietary/confidential. Use is subject to license terms.
 */
package com.mt.util.testing;

import org.junit.Test;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * The Class AddIterateRemoveTest.
 *
 * @author Karthikeyan V. Reddy
 * @version 1.0
 */
public class AddIterateRemoveTest
{
   
    /** The Constant RUNS_TIME_MS. */
    static final int RUNS_TIME_MS = 10 * 1000;
   
    /** The Constant LARGEST_SIZE. */
    static final int LARGEST_SIZE = 100 * 1000;
   
    /** The Constant INTS. */
    static final int[] INTS = new int[LARGEST_SIZE];
   
    static
    {
        for (int i = 0; i < LARGEST_SIZE; i++)
            INTS[i] = i;
    }
   
    /**
     * Performance test.
     */
    @Test
    public void performanceTest()
    {
        test(new TreeSet<Integer>());
        test(Collections.synchronizedSortedSet(new TreeSet<Integer>()), "synchronized TreeSet");
        test(new ArrayList<Integer>());
        test(new LinkedList<Integer>());
        test(new HashSet<Integer>());
        test(new LinkedHashSet<Integer>());
        test(Collections.newSetFromMap(new IdentityHashMap<Integer, Boolean>()), "newSetFromMap IdentityHashMap");
        test(Collections.newSetFromMap(new WeakHashMap<Integer, Boolean>()), "newSetFromMap WeakHashMap");
       
        test(Collections.synchronizedList(new ArrayList<Integer>()), "synchronized ArrayList");
        test(new Vector<Integer>());
        test(Collections.synchronizedList(new LinkedList<Integer>()), "synchronized LinkedList");
        test(Collections.synchronizedSet(new HashSet<Integer>()), "synchronized HashSet");
        test(Collections.synchronizedSet(new LinkedHashSet<Integer>()), "synchronized LinkedHashSet");
        test(new CopyOnWriteArrayList<Integer>());
        test(new CopyOnWriteArraySet<Integer>());
        test(Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>()), "newSetFromMap ConcurrentHashMap");
        test(Collections.newSetFromMap(new ConcurrentSkipListMap<Integer, Boolean>()),
                "newSetFromMap ConcurrentSkipListMap");
    }
   
    /**
     * Test.
     *
     * @param ints
     *            the ints
     */
    private void test(Collection<Integer> ints)
    {
        test(ints, ints.getClass().getSimpleName());
    }
   
    /**
     * Test.
     *
     * @param ints
     *            the ints
     * @param collectionName
     *            the collection name
     */
    private void test(Collection<Integer> ints, String collectionName)
    {
        for (int size = LARGEST_SIZE; size >= 10; size /= 10)
        {
            long adding = 0;
            long removing = 0;
            long iterating = 0;
           
            int runs = 0;
            long endTime = System.currentTimeMillis() + RUNS_TIME_MS;
            do
            {
                runs++;
                long start = System.nanoTime();
                testAdding(ints, size);
               
                adding += System.nanoTime() - start;
               
                start = System.nanoTime();
                for (int repeat = 0; repeat < 100; repeat++)
                    testIterating(ints);
                iterating += System.nanoTime() - start;
               
                start = System.nanoTime();
                testRemoving(ints, size);
                removing += (System.nanoTime() - start) * 2;
               
                ints.clear();
            } while (endTime > System.currentTimeMillis());
            System.out.println("<tr><td>" + collectionName + "</td><td aligned=\"right\">" + String.format("%,d", size)
                    + "</td><td aligned=\"right\">" + format(10 * adding / runs / size) + "</td><td aligned=\"right\">"
                    + format(iterating / runs / size) + "</td><td aligned=\"right\">"
                    + format(10 * removing / runs / size) + "</td></tr>");
        }
    }
   
    /**
     * Format.
     *
     * @param l
     *            the l
     * @return the string
     */
    private String format(long l)
    {
        return l < 1000 ? "" + (l / 10.0) : l < 10000 ? "" + l / 10 : String.format("%,d", l / 10);
    }
   
    /**
     * Test adding.
     *
     * @param ints
     *            the ints
     * @param size
     *            the size
     */
    private static void testAdding(Collection<Integer> ints, int size)
    {
        // adding
        for (int i = 0; i < size; i++)
            ints.add(INTS[i]);
    }
   
    /**
     * Test iterating.
     *
     * @param ints
     *            the ints
     * @return the long
     */
    private static long testIterating(Collection<Integer> ints)
    {
        // iterating
        long sum = 0;
        for (Integer i : ints)
            sum += i;
        return sum;
    }
   
    /**
     * Test removing.
     *
     * @param ints
     *            the ints
     * @param size
     *            the size
     */
    private void testRemoving(Collection<Integer> ints, int size)
    {
        // forward and reverse
        for (int i = 0; i < size / 2; i++)
        {
            ints.remove(INTS[i]);
            ints.remove(INTS[size - i - 1]);
        }
    }
}


Thursday, August 4, 2011

Being as fast as possible is a bad idea

Overview



When you design a system which must perform, a common assumption is that you need to it to be as fast as possible. However this needs to be qualified and not having a clear idea of how much performance you need can mean you spend more money, waste time or impact your design more than needed.

The first question is knowing whether it is latency, throughput or both which are required. Often only one really matters. A low latency usually gives you a high throughput. If only throughput is required using parallelism is often a cost effective solution.

Should you only design a system to be just what you need?

It is a brave move to only design a system to do exactly what you need and no more. This is because systems often behave as well as they should on paper.

The other reason is that systems tend to vary in their performance due to the complexity of their systems. End users tend to remember the worse performance they ever got making the occasion slow performance more significant than you might give credit.

My rule of thumb is to say; if your design is ten times the realistic performance required, you are likely to be safe and don't aim higher unless you are sure it won't cost you anything.

Without clarity of the performance required it is easy to be out by more than a factor of ten, which is usually a disaster (if too low) or costly (if too high) (See below)

You shouldn't feel that whatever you produce is set in stone. If a solution is particularly successful, you will find you can get more resources to improve to solution. If you spend too much resources building a solution which will never be needed you can't get that time/resource back.

How fast is fast enough?

When people say something must be as fast as possible, in my experience this can mean just about anything. For some people this means 100 ms, 10 ms, 1 ms, 100 μs, 10 μs, 1 μs is acceptible. Additionally you need to determine what is to be measured and how to measure it, but you have to start with a ball park figure.

Just this week I have had several conversations with people who said they need a top candidate with strong high frequency trading experience (sounds great). So I ask them what are they trying to achieve and they have to admit they are not trying anything like what I have been doing for the last couple of years, by more than ten times. (sounds disappointing)

For many systems, a low latency gives a high throughput. The relationship is usually inverse. i.e. a system with a latency of 1 ms can handle at least 1 K/s and 10 μs can handle 100 K/s. The reason this matters is, if the latency is low enough the throughput can be fine as well. In this case you only have to worry about latency.

Note: 1 μs is one micro-second or a millionth of a second.

What throughput do you need?

Often having a low latency system alone is not enough and perhaps not required. Many libraries, frameworks and systems were not designed with low latency in mind and re-engineering them is expensive.

Systems with high throughput are often highly parallel performing many unrelated tasks at once. Most systems have a throughput of 100/s, 1K/s, 10K/s, 100K/s, 1M/s or 10M/s.

When you can get it really wrong


When an application takes far too long

A large project I worked on was a GIS application in Australia. It had every telecom wire, every property boundary, every fence, street etc in Australia. Australia has a relatively sparse population but it has a large area (alot of it not containing any recorded features)

For this program we had to migrate data from the old system to the new system. The time taken to migrate the data was proprortional to the area described. (Did I mention Australia was big?) and unfortunately this hadn't been recognised as a problem until the project had spent $70m. A migration of data which had to take 24 hours I estimated to take 100 years. That is out by many orders of magintude. Even after explaining the problem to the vendor, they didn't see the need to redesign the software. A year later, they made it four times faster. (Funding for the project was stopped at that point as 25 years was still too long)

When a project is far too fast

One project I did design, there was a "requirement" to support 8,000 concurrent users. At the time I was highly sceptical, however I was keen to build such a systemm as a technical challenge and it didn't bother me then it might never be needed. However getting to the end of the project, I realised we could have delivered the solution perhaps 6 months earlier if we had a more realistic number of users which was closer to 800 users. As it turned out, we got more hardware than I had assumed we would have as it was a successful project and the systems never used more than 2% of cpu.

The unrealtistic requirement delayed the project and we could have been in the market making money earlier.

Human factors

You have to realistic when you have a human input or display. A human cannot respond faster than about 1/20 of a second or about 50 ms. There is no point updating a screen too often. A delay of less than 10 ms is unlikely to make much difference to a user.

People tend to remember the worse service/performance they got. When measuring a system, you want to pay attension to the high percentile/worst times and try to minimise these as well as looking at the averages.