Archive

Archive for the ‘HOW TO’ Category

Use a classpath resource or kill your application’s portability

July 10th, 2009 Mark Turansky 3 comments

Here is the secret way to kill your application’s portability — and by portable, I mean across different computers, let alone operating systems:  Hardcode all your paths.

That’s it.  That very quickly kills portability.  It’s easy to accomplish, too.  Simply refer to all your configuration files, for example, by fully qualified pathname, like this:

System.setProperty("com.yourcorp.refdata.config.filename",
    "C:\\Documents and Settings\\FOO\\Perforce_FOO\\PATHS_CHANGED_FOR_ANONYMITY\\RefDataConfig.xml");

The above snippet is something I’m battling with to get unit tests working in my project. Naturally it doesn’t work for me because “FOO” isn’t my username nor is my Perforce sandbox “Performance_FOO” because, again, “FOO” isn’t my username.

This unit test won’t work across machines using the same OS, and our brethren using Macs or Linux boxes are completely hosed.

Don’t hardcode any paths in your application!

In Java, use a classpath resource.   This gives you portability.  It also allows a Configuration Management team the ability to package all required resources into a single artifact for better version control.

The safest way to get a classpath resource would be to use your current classloader to find the resource.

// Well-behaved Java programs set the thread's current classloader when running in a
// multi-classloader environment.  You see this when you write containers of any type.
Thread.currentThread().getContextClassLoader().getResource("/some/path/RefDataConfig.xml");

// or another way... sufficient for most cases
this.getClass().getClassLoader().getResource("/some/path/RefDataConfig.xml");
Categories: Code Hints, HOW TO Tags:

HOWTO: Sort a Python Dictionary/Map

May 11th, 2009 Mark Turansky No comments

I use Python all the time for quick little scripting tasks.  There’s nothing better to slice and dice a file, so I use Python for a lot of reporting tasks.  That usually involves building some kind of data structure in my script that I’m slicing and dicing from files.

In my work, I have a LOT of units of work processing in parallel on a grid.  I have GUIDs tagging each unit of work, and that GUID is the perfect key for a Map/Dictionary data structure.  There are times, though, that I want to get the values of the Map and sort by some value in the data itself.  The is important if I want to sort my results by elapsed time or some other interesting metric.

Here’s how you sort a Python Dictionary by some arbitrary value within the data structure:

import time

work = {}

#
# create some sample data...
#
for i in range(10):
    key = "unit_%s" % i
    unitOfWork = {
        "id" : key,
        "data" : {
            "name" : "Turansky",
            "dob" : "03/28",
            "favoriteNumber" : int(time.time()) + i
        }
    }
    work[key] = unitOfWork

print "The 'work' dictionary will print the objects randomly..."
for i in work:
    print work[i]

print ""
print "Sprinkle some sorting magic..."

# but you want to sort the objects by favoriteNumber'
# get your values as a list... you want to use the list.sort() method
units = work.values()

# provide a lambda function that references your data structure
units.sort(key = lambda obj:obj["data"]["favoriteNumber"])

print ""
print "... and just like that, you have order."
for u in units:
    print u

Here is the output:

The 'work' dictionary will print the objects randomly...
{'data': {'dob': '03/28', 'favoriteNumber': 1242069926, 'name': 'Turansky'}, 'id': 'unit_5'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069925, 'name': 'Turansky'}, 'id': 'unit_4'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069928, 'name': 'Turansky'}, 'id': 'unit_7'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069927, 'name': 'Turansky'}, 'id': 'unit_6'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069922, 'name': 'Turansky'}, 'id': 'unit_1'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069921, 'name': 'Turansky'}, 'id': 'unit_0'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069924, 'name': 'Turansky'}, 'id': 'unit_3'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069923, 'name': 'Turansky'}, 'id': 'unit_2'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069930, 'name': 'Turansky'}, 'id': 'unit_9'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069929, 'name': 'Turansky'}, 'id': 'unit_8'}

Sprinkle some sorting magic...

