Sunday, November 13, 2011

Is microtime enough to get consumed time in software application

I heard that few engineers are debating on to find out actual time consumption in profile page and specially accuracy in php micortime. A sort of benchmarking that tells how much CPU utilization were involved in new peaces of code.

We live in a time of multi-tasking,multi processors and background processing. So debating on microtime or cpu usages is worthless if you are working for web application. Each and every measurement and benchmarking is required to make robust and reliable application.

Lets back to system time,wall time debate.

Open a browser for one hour doesn’t mean that computer has spent an hour of dedicating it’s resources to the browser.(unless it’s IE of course…:P)

Also its depend on application. APC,memcached,varnish,gearman,cassandra etc can divert the result to benchmark application as well as estimated results.

There are three different types of times which was taken by application.

1. System time(processor spends on behalf of the current process)
2. User time (time spend in executing code in user mode)
3. Wall time (Amount of time that passes on our wall clock while process runs)

The advantage of system time is it will give the amount of time that any subproccesses right down to the low lever system processes (ie. file i/o, socket operations) take. This can also be a
disadvantage though because it may make it harder to determine if a bottleneck is happening in your code or in some sub-system your code uses.

getrusage() information is CPU time used and microtime() is wall clock time. The program may run for 10 minutes according to the clock on the wall, but internally may only use a few seconds of CPU
time. Then there's contending for CPU time with all the background programs running on the system, resource contention, plus regular housekeeping.

There's far too many factors involved to be able to get an accurate timing for such short periods. Doing three runs of the while(microtime()) version of loop, I got the following timings:

user: 0.98, 0.09, 0.90 sys: 0.12, 0.05, 0.94

Obviously quite the variance. Even just a simple has utime/stimes ranging from 0 to 0.03.

Try running application longer periods, and do something within them to increase cpu usage.

API in PHP to check server Load
// make sure safe mode shud be off

function ServerLoad()
    $stats = exec('uptime');
    preg_match('/averages?: ([0-9.]+),[s]+([0-9.]+),[s]+([0-9.]+)/', $stats, $regs);
    return ($regs[1].', '.$regs[2].', '.$regs[3]);

Happy Coding !!! Enjoy !

I wanna exit from a user defined php function if it takes longer time than expected

I think this is common problem that we suffered in our applications. There are many activity in application which took longer time, its shown bad user experience as well as sometime its thrown unexpected errors
like blank page etc.

In php there is no way to judge this thing unless your code is written in multi processing manner.

Let's look on function signature.

function downloadCSV(activityid)
1. is logged-in user (user has proper credits and authenticate user again)
2. RPC to get activity detail (activity is marked as pending/done/in progress etc)
3. RPC to get userids as result from activityid
4. RPC to get user details
5. Make CSV
6. detact credits from user account and update log tables in DB
7. download CSV

This API can fails in various steps.

1. network timeout/failure
2. DB was down
3. Server was down
4. RPC fails because as above reasons
5. Time out due to get user detail
6. DB server was hogged-up

Below i listed, what i figured out from Internet however there are other solutions too but i keep in mind to untouched existing code as less as possible.

1. Check HTTP code using curl

$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
/* Handle 404 here. */

2.  Using curl to set time out time

curl_setopt($curl, CURLOPT_TIMEOUT, 2);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);

3. Provide socket time out settings

ini_set('default_socket_timeout', 10);
$url = "";
if( file_get_contents( urlencode($url) ) === false ) {
   // failure
} else {
   // success

4. Using PHP's stream_set_timeout settings
stream_set_timeout($obj, 2);

Another approaches are forking your code in such manner that it divides in different threads then we can set a timer that will exit after some configurable value.

design components of distributed application architecture

Its requires time to discuss in details, however this post is in draft state but i figured out 6 important layers in architecture. Please go thrugh and feel free suggest if you have any better suggestions.

  1. Create a web service with something like Apache Axis
  2. Use an ESB - something like Mule or JBoss
  3. Use a simple web Servlet on the server, and submit data using HTTP POST. You could use a simple embeddable Java web server like Jetty to do this.
  4. Use a messaging protocol like Kryonet or Google's protocol buffers
  5. Use a more general network application framework such as Netty

Sunday, November 6, 2011

funny programming terms that we share in workplace

I have maintained a list. Please feel free add.


It mean cheap-less code but it works well.


Impressive code that was written in very short time.


Things are properly understandable.

Quantum bug

The bug that fails to occur when trying to observe it (ie tracing through code a line at a time).

Code Monkey

An insulting term to describe a poor programmer, usually who does not grasp basic or common programming concepts, and sometimes whose best coding capabilities can be described as "GoogleCut&Paste".

Ghost Bug.

Referring to a bug that cannot be reproduced in controllable conditions, a bug that seams to have appeared but no one is sure about it. A bug that requires voodoo for fixing. A bug
that drives a developer to think that a mutex should be used in a single threaded app.


The process of taking code and refactoring it without consequence to make it do what management demands that the code do.

Faith based programming

When "Jimmy", instead of using a more.. "scientific" approach to problem solving, just randomly delete, comment or rename a variable/line of code and prays for it for compile/runs.

Pixie Dust

A "tool" used by developers to "magically" fix certain issues via abnormal/illogical/unexplained means. (coined by the leader of our support team) When an issue completely baffles the
development team we are "out of pixie dust".

A**hole Features

Features that are thought of during release planning that add little to no actual value to the software.


To use Agile methodologies and have people totally screwing it up.

Hi-driven development

When you debug your program by writing alert('Hi') statements in a trial-and-error fashion

Disaster Driven Development

When Your PMs and salesmen promised that You will build "space shuttle" in one month.

Hope Driven Development

A software development technique in which an application is developed in a long unplanned development cycle, with minimal "Steve Irwin-style testing", all with the hope that
everything will work as intended when released.

Different kinds of bug reports:

Smug Report - a bug submitted by a user who thinks he knows a lot more about the system's design than he really does. Filled with irrelevant technical details and one or more suggestions (always
wrong) about what he thinks is causing the problem and how we should fix it.

Drug Report - a report so utterly incomprehensible that whoever submitted it must have been smoking crack. The lesser version is a chug report, where the submitter is thought to have had one too many.

Shrug Report - a bug report with no error message or repro steps and only a vague description of the problem. Usually contains the phrase "doesn't work."


The process of taking a well-designed piece of code and, through a series of small, reversible changes, making it completely unmaintainable by anyone except yourself.


Can't take credit for this, but it is awesome!
A computer bug that disappears or alters its characteristics when an attempt is made to study it.


A catastrophic data destroying bug - "Oh the humanity!"


A bug you present when presented with a bug caused by the person presenting the bug


A bug that accidentally generates money (just did this one)

Fear Driven Development

When project management adds more pressure (fires someone or something).

Common Law Feature

A bug in the application that has existed so long that it is now part of the expected functionality, and user support is required to actually fix it.

Hydra Code

Code that cannot be fixed. One fix causes two new bugs.It should be rewritten.


A prototype that ends up in production.

Ninja comments

Also known as invisible comments, secret comments, or no comments.

Chunky salsa

Based on the chunky salsa rule, a single critical error or bug that renders an entire system unusable, especially in a production environment.


Sometimes, you just have to talk a problem out. I used to go to my boss and talk about something and he'd listen and then I'd just answer my own question and walk out without him saying a thing.

I read about someone that put a rubber duck on their monitor so they could talk to it, so rubberducking is talking your way through a problem.


"Hey, I'll put all of our customers into a Word document and then we can X." "No, we should do that database-ically so that we can keep that list up to date."

Hooker Code

Code that is problematic and causes application instability (application "goes down" often).