ClickOnChris

Christopher G Johnson: programmer, entrepreneur

Archive for the ‘Programming’ Category

Making Software that Doesn’t Suck – With Automated Testing

without comments

I’ve seen a lot of different attitudes towards automated testing over the years.   They range from zealous acceptance to complete rejection.  I would like to cut through the rhetoric, pre-conceptions, and mis-conceptions, and start the conversation from the only place that makes sense.

First, a couple of definitions:

Unit Tests – Low level tests for application logic.  Inputs will be mocked or stubbed.  These do not make calls to services or external databases.  They must run quickly, easily, and frequently. Example: testing a method that calculates taxes based on a matrix of product types and zip codes.

Integration Tests – Application logic tests that may make calls to external services or databases.  These are slower and may run less frequently. Example: testing payment submission such that a payment gateway is called, a successful response is received, and that the response is logged to the database.

Functional Tests – Tests that run from a user perspective.  Usually this will mean invoking a user interface and testing that it reacts appropriately.  Example: testing that the user can login, place an order, and receive a confirmation email.  These tests are not the focus of this article

The Business Case: How Does Building Unit Tests Help Me Make Money?

Make no mistake: there are costs associated with building tests, and may require a large upfront investment before any benefit is realized.  In fact, to the non-technical manager, this combination of fuzzy benefits and clear costs may make testing seem like a poor investment.  That line of thinking is at best short-sighted, and at worst, blatantly negligent to your product’s future costs.

Testing helps your product development cycle move faster

This may be counter-intuitive. As it turns out,  a the whole team has much higher confidence that the new features have not broken existing features when those existing features are continuously and automatically tested.  The result is a QA cycle with less defects, which finishes faster and gets the product to market quicker.  The business gains more confidence in product quality during each product development cycle.  The tech team also gains confidence – unafraid to jump in and modify critical parts of the system to meet the latest business need.  The tests got yo back.

Tests provide historical proof about how the application is supposed to function

One client I had actually opposed maintaining a unit test suite for their enterprise system.  When it came time to make some minor enhancements to the billing system it turned into a big project.  The billing logic had become un-maintainable spaghetti from years of monkey patching by developers who had come and gone over the years.  In some cases the business people couldn’t even tell us how it was supposed to work.  We were literally afraid to modify it.  We limped through the project, but started planning a rewrite of the building system as a future project, involving not just the tech team but also business resources to properly document the functionality.

 

We test because we care about our customers and because we care about our software.

 

 

If you’ve read this far, check out my latest project: FindMyBeer , where you can find who sells or serves craft beer near you.

Written by clickonchris

October 19th, 2012 at 6:52 pm

Posted in Programming,testing

Download Sources for SpringSource Tool Suite and Grails 1.3.7

with 2 comments

Grails is a great framework framework that enables rapid development with Java. Like with any framework however, you sometimes get stuck and need to take a look under the covers to solve a problem.

Today I was fighting with mapping a many-to-many relationship (a common occurrence in grails), and needed to figure out exactly why Grails and Hibernate were not doing what I expected.  One of the great things about Java is that nearly the whole stack is open source so you can just step through the code to see what is going on, as long as you can find the code (and navigate through injected dependencies, but that is a different story).

Eclipse Maven plugins provide great tools to ‘Download Sources’ start viewing them immediately when you step into some third party library code.  I am developing a Grails application on SpringSource Tool Suite (STS, v2.8.2 as of this writing), which is becoming the industry standard IDE for Spring and Grails based applications.  Since Grails uses a Maven-like dependency management system, you would expect STS to be able to download sources for for any of the grails dependencies easily, right?

Not quite.

While this is a feature that might work for Grails 2.0, if you’re using Grails 1.3.7 then you will find a plugin named eclipse-scripts that enables you to download sources and then configure your projects so that STS can find the sources.  Here’s what you do:

grails install-plugin eclipse-scripts
grails compile
grails download-sources-and-javadocs
grails sts-link-sources-and-javadocs

Then restart STS and refresh your project.  Now you can navigate into your project’s Grails Dependencies and view their source through STS!

I found the solution here: https://issuetracker.springsource.com/browse/STS-1440?focusedCommentId=36879&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-36879

Credit for creating the eclipse-scripts plugin to Lari Hotari

Written by clickonchris

January 1st, 2012 at 6:19 pm

Posted in Grails,Groovy

Groovy Lunch and Learn

without comments

I gave a talk at the Geneca office back in July and did not realize that it was available on the internet until today.

Linky: http://genecalal.posterous.com/groovy-talk-thursday-july-21

Written by clickonchris

December 13th, 2011 at 11:03 pm

Performance Optimization: Doing Science

without comments

I couldn’t hold a candle to Brian Green on such topics as Quantum Entaglement, Higgs Boson, or Grand Unified Theory (despite obtaining a B.A. in Physics), however I can apply the scientific method to improving the performance of your software.

The Scientific Method (sciencebuddies.org)

In this article I will explain a basic, but often overlooked foundation for improving the performance of any software application.

Much of software development is an art, but performance tuning is a science.  I’ve seen a lot of good developers waste time significant amounts of time on performance with little to show for it, or just as bad, improve performance without knowing exactly which change had the desired effect.

Do you remember talking about the Scientific Method from your high school science class?  The diagram on the right is a refresher.  The scientific method is the repeatable process on which all scientific exploration is based.  It gives scientists across the world a common language and framework to compare the process and outcomes of experiments.

The scientific process provides a few of important points that can be applied to software performance optimization:

  1. Repeatable process – use the same process for every performance enhancement you make
  2. Only modify one variable at a time – Do not make multiple tweaks at the same time.
  3. Record the results of each optimization.  Track what you did and how much it helped.

Performance Optimization Method (clickonchris.com)

This sounds simple right?  It is.  The tough part for software developers is to never break these rules during a round of optimizations.  To the right I’ve also included a more detailed diagram of what the scientific process looks like when applied to performance optimization.  Let’s call it the Performance Optimization Method.

But I know what I’m doing!  Why shouldn’t I make multiple tweaks at once?

Lets say you do make two changes at once.  You optimize two queries and drop the page load time from 3s to .1s.  Do you know how much relative impact the changes had?  Did each change reduce the cost by the same amount (50%/50%)?  Did one query account for most of the cost (75%/25%)?  Or did one of the changes not even have any impact (100%/0%)?  What if the two changes were somehow interdependent?  For the most part these questions are impossible to answer unless you use a repeatable process and only modify one variable at a time.  There are exceptions *(there are always exceptions.  If you have a good profiling tool that tells you exactly what two different method calls cost and you are absolutely sure they are not somehow related then you could cut a corner and make multiple changes at once.  If the results do not turn out as expected you still need to go back and make the changes one at a time).  By the way, I hope you are testing against a volume of data you expect in production.

Don’t forget to record the result of each optimization.  This way you can throw your results into a table, and with a little explanation about the process and results you turn it into a report and send it to management so they can see how you’re spending their budget (and how good you are at science).  Having these sorts of metrics reports also makes it easy for stakeholders to justify the time spent on performance optimization activities.

The law of diminishing returns applies to performance enhancements.  At some point you will have picked all of the low-hanging fruit and enhancements start to get progressively more expensive.  Stakeholders need insight into how this is progressing on your project so they can make decisions on how much more to spend on performance.  Metrics reports should provide sufficient detail for stakeholders to make those decisions.

Ultimately you will end up with a faster application and a clear story of how you got there.  Isn’t science fun?

Written by clickonchris

June 27th, 2011 at 9:37 pm

Reducing JBoss’s Memory Footprint

without comments

I am working on a project to convert a handful of J2EE applications from an Oracle OC4J application (no longer supported) server to JBoss 5.1.0.  Among the many challenges in the conversion is the fact that JBoss’s default profile has a significantly larger memory footprint than OC4J.  In the past I have just accepted that Jboss uses over 400MB of heap space before you even deploy anything. This time however we were hoping to reuse the same hardware from the old application server with the new application server.  When the test system started paging and eventually using up all of the physical memory available, we were forced to choose between ordering more memory and trying to tune jboss to reduce the memory footprint.

We ended up having a lot of success reducing the footprint through tuning.  Bottom line: we reduced the memory footprint by 120MB, and the startup time from 53s to 24s

Here were the steps taken

Heap Size (MB) Used (MB) Reduction in Used (MB)
Starting Heap 419 314
commented out debug level MBeans annotation in deployers.xml 322 247 67
removed ejb3 services 317 238 9
removed messaging folder & props 310 238 0
removed seam & admin-console 256 205 33
Removed xnio-deployer and xnio-provider 256 203 2
removed ROOT.war 256 203 0
removed management 256 199 4
removed jbossws.sar 256 193 6

 

The instructions for each step can be found : http://community.jboss.org/wiki/JBoss5xTuningSlimming

Notes on my environment and testing process:

  • Windows XP, JDK 1.6.0_22,
  • JBoss 5.1.0.GA.  Xmx=512M , Xmx=256 (this is why heap didn’t drop below 256)
  • I used jvisualvm to watch the heap and “used” memory values
  • For the “Used” memory, I took the maximum observed value while JBoss was starting.  If you understand that a time vs. memory usage graph follows a sawtooth pattern as objects are instantiated and garbage collected, then I took the value from the tip of the highest tooth.

Written by clickonchris

June 1st, 2011 at 11:58 am

Posted in Java,JBoss

No Fluff Just Stuff 2011, Madison, WI

without comments

I attended the No Fluff Just Stuff conference in Madison, WI this weekend. It was a great chance to learn the newest Java trends and share struggles in programming with people much like myself, even if most of them were cheeseheads. Here are some of my reflections on the state of Java tech post-conference:

-Java 7 is underwhelming, mostly because it will not have closures. It will however introduce enhancements to speed up Groovy, JRuby, and Scala

-If I read between the lines what features are in HTML and any of them are supported by Chrome, then I think that HTML5+Chrome could easily turn into a gaming platform!

-There is no question that Groovy is the “next big thing” for Java. Get on board.

-A java developer could easily add Hadoop to his resume (and dollars to his pocket), by learning Hadoop with Cascading. Hadoop-worthy scale data sets are available for free from amazon: http://aws.amazon.com/publicdatasets/

Grails is really cool. I wish there was a hosting platform available that was anything close to Heroku for Rails. I think it is unlikely that we will hear any good Startup stories with Grails for that reason.

Written by clickonchris

February 27th, 2011 at 1:54 pm

Facebooker2 Demo Video

with one comment

I published my first ever screen cast tonight. enjoy!

The source code is available here: https://github.com/clickonchris/facebooker-demo

Written by clickonchris

February 6th, 2011 at 3:08 am

Sprite Club

without comments

UPDATE: Sprite Club’s code has been open-sourced :  https://github.com/clickonchris/spriteclub

===========================================================

I have been working on a Facebook application in my spare time for quite a while now and I think that it is finally interesting enough to start telling people about.

Sprite Club banner

The application is a game called Sprite Club; it is a place where you can judge kids (sprites) on the basis of cuteness, and upload a photo of your own sprite to have them compete for internet fame.  The application is accessible from

apps.facebook.com/spriteclub

or from the friendly url:
www.spriteclub.net

The idea was conceived by my uncle, Matthew Deutch, while looking for an outlet to prove that his daughter is the cutest kid on the internet (We think she’s pretty great).  I am in the project to learn how to do Facebook and social media development.  The application is written in Rails and uses the Facebooker library to interface with Facebook.

Enjoy!

Written by clickonchris

January 15th, 2011 at 8:40 pm

Facebooker – converting from FBML to iFrame

with 4 comments

In my free time I dabble in Ruby on Rails development and Facebook application development.   Last year I picked up Mike Mangino’s book, Developing Facebook Platform Applications with Rails, which gives great step-by-step instructions for creating a facebook FBML application using the facebooker gem.

FBML is not the only type of Facebook application, however.  There is also the iframe method, which Lead Facebook Engineer Charlie Cheever has been endorsing for a while.  His blog post gives a good comparison of the two methods:http://www.ccheever.com/blog/?p=10 .  In addition to this, the Facebook developer roadmap (http://developers.facebook.com/roadmap ) shows that soon you will not be able to create new FBML applications, and is recommending that existing FBML applications be migrated to iframe.  The message is clear: time to switch to iframe people!

Unfortunately the tutorials performing this kind of switch are hard to come by.  Mike Mangino has created a new RoR gem named facebooker2 to support iframe and facebook connect development but documentation is limited.

This blog post includes some documentation on how to convert a facebooker FBML app to a facebooker2 iframe app.  Read Charlie Cheever’s blog post to understand the architectural differences between the FBML and iframe approach, especially the bit about server-side FBML.  More details here: http://developers.facebook.com/docs/reference/fbml/serverFbml

Remove the facebooker gem

Get Facebooker2 and mogli


$gem install facebooker2
$gem install mogli

Reccomended: unpack gems to /vendor (http://errtheblog.com/posts/50-vendor-everything)

Update environment.rb, set

RAILS_GEM_VERSION = 2.3.5

Create an initializers/load_facebook_yaml.rb file to read from facebook.yml.  It should include a single line:

Facebooker2.load_facebooker_yaml

Inside your facebook.yml file add the following two parameters to each environment config:

app_id: 131336175362
secret: f7b15a925058e7be60f0a4d56294e938

Application controller

Rename application.rb to application_controller.rb. Add the following command to your application_controller.rb

Include Facebooker2::Rails::Controller

Comment out the set_current_user method

Paste in this method:


  def current_user
    if session[:user_id]
      @current_user ||= User.find(session[:user_id])
    elsif current_facebook_user and @current_user.nil?
      @current_user = User.find_by_facebook_id(current_facebook_user.id)
      session[:user_id] = @current_user.id
    end
  end

Add a ‘ensure_authenticated_to_facebook’ method to keep making sure that the user has a facebook session when accessing your site.  In my case my sessions/login page just displays “please login to facebook”, and since we’re in an iframe in the context of apps.facebook.com/myapp, there is a facebook login prompt displayed immediately above my page.


before_filter :ensure_authenticated_to_facebook  
def ensure_authenticated_to_facebook
   if current_user == nil
     logger.info "current user is nil"
     redirect_to :controller=>'sessions', :action=>'login'
   end
end

With Facebook Connect it is not required that your application runs inside the context of apps.facebook.com/myapp.  Your login page could technically detect if it is running in app.facebook.com and only display “please login”, otherwise if you are running it as a standalone page present the user with a facebook connect login button.

Enable oauth 2.0.  This will facebook cause to send a “signed_request” request parameter to your application once the user has authenticated.  Mogli will use this token to authenticate the user and create the current_facebook_user object.

Copy/Rename your .rhtml.erb files to .html.erb.  You will no longer have a canvas and will never render rhtml files again.

Remove  <%= facebook_messages %> in your erb files or replace them with <%= flash[:error]  %> or <%= flash[:error]  %>

Put the facebook connect javascript initializer in your layouts file so that it gets executed during each page load


    <% fb_connect_async_js  do %>    
      <%= yield :fb_connect%>
    <% end %>

This should always be the first snippet of javascript to be executed as it will instantiate the FB global variable that the rest of your page will probably use.

Start going through your erb files and replace FBML code with <fb:serverFBML /> and their javascript SDK equivalents. This is pretty much the final step and will require the most effort. Use the documentation and test console found here: http://developers.facebook.com/docs/reference/javascript/

-facebooker2 reference project: https://github.com/mmangino/facebooker2_fb_connect_example

-images for stream publisher popups need to be accessible by facebook servers.  You used to use a reverse tunnel so facebook could read them directly from your local server.  If you have images that fall into this category you may still need to use a reverse tunnel.

Written by clickonchris

November 9th, 2010 at 12:24 am

Setting the Default Version of Java in Windows

without comments

You’re a java programmer on a Windows development environment.  You fire up the command prompt and run “java -version”.  It’s some JRE and its not even the version you want!  Eclipse is throwing a fit.  There’s no good way to figure out what path the “java.exe” you are executing lives in.

The most surefire way to solve this problem is take the java bin path (ex: C:\program files\Java\jdk1.6.0_07\bin)  you want and prepend it to your System level PATH environment variable, as highlighted below.  This will ensure that you are executing the java.exe from the expected java installation every time.

Written by clickonchris

October 27th, 2010 at 8:32 am

Posted in Java

Switch to our mobile site