... and just like that, you have order.
{'data': {'dob': '03/28', 'favoriteNumber': 1242069921, 'name': 'Turansky'}, 'id': 'unit_0'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069922, 'name': 'Turansky'}, 'id': 'unit_1'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069923, 'name': 'Turansky'}, 'id': 'unit_2'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069924, 'name': 'Turansky'}, 'id': 'unit_3'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069925, 'name': 'Turansky'}, 'id': 'unit_4'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069926, 'name': 'Turansky'}, 'id': 'unit_5'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069927, 'name': 'Turansky'}, 'id': 'unit_6'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069928, 'name': 'Turansky'}, 'id': 'unit_7'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069929, 'name': 'Turansky'}, 'id': 'unit_8'}
{'data': {'dob': '03/28', 'favoriteNumber': 1242069930, 'name': 'Turansky'}, 'id': 'unit_9'}
Categories: HOW TO, Python Tags:

Be mindful of Collection.contains(obj)

May 8th, 2009 Mark Turansky 2 comments

Summary

All Collection.contains(obj) methods are not the same!

This article is a real world case study of the Big O differences between various implementations of Java’s Collection interface.   I found and fixed a grievous O(n^2) algorithm by using the right data structure.

Background

I was asked to investigate why some pages in our web application would save session data very quickly while another problem page would take literally tens of minutes. The application had at its core a Stateful Session Bean that held dirty objects which would be persisted to the database in a single transaction. Sure, the easy pages didn’t contain very much data to persist and we knew the problem page contains many times more data, but certainly not that much more data to cause 20 minute request times!

After I implemented the fix, the page elapsed time dropped from 20+ minutes to ~10 seconds. What did I do? I used the right data structure.

Data Structures and the Big O

The application used a Vector to store dirty objects. A Vector was used for two reasons: 1) the original engineers thought synchronization was important and 2) order was important for referential integrity. A Vector’s internal synchronization was unneeded because only a single user’s request thread ever access the application. The ordering, however, was extremely important because you couldn’t add a person’s data without first adding the person!

The problem page in the web app had to add thousands of rows of data to the database, hence there were thousands of dirty objects waiting in the cache for persistence. As the application created or dirtied objects, it checked its cache (the Vector) before adding it. You wouldn’t want the data to be persisted twice.

How did the app check its cache? vector.contains(obj);

The problem with vector.contains(obj) and list.contains(obj) is that they are O(n), which means they scale linearly. Put another way, it gets slower the more items you put into it. The page that created thousands of objects to persist got progressively slower with each object it created.

The solution was to switching to a LinkedHashSet which perserves order for referential integrity while providing O(1) performance for set.contains(obj) because all the objects are hashed.

The real problem was even worse, of course, because the app checked the cache each time before it added a new object.  This represents a good ol’ fashioned O(n^2) algorithm.

To be fair to the original developers, they wrote the application in Java 1.3 and LinkedHashSet was implemented in 1.4. Also, I don’t think they anticipated having a single page in the application generate thousands of objects.

Sample Code

Below is a simple program to highlight the performance differences between various Collection.contains(obj) methods

Elapsed times (in ms):

Vector: 3663
List: 3690
Set: 15
LinkedSet: 12

package mgt.perf;

import java.util.*;

public class ContainsExample {

    private int collectionCount = 10000;
    private int testCount = 50000;

    public static void main(String[] args) {
        new ContainsExample().start();
    }

    private void start() {

        Collection vector = new Vector();
        Collection list = new ArrayList();
        Collection set = new HashSet();
        Collection linkedSet = new LinkedHashSet();

        populate(vector);
        populate(list);
        populate(set);
        populate(linkedSet);

        System.out.println("Elapsed times\n");
        System.out.println("    Vector:" + test(vector));
        System.out.println("      List:" + test(list));
        System.out.println("       Set:" + test(set));
        System.out.println(" LinkedSet:" + test(linkedSet));
    }

    private void populate(Collection set) {
        for (int i = 0; i < collectionCount; i++) {
            set.add(i);
        }
    }

    private long test(Collection collection) {
        Random rnd = new Random(System.currentTimeMillis());
        long started = System.currentTimeMillis();
        for (int i = 0; i < testCount; i++) {
            int lookFor = rnd.nextInt(collectionCount);
            if (!collection.contains(lookFor)) {
                throw new IllegalStateException(lookFor + " really should be in the collection");
            }
        }
        long elapsed = System.currentTimeMillis() - started;
        return elapsed;
    }

}
Categories: Code Hints, Engineering, HOW TO, Performance Tags:

HOW TO: Enable debug and JMX ports in your java app

September 15th, 2008 Mark Turansky No comments

Ever have a stuck or deadlocked thread in a production application? Use JMX to inspect what’s going on inside your JVM, which includes thread views.  It’ll show you which threads are running, waiting, or blocked and where in the stacktrace they currently are.  I’ve used this information to find blocked threads in strange places.  JMX also shows you the memory usage of your java process, including memory consumed by classloaders in permspace.

The debug options will open your debug ports, naturally, and let you connect your debugger.

All you have to do is run your java process with these startup options:
DEBUG
-Xdebug -Xrunjdwp:transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=n
JMX
-Dcom.sun.management.jmxremote.port=$JMX_PORT -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
Look in your $JAVA_HOME/bin and you’ll see a jconsole executable. That GUI will let you connect to the machine running your java process on the port specified.

I hope you find these tips useful.  Both have been extremely useful to me (as well as adding optional profiling vars to a JVM!).

Categories: Engineering, HOW TO Tags:

Design Patterns Quick Reference Cards

June 16th, 2008 Mark Turansky No comments

Jason McDonald’s printable design pattern reference cards were printed by DZone as part of their “RefCardz” iniative.

You can find Jason’s cards here and you can find other useful design patterns stuff here.

Categories: HOW TO, Misc. Tags:

HOW TO: Use JDBC Batching for 7-8X throughput gains

June 3rd, 2008 Mark Turansky 2 comments

Using the batched statement capability of your JDBC driver can give you 7-8X throughput gains. Not only is batching significantly faster, it’ll save database CPU cycles and be easier on the network, too.

The graph below shows elapsed time (in milliseconds) by batch size. For each data point, 1K rows were inserted into a simple table in MySQL. The benchmarking code I used can be found here.

jdbc_batching_gains.png

Why is batching so much faster?

First, depending on how much PreparedStatement caching your driver is doing, your database may be spending a lot of time parsing and compiling statements. After the statement is parsed and compiled, bind variables are applied. In our example, the data base will parse and compile the statement once as opposed to 1,000 times. This reduces the work your database performs and saves CPU.

Second, all bind variables are passed to the database in a single network call instead of 1,000 separate out-of-process, across-the-network calls. This helps reduce network traffic.

Third, depending on the internal architecture of your code, single statements may return the connection to a pool after every use. Multiply that by 1,000 and run a profiler and you’ll see yourself calling take/put methods a lot. Many pools also verify the connection on check-in and check-out. “select 1 from dual” is a common check for a pool to use. Your 1,000 uses of a connection may also be incurring the cost of 2,000 “select 1 from dual” statements!

When should you use batching?

Batching is particularly useful in importing scenarios where you need to get lots of data into your application quickly, but it can be used even when executing a few similar statements. Check out the example source code provided to see if batching is right for you. Fiddle with the numbers to see the gains for batching just 10 similar statements. It may not be 8X big, but trumpeting 25% gains to management is still a win for you and your team.

Use JDBC Batching!

JDBC batching can give you dramatic throughput gains while simultaneously being less abusive to your hardware. Overall, if you have the opportunity to use batch inserts and updates, you should seize that opportunity. Look at your application’s internal architecture to see if batching is right for you.

Categories: Code Hints, Engineering, HOW TO Tags:

“Don’t Make Me Think” applies to your code, too

March 27th, 2008 Mark Turansky 4 comments

Don’t make me think. That’s how I feel about your code.

Or as Martin Fowler puts it:

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” -Martin Fowler, Refactoring: Improving the Design of Existing Code

You’ve reached a whole new level of mastery when you write for simplicity, elegance, and maintainability. This is done on purpose, and it’s hard to get right. Deadlines, schedules, pressure, and stress all encourage us to cut corners and adopt a “Git ‘er done!” mentality. But Abandonment of planning under pressure is one of software’s classic mistakes. It’s a cardinal sin.

How do you write simple and maintainable code? I’ve got a 3-step program for you:

Step 1: Admit that simple isn’t easy

Designing simple software is hard. It has to be done on purpose. You can’t accidentally find yourself with well-written code and an elegant solution, it has to be written that way on purpose.

This admission is a bedrock principle required for designing great software and products. If you can’t admit that simple is Hard Work™, you haven’t hit rock bottom yet by having to maintain code that would make readers of The Daily WTF blush.

Step 2: Read “Don’t Make Me Think”

Steve Krug’s excellent book “Don’t Make Me Think” is about website usability, yet it changed how I look at my code.

Why? Because Steve applied the same principles in his book to his book! And if it works in those two mediums, I thought it just might work for me, too, in my medium (code).

“Don’t Make Me Think” is very easily absorbed because he’s feeding you information in a readily accessible way. He wrote it simply on purpose, and I’m certain it took many more hours to edit than it did to write. Simple is hard.

Step 3: Practice simple everyday

There are innumerable decisions you make everyday that affect your project for better or worse. You need to recognize these as the opportunities they are. Here are a few things you can do every day:

  • Code in plain English. Use an active voice (just like writing). What do you think this method does?
  • dao.findCustomerBy(order);

    Or what about this if statement?

    if(admin.hasPermission(Permissions.VIEWFILE)){
       // allow...
    }

    or better yet…

    if(admin.hasViewFilePermission()){
       // allow...
    }

    The pretty method on the Admin class looks like this:

    public boolean hasViewFilePermission(){
       return hasPermission(Permissions.VIEWFILE);
    }

  • Make Stuff Obvious. Quick, what does this line of code do?
  • Date dt = march(28, 1973);

    When I’m reading through unit tests, I’d much rather see the above statement to create a date than the equivalent Java:

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, Calendar.MARCH);
    cal.set(Calendar.DATE, 28);
    cal.set(Calendar.YEAR, 1973);
    Date dt = cal.getTime();

    You can find those convenient date methods here: dates.java (it’s Free software). Use Java 5’s static imports to make the short date seen above.

  • Be Merciless. Be your own worst critic when reviewing your code. Always strive to improve what you’ve written. Just as great essays and novels (and books like “Don’t Make Me Think”) require several rounds of editing, so too does your code.
  • Never nest ternary statements. ’nuff said.
  • Write comments, but be brief and explain why your code does what it does, not how it does it. We already know how it does it, we’re looking at the code.
  • That’s it. Three steps to better code. Putting it into practice won’t be easy, but if you want to be a master of your craft you’ll embrace the challenge and write things simply on purpose. The people who follow you and maintain your code will appreciate it.

    Categories: Code Hints, Design, Engineering, HOW TO Tags:

    HOW TO: Better JavaScript Templates

    March 5th, 2008 Mark Turansky 4 comments

    JavaScript Templates (Jst) is a pure Javascript templating engine that runs in your browser using JSP-like syntax. If that doesn’t sound familiar, check out the live working example on this site and download the code. It’s Free Open Source Software.

    Better JavaScript Templates

    Categories: Code Hints, Engineering, HOW TO, Technology Tags:

    HOW TO: Download & sort pictures from your camera using Python

    January 18th, 2008 Mark Turansky 1 comment

    I’ve got tens of thousands of photos. When I last checked, the size on disk was over 25gb. And why not? Film is free!

    How do I keep track of them all?

    First, there’s Picasa from Google. It’s awesome. It’s the iTunes of photos.

    Second, there’s a little Python script I called “DownloadPhotosFromCameraAndSort.py” (It’s .txt on the server, rename to .py if you download it).

    If you couldn’t tell, I like meaningful names for my scripts. Likewise for my Java classes, components, projects, etc. Good code communicates it’s purpose clearly without too much human parsing.

    The script does the following:

    1. Find my camera
    2. Download all pictures/movies to 1 or more destinations
    3. Confirm the download before deleting the picture from the camera
    4. Sort the pictures into a dated subdirectory based on the individual picture’s last modified time

    The dated directories give me a running chronology of my life and the lives of my wife and daughter. It’s not a fancy archival system, but it’s simple and easy for me. It may work for you, too.

    The archive looks like this:


    photosdir.jpg

    Categories: HOW TO, Python Tags:

    HOW TO: Bootstrap Java programs in isolated classloaders

    January 11th, 2008 Mark Turansky 12 comments

    Bootstrapping is the process by which you load a very small and very simple pure java program with no dependencies that, in turn, loads, configures, and runs more complex programs with varying dependencies. Bootstrapping lets you run your container without polluting the system classpath. This allows you to run your deployed applications with the unpolluted system classpath as its parent. You’ve achieved classloader isolation.

    When would you want to bootstrap? Any time you want an unpolluted system classpath, which I’m finding is often convenient.

    Let’s say you want to write some kind of middleware product, a container of some sort that deploys other applications within it. You will run into classloading issues. The dependencies that your container has (say, Spring 2.0.6) may not be what your deployed application requires (maybe, Spring 1.2.6). You will find that you cannot have commons-logging in both applications (container and child). There are many ways to encounter java.lang.LinkageErrors. It’s very easy to cross the streams when running in a mutli-app environment.

    What you want to do is load your container and deployed apps in splendid isolation from each other. How do you do that? Bootstrapping!

    Here’s how you bootstrap…

    
    import java.io.File;
    import java.lang.reflect.Method;
    import java.net.URL;
    import java.net.URLClassLoader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Bootstrap {
    
        public static void main(String[] args) throws Exception {
    
            /*
                Assume your application has a "home" directory
                with /classes and /lib beneath it, where you can put
                loose files and jars.
    
                Thus,
    
                /usr/local/src/APP
                /usr/local/src/APP/classes
                /usr/local/src/APP/lib
             */
    
            String HOME = "/usr/local/src/YOURAPP";
            String CLASSES = HOME + "/classes";
            String LIB = HOME + "/lib";
    
            // add the classes dir and each jar in lib to a List of URLs.
            List urls = new ArrayList();
            urls.add(new File(CLASSES).toURL());
            for (File f : new File(LIB).listFiles()) {
                urls.add(f.toURL());
            }
    
            // feed your URLs to a URLClassLoader!
            ClassLoader classloader =
                    new URLClassLoader(
                            urls.toArray(new URL[0]),
                            ClassLoader.getSystemClassLoader().getParent());
    
            // relative to that classloader, find the main class
            // you want to bootstrap, which is the first cmd line arg
            Class mainClass = classloader.loadClass(args[0]);
            Method main = mainClass.getMethod("main",
                    new Class[]{args.getClass()});
    
            // well-behaved Java packages work relative to the
            // context classloader.  Others don't (like commons-logging)
            Thread.currentThread().setContextClassLoader(classloader);
    
            // you want to prune the first arg because its your main class.
            // you want to pass the remaining args as the "real" args to your main
            String[] nextArgs = new String[args.length - 1];
            System.arraycopy(args, 1, nextArgs, 0, nextArgs.length);
            main.invoke(null, new Object[] { nextArgs });
        }
    
    }
    
    

    You can try this code out for yourself. Cut & paste the bootstrap code above into your favorite IDE, put that single Bootstrap.class onto your classpath, and run it like so:

    
    java -cp . Bootstrap sample.HelloWorldMain Hello!
    .
    

    Click here to download the sample /usr/local/src/YOURAPP application.
    Tip for Windows users, you can make the path c:\usr\local\src\YOURAPP it’ll work.

    Switch to our mobile site