Saturday, September 24, 2011

My experience with unit testing in software development

I am not a fan of software engineering which is taught in school. Waterfall model, spiral model,  many blah blah phases ,diagrams and documents. I usually thought of those as useless works or not worth to do.  I thought that software development should be simple. Get requirement -> Draft design -> Go coding! ->  Debug -> Someone test it manually -> Debug -> Test again (until it's acceptable) -> OK, Finished

Documentation should exist only if it is essential and the source code is one of the documentation. This methodology works fine because I usually develop alone or in a small team and the software does not have many modules.

Unit testing is one of those which I thought it's not worth to do. I agree that's it's useful for regression test of the code but I thought I know what I've done with my code and know what are affected. 

Around two years ago, I and my colleagues started a new project. One of them wanted to try a kind of Agile development model (I don't know the name. I've said I'm not a fan of these models) and we tried.

He encouraged me to write the unit test for each class and a few acceptance tests for overall project behaviors. I did it because I wanted to give it a try too.

Then I know I was wrong about testing. I usually see some bugs because the tests fail. In addition, I feels great when all the tests go green. The tests guarantee that the software still work as it should. However, I see some disadvantages or difficulties in writing test.

Sunday, August 14, 2011

VisualVM on ubuntu(lucid) failed to initialize the Profiler engine

I installed VisualVM on my lucid(amd64), luanch it and it shows an error.
Failed to initialize the Profiler engine: Problem with a required JFluid installation directory or file /usr/lib/jvm/java-6-openjdk/lib/visualvm/profiler3/lib/deployed/jdk16/linux-amd64/libprofilerinterface.so Original message: File does not exist.
In sprite of this error, the visualvm still can run but profiler/sampler doesn't show up.
Someone have filed this bug here. To fix this problem, recompile the missing lib manually and move to the right place.
Here are the steps
  1. Download visualvm source here (visualvm_1.2.2.orig.tar.gz at the right pane)
  2. Extract it and cd to the build directory
    tar -xzvf visualvm_1.2.2.orig.tar.gz
    tar -xzvf netbeans-profiler-visualvm_release68.tar.gz
    cd lib.profiler/native/build
  3. You will see build script ( buildnative-linux64.sh ) then edit it. 
    1. Replace gcc32 with gcc
    2. Change 2 BuildJDK lines to below. /usr/bin/jvm/default-java/ is my jdk path; yours may differ.
      BuildForJDK "/usr/lib/jvm/default-java" "jdk16"
  4. Make directory for release
    mkdir -p ../../release/lib/deployed/jdk16/linux-amd64/
  5. Run this build script. It will compile the source against jni headers in our jdk and output as libprofilerinterface.so
    ./buildnative-linux64.sh
  6. Move libprofilerinterface.so to the right place in your jdk path
    sudo cp ../../release/lib/deployed/jdk16/linux-amd64/libprofilerinterface.so /usr/lib/jvm/default-java/lib/visualvm/profiler3/lib/deployed/jdk16/linux-amd64/
  7. Launch VisualVM again. 

Sunday, September 5, 2010

How to reset the main menu in Ubuntu

I rearranged and deleted some menu items in the main menu. Finally they are in a mess. Some menu is missing and I can't remember the original position or command. I want to reset them to default like fresh install. I don't see any way to reset it easily. There's a revert button but It doesn't work right. So I searched and dug in some configurations.
Then I learn that there're default menu configuration somewhere. One of them is at /etc/xdg/menu which there're applications.menu and settings.menu for application menu and system setting menu respectively. And each user has its own customization of menu in ~/.config/menus which has applications.menu and settings.menu too. Those files contain some exclude configurations and they are merged with default configurations when it is used.

So deleting directory ~/.config/menus should reset the main menu to default state and that's all.

rm -rf ~/.config/menus  

and logout and login again.

but if it's still not be reset like in my case, it need to delete *.desktop in your home directory too.
find ~/ -name "*.desktop" -delete 

Wednesday, July 28, 2010

How to forward port by ssh

There are 3 machine
  1. My machine
  2. Host A with public IP
  3. Target Host B with can be reached from Host A
The goal is that I and my friends want to access target host B services in convenience. We can ssh to Host A with public IP.

Local Forwarding

Run this command on my machine

ssh -L 123:targetB:80 userA@hostA
  • We can access targetB:80 by myMachine:123 instead
  • The traffic will be forward from myMachine:123 through ssh tunnel to hostA and the forward to targetB finally.
Remote Forwarding

Run this command on hostA

ssh -R 123:targetB:80 me@myMachine
  • We can access targetB:80 by myMachine:123 instead
  • It's similar to local forwarding but the ssh tunnel is created by hostA which is the forwarder
  • The traffic will be forward from myMachine:123 through ssh tunnel to hostA and the forward to targetB finally.
Dynamic Forwarding

Run this command on my machine

ssh -D 123 userA@hostA
  • It's similar to local forwarding but it doesn't specify the target:port because the target address and the port will be determined automatically by application protocol such as SOCKS.
  • We can access targetB:80 by SOCKS proxy in browser or OS network settings.
  • Currently SOCKS4 and SOCKS5 protocols are supported.
Remarks
  • There might have firewall issues. It might have to change some port unless the packets will be blocked.
  • This method is for TCP traffic. For UDP, you may have to try other methods.

Wednesday, June 30, 2010

How to setup remote profiling session with JProfiler

What does JProfiler do?
JProfiler is a tool to profile a JVM in runtime locally or remotely. Its profiling features are mainly in 2 parts which are CPU and memory.

In CPU part, it can shows all threads situation in JVM and its state(RUNNING,BLOCKING,WAITING,..) and also traces which part of instruction the thread currently runs. It can show object locking graph to identify a locking problem which is difficult to figure out by normal debugging.

In memory part, it collects and shows memory allocation stats, object counts and also traces the methods or classes which do most of memory allocation. Furthermore, it can shows what and when java garbage collector do with current memory consumption as a time-based graph.

How to setup?


  1. Download JProfiler and its evaluation key

  2. Install JProfiler in your computer.

  3. Copy JProfiler agent library to a target which you want to do remote profiling. JProfiler agent library is in [JProfiler path]/bin/

  4. Add an argument in JVM which you want to do profiling. (I use FreeBSD.)

    -agentpath:/usr/home/tmp/jprofiler6/bin/freebsd-x86/libjprofilerti.so=port=8849

    ** 8849 is a port for JProfiler client to communicate with its agent. You can change this

    if you have firewall, you may need to allow the port

    ipfw add 6000 allow tcp from any to me dst-port 8849 keep-state




    1. Open JProfiler client. -> Start center -> New session tab -> New session button -> select remote -> configure host and port of target jvm -> Start

    2. Start target JVM. it will pause and wait JProfiler to attach it.

    3. In JProfiler client, click Attach and OK

    It should be work now.
    Next time I may write about how to setup JProfiler with Eclipse to do local profiling. If you can’t wait, find it in JProfiler help.
    ** When the evaluation ends, the client still can open but it doesn’t work

    Friday, February 5, 2010

    Picasa's bugs cause my images to be deleted.

    I use Picasa client and Picasa Web Album for a time and it works enough for me. In addition to managing my photos in Picasa client, I also use Picasa Web Album as images hosting for my thai blog. It's really easy and safe in an album.

    Yesterday, an unlucky day, I wanted to sync the blog album to my Picasa client and tried setting them to keep syncing in the future therefore I can upload an image via either the client or the web album. It would be great, isn't it?
    I clicked the "download album to picasa" button in the web client. The album was downloaded to the client beautifully and it was shown as a downloaded web album. Then I clicked the "sync to web" button in the client. It asked something and uploaded all images in the album. Why did they need to be uploaded again?
    The problem is here. In the web client, there were 2 album now in the same name, the original and the recently uploaded. At first I thought it was not a bug, you might need to have a fresh uploaded album at the first time to keep syncing in the future and these twin albums was different album and unique.
    and this idea leads to the mistaken operation.
    I right-clicked at the downloaded web album in the client and chose to delete the album with "delete the online copies" checked. Then both original and recently uploaded album were gradually deleted! and they were deleted successfully.
    So I don't have any copies of this album left. It's a bug of Picasa and I reported to its forum yesterday.
    but good luck of me that the album has only 25 images and google still cache some of them. :P

    Sunday, January 24, 2010

    How to change 'open link in new tab' behavior in Firefox 3.6 back to the old style.

    I downloaded Firefox 3.6 yesterday and I see that the 'open link new tab' behavior is changed. In the previous version, an opened new tab will be inserted at the rightmost in the tab pane but now the new tab will be inserted next to the current related tab. It's just a style but I like the old one.
    To change it back to an old style. Steps are below.

    • Enter about:config in the location bar.

    • Press "I'll be careful, I promise!" button. yes, you have to be careful about this.

    • Type tab in filter box as in image.


    • Find the preference name browser.tabs.insertRelatedAfterCurrent and double click it. The value of this preference will be toggled to false.

    That's it.