Wednesday, October 3, 2012

Before start coding in Node.JS

Following on the Node.js hype, I've noticed that a lot of people are talking about Node.js but do you really know how it works?
Before start coding in Node.js is necessary to understand how it works internally and understand a new programming paradigm. So, in my talk i'll start talking about the internal architecture of Node.js and which core libraries are being used in windows and linux. It is very important to understand the concept of event driven programming as well as how the event loop works internally and, therefore, prevent the development of bad code or apply Node.js for a function for which it was not designed. The fundamental concepts about Node.js are very simple, but first you have to learn them.

Finally, I will talk about how to do profiling in Node.js, some performance tips and present some real cases where Node.js is being used.

Presentation Topics

  • Internal Architecture
  • Asynchronous I / O
  • Event Driven Programming
  • EventLoop
  • Node.js is single threaded?
  • Flow control
  • Performance tips
  • WebSockets
  • Who & How is using Node.js

If you are interested in this talk, please contact me.

Friday, May 25, 2012

Hash read performance (perl,ruby,python,node.js)


After having read the chapter "How Hashes Scale From One To One Million Elements" from the book "Ruby Under A Microscope" (This post does not dispense the reading of the respective chapter), I decided to play around with the subject and make some tests with other languages and do a small analysis of the performance of each. The chosen languages ​​were PerlRubyPython and Javascript (node.js).

The tests focused on measuring the time in ms when retrieving an element from a hash with N elements 10.000 times and were based on tests that were performed on the book.

The tests were run on an Amazon EC2 server instance (free tier) with the following characteristics:
  • Ubuntu Server 12.04 LTS
  • Instance type micro
  • Intel(R) Xeon(R) CPU E5430 @ 2.66GHz
  • 604.364 KB of memory
  • 64-bit platform

Test description

Programs have been implemented for each of the languages ​​to be tested and they are similar as long as possible.The implementation passed by the creation of hashes with size equals to powers of 2, ranging from 1 to 20.

For each of the hashes created, are made 10.000 gets of the value for a key (key=target_index), and measuring the time in milliseconds.

The ​​measured values are placed in an output file for later to be consumed by a program that will generate the graphics.

Comparing Ruby Versions

From the graphs we can see greater improvements from ruby 1.8 to ruby 1.9 so 1.9 is the the way to go.

Time taken to retrieve 10.000 values (ms)

Comparing Perl Versions

Perl is a language with some years and performance has been stable at least since version 5.8, the results show it.Yet I hoped there were more commitment from the community in trying to optimize these values​​.

Time taken to retrieve 10.000 values (ms)

Comparing Python Versions

The versions of python show few variations and show some stability between versions. However python 3.2.3 shows a slight degradation in performance.
I may also say that it's performance is quite interesting.

Time taken to retrieve 10.000 values (ms)

Comparing Languages

Represented in the graph below are the tests for all languages ​​and respective versions.

Time taken to retrieve 10.000 values (ms)

Python was the winner for the interpreted languages. node.js has a great performance however the library used in node.js is not enough accurate to give us reliable values.
You can easily do the tests in your environment using my scripts, the source code used in this tests can be found on Github. I've used rvmperlbrew and pythonbrew to switch between versions, you can do the same if you want. Nevertheless, you can use your installed versions.

Run the tests:

$ ./experiment1.rb
$ ./experiment1.pl
$ ./experiment1.py

The execution of these scripts will create an output file in the form "values.#{lang}-#{version}" (ex: values.perl-5.8.8) for each script.

To generate the graph just run the ruby script (requires gem googlecharts):

$ ./chart.rb

This script will output the link to the googlechart image.

In the near future I might do the same but for hash writes and that will be more interesting.

Friday, April 13, 2012

Count the number of 1's

I've participated in a Quiz where i had to implement a function that counts the number of 1's required to reach a positive integer passed as a parameter.

Example:
countOfOnes (12) = 5
1 - one 1
3
4
5
6
7
8
9
10 - two 1's
11 - four 1's
12 - five 1's

Since countOfOnes (1) = 1, they have requested to find the next number in which the parameter is equal to the return value of countOfOnes ( countOfOnes(n) = n ).

At first glance I've implemented a perl one-liner that returns the number of 1's for an input number. Here it is:

$ perl -e '$sum += () = /1/g for 1..shift; print "NumberOfOnes=$sum\n"' 111
NumberOfOnes=36

However, this solution was inefficient due to the high number of invocations needed to find the solution to the quiz.

So, analyzing the evolution of the mathematical calculation of the function numOfOnes, I outlined the following recursive algorithm:

# NumOfOnes
sub num_of_ones{
  my $n = shift;

  return 0 if $n < 1;
  return 1 if $n <= 9;

  my @a = split('',$n);

  my $len = length($n);
  my $sum = 0;
  my $delta = 0;

  if($a[0] > 1){
    $delta += 10**($len-1);
  }else{
    $delta += substr($n,1) + 1;
  }

   $sum += $delta + $a[0]*num_of_ones(9x($len-1))+ num_of_ones(substr($n,1));
   return $sum;
}


Since, recursive functions were used, which were invoked repeatedly with the same parameters, I used the Memoize module, which makes caching for functions with the same parameters.

Although the previous function is more efficient for calculating the numberOfOnes just for a single number, it is not when you want to go all the way incrementing to find the next number that satisfies the requested condition. Once we have the numOfOnes for the previous number, we only need to count the numOfOnes that exists in the current number and increment numOfOnes of the previous number.

The following perl on-liner is a better approach to achieve that

perl -e 'while(1){$sum += () = ++$i =~ /1/g; print "Found:$i\n"  if $i == $sum;}'

Found:1
Found:199981
Found:199982
Found:199983
Found:199984
Found:199985
Found:199986
Found:199987
Found:199988
Found:199989
Found:199990
Found:200000
Found:200001

The number after the 1 that satisfies the condition countOfOnes(n) = n is 199981


The source code is here.


Thursday, March 10, 2011

How to collect CPU values from WMI. #perl #win32 #wmi

This program will demonstrate how easy it is to obtain data from Windows Management Instrumentation (WMI), using a perl script. By analogy other information may be collected from WMI.

To obtain these data, I used the WMI Performance Counter class "Win32_PerfRawData_PerfOS_Processor" , which provides me the counters that measure aspects of processor(s) activity.

Since the data collected via WMI are percentages. I just needed to calculate the percentage of time used for each counter, depending on the time interval between measurements.



use strict;
use warnings;

use Win32::OLE;


my $interval = 1;
my $key = 'Name';
my @properties = qw(PercentIdleTime PercentProcessorTime PercentPrivilegedTime PercentUserTime PercentInterruptTime TimeStamp_Sys100NS);

my $hash1 = {};

my $wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2")
or die "Failed to get object\n";


my $list = $wmi->InstancesOf('Win32_PerfRawData_PerfOS_Processor')
or die "Failed to get instance object\n";

my $v;
foreach $v (in $list) {
map{$hash1->{$v->{$key}}->{$_} = $v->{$_} }@properties;
}

while(sleep 1){

$list = $wmi->InstancesOf('Win32_PerfRawData_PerfOS_Processor')
or die "Failed to get instance object\n";

my $hash = {};
foreach $v (in $list) {
map{$hash->{$v->{$key}}->{$_} = $v->{$_} }@properties;
}

my $cpu_time = sprintf("%.2f", (1 - get_value($hash1->{'_Total'}, $hash->{'_Total'}, 'PercentProcessorTime' )) * 100);
my $cpu_idle = sprintf("%.2f", 100-$cpu_time);
my $cpu_user = sprintf("%.2f", get_value($hash1->{'_Total'}, $hash->{'_Total'}, 'PercentUserTime' )* 100);
my $cpu_priv = sprintf("%.2f", get_value($hash1->{'_Total'}, $hash->{'_Total'}, 'PercentPrivilegedTime' )* 100);
my $cpu_int = sprintf("%.2f", get_value($hash1->{'_Total'}, $hash->{'_Total'}, 'PercentInterruptTime' )* 100);
printf "CPU Time %s %% , privileged %s %% , user %s %%, interrupt %s %%\n", $cpu_time,$cpu_priv,$cpu_user,$cpu_int;

$hash1 = $hash;
}


exit;

sub get_value {
my $h1 = shift;
my $h2 = shift;
my $property = shift;
return (($h2->{$property} - $h1->{$property})/($h2->{'TimeStamp_Sys100NS'}-$h1->{'TimeStamp_Sys100NS'}));
}




Output sample:


CPU Time 2.03 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 1.87 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 2.16 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 1.76 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 2.19 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 1.77 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 1.98 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 1.93 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 2.08 % , privileged 1.96 % , user 0.00 %, interrupt 0.00 %
CPU Time 2.84 % , privileged 2.94 % , user 0.00 %, interrupt 0.00 %

Wednesday, September 8, 2010

Diff between two files (unordered and with different number of lines)

This program will find the differences between two files, and should be applied in the following situations:
  • The files have different number of lines
  • The files are unordered.
  • `diff` fails to get the differences
  • Large files (the reason to use shell `sort` function)
  • Single record per line
For example: Imagine you have two files with a list of users, and these listings were taken from a database at different time instants. These listings will be different because users have been added and removed from the database. This script will help you to find the differences when `diff` can't resolve it.

#!/usr/bin/perl -w

use strict;

my $file1= $ARGV[0];
my $file2= $ARGV[1];

unless($file1 and $file2){
print "Usage: $0 <file1> <file2>\n\n";
exit;
}
unless( -f $file1){
print "File 1 does not exist: [$file1]\n\n";
exit;
}
unless( -f $file2){
print "File 2 does not exist: [$file2]\n\n";
exit;
}


my $tmp_file1 = '/tmp/f1.tmp';
my $tmp_file2 = '/tmp/f2.tmp';

`sort $file1 > $tmp_file1`;
`sort $file2 > $tmp_file2`;

open(F1, $tmp_file1) or die "$!";
open(F2, $tmp_file2) or die "$!";

my $read_f1 = 1;
my $read_f2 = 1;

my $s1;
my $s2;
while(1){

if (eof(F1)){print ">>$_" while <F2>;}
if(eof(F2)){print "<<$_" while <F1>;}

if($read_f1){$s1 = <F1>;}
if($read_f2){$s2 = <F2>;}

last unless $s1 and $s2;

$read_f1 = 1;
$read_f2 = 1;

next if ( lc($s1) eq lc($s2) );

if(lc($s1) gt lc($s2)){
print ">$s2";
$read_f1 = 0;
}else{
print "<$s1";
$read_f2 = 0;
}
}

unlink $tmp_file1 or die "$!";
unlink $tmp_file2 or die "$!";



Wednesday, May 12, 2010

How to kill TCP client connections

Several times we are faced with a large number of TCP connections from the same source IP.
How to kill that connections?

This is an easy way:

$ for i in `lsof -i tcp@client_ip_address| awk '{print $2}'`; do kill -9 $i; done


See also : tcpkill

Monday, November 2, 2009

Solaris: analyze a core dump file

Whenever there is a coredump, the task of uncovering the source of the problem may not be easy. In Solaris we have some tools that will give us clues in order to solve the problem.

Coredump files on Solaris are generated in the directory:
/var/core/

Once in that directory, find your file core. Then run the following commands:
$ pstack 
core 'core_host_progname_1001_1_1256228346_13885' of 13885: /opt/coolstack/bin/perl -w /export/home/user/bin/progname
-----------------  lwp# 1 / thread# 1  --------------------
 fede5d77 lwp_yield (fed32a00) + 7
 feddfa66 mutex_unlock (fee5c5d0) + 133
 fed846ce malloc   (15, 8be8ed0, 8dcf888, 80cf342) + 3e
 080c20ac Perl_safesysmalloc () + 2c
-----------------  lwp# 2 / thread# 2  --------------------
 fede5ceb __lwp_park (87a93c8, 87a9398, 0) + b
 fede04f0 cond_wait_queue (87a93c8, 87a9398, 0) + 5e
 fede09c4 _cond_wait (87a93c8, 87a9398) + 64
 fede0a06 cond_wait (87a93c8, 87a9398) + 21
 fede0a3f pthread_cond_wait (87a93c8, 87a9398, 1, fe7a8108) + 1b
 fe7a82a8 XS_threads__shared_cond_wait (0, 0, 0, 0, 0, 0) + 1ac
 4d580000 ???????? ()
-----------------  lwp# 3 / thread# 3  --------------------
 feabf697 add_collation (fe71bc28, feaf49b7, 1a, feaf49b7) + 54b
 feac939b cs_leave (fe71bca0, fe71bd28, 1a, 0, fe71b0c0, fefd261b) + 83
 feada60d my_xml_leave (0, fe71b160, fe71b178, feada92b) + 141
 feadac9a my_xml_parse (fe71bca0, 8dee0e8, 4755, 0, 0, 0) + 662
 feac9ae7 my_parse_charset_xml (8dee0e8, 4755, feabf14c, 0, feffdd28, 4755) + 77
 feabf7d5 my_read_charset_file (fe71bf88, fe71bf00, fe71bf50, feffb818, fe71bf14, fefce6f6) + dd
 feac01f2 get_charset_by_csname (8dd0a08, 20, 10, 0) + 16a
 feadcbab mysql_init_character_set (8b00598, 8d01a8b, c, 2) + 5f
 feaddcb5 mysql_real_connect (8b00598, 8af44a8, 89ab6f8, 89b28a0, 8afa190, cea) + 6cd
 fec24031 mysql_dr_connect () + 1add
-----------------  lwp# 4 / thread# 4  --------------------
 feabf697 add_collation (fe65cc28, feaf49b7, 1a, feaf49b7) + 54b
 feac939b cs_leave (fe65cca0, fe65cd28, 1a, 0, feaaed64, feffb104) + 83
 feada60d my_xml_leave (0, fe65c160, fe65c178, feada92b) + 141
 feadac9a my_xml_parse (fe65cca0, 8df2848, 4755, 0, 0, 0) + 662
 feac9ae7 my_parse_charset_xml (8df2848, 4755, feabf14c, 0, fee5c5d0, 4755) + 77
 feabf7d5 my_read_charset_file (0, 0, 0, 0, 0, 0) + dd
 feac01f2 get_charset_by_csname (8dd0a18, 20, 10, 0) + 16a
 feadcbab mysql_init_character_set (8bfd4c0, 8d03a9b, c, 2) + 5f
 feaddcb5 mysql_real_connect (8bfd4c0, 8bf1bf8, 8bf2380, 8bf23c8, 8bf2440, cea) + 6cd
 fec24031 mysql_dr_connect () + 1add

The command above shows the last state of the stack for each thread (lwp) of the program.
That isn't enough we need to know in each thread the problem have occurred. There is another command that lets us know that, the "pflags" command.
$ pflags 
core 'core_host_progname_1001_1_1256228346_13885' of 13885: /opt/coolstack/bin/perl -w /export/home/user/bin/progname
        data model = _ILP32  flags = MSACCT|MSFORK
 /1:    flags = STOPPED  yield()
        why = PR_SUSPENDED
        sigmask = 0xffbff8f7,0x0000fff7
 /2:    flags = STOPPED  lwp_park(0x0,0x0,0x0)
        why = PR_SUSPENDED
 /3:    flags = STOPPED
        why = PR_SUSPENDED
        lwppend = 0x00000400,0x00000000
 /4:    flags = 0
        sigmask = 0xffffbefc,0x0000ffff  cursig = SIGSEGV

Now we know know that thread 4 have received the SIGNAL "SIGSEGV" (Segmentation Fault).
So, the SIGSEGV was generated in Thread 4 when using a MySQL function "add_collation".

With that information isn't easy to know the reason of the coredump. Lets use another command, the "pmap".
The "pmap" command display information about the address space of a process, in this case the program that causes the coredump.
$ pmap | less
core 'core_host_progname_1001_1_1256228346_13885' of 13885: /opt/coolstack/bin/perl -w /export/home/user/bin/progname
08042000      24K rw---    [ stack ]
08050000    1128K r-x--  /opt/coolstack/bin/perl
08179000      52K rwx--  /opt/coolstack/bin/perl
08186000   12752K rwx--    [ heap ]
FE65A000      20K rw---    [ stack tid=4 ]
FE660000      12K r-x--  /lib/nss_dns.so.1
FE673000       4K rw---  /lib/nss_dns.so.1
FE680000      24K r-x--  /lib/nss_files.so.1
FE696000       4K rw---  /lib/nss_files.so.1
FE6A0000      64K rwx--
FE6C0000      64K rwx--
FE719000      20K rw---    [ stack tid=3 ]
FE75E000       4K rw---    [ stack tid=2 ]
FE760000      64K rw---
FE780000      64K rw---
FE7A0000      44K r-x--  /opt/coolstack/lib/perl5/5.8.8/i86pc-solaris-thread-multi/auto/threads/shared/shared.so
FE7BA000       8K rwx--  /opt/coolstack/lib/perl5/5.8.8/i86pc-solaris-thread-multi/auto/threads/shared/shared.so
FE7C0000       8K r-x--  /opt/coolstack/lib/perl5/5.8.8/i86pc-solaris-thread-multi/auto/Cwd/Cwd.so
FE7D1000       4K rwx--  /opt/coolstack/lib/perl5/5.8.8/i86pc-solaris-thread-multi/auto/Cwd/Cwd.so
FE7E0000      64K rwx--
FE800000      40K r-x--  /usr/sfw/lib/libcrypto_extra.so.0.9.7
FE81A000       4K rw---  /usr/sfw/lib/libcrypto_extra.so.0.9.7
FE820000      36K r-x--  /usr/sfw/lib/libssl_extra.so.0.9.7
FE839000       4K rw---  /usr/sfw/lib/libssl_extra.so.0.9.7
FE840000      56K r-x--  /lib/libmd.so.1
FE85E000       4K rw---  /lib/libmd.so.1
FE860000      28K r-x--  /lib/libaio.so.1
FE877000       4K rw---  /lib/libaio.so.1
FE878000       4K rw---  /lib/libaio.so.1
FE880000     936K r-x--  /usr/sfw/lib/libcrypto.so.0.9.7
FE97A000      84K rw---  /usr/sfw/lib/libcrypto.so.0.9.7
FE98F000       8K rw---  /usr/sfw/lib/libcrypto.so.0.9.7
FE9A0000     200K r-x--  /usr/sfw/lib/libssl.so.0.9.7
FE9E2000      12K rw---  /usr/sfw/lib/libssl.so.0.9.7
FE9F0000      24K r-x--  /lib/libgen.so.1
FEA06000       4K rw---  /lib/libgen.so.1
FEA10000     216K r-x--  /lib/libresolv.so.2
FEA56000       8K rw---  /lib/libresolv.so.2
FEA60000      24K r-x--  /lib/librt.so.1
FEA76000       4K rw---  /lib/librt.so.1
FE878000       4K rw---  /lib/libaio.so.1
FE880000     936K r-x--  /usr/sfw/lib/libcrypto.so.0.9.7
FE97A000      84K rw---  /usr/sfw/lib/libcrypto.so.0.9.7
FE98F000       8K rw---  /usr/sfw/lib/libcrypto.so.0.9.7
FE9A0000     200K r-x--  /usr/sfw/lib/libssl.so.0.9.7
FE9E2000      12K rw---  /usr/sfw/lib/libssl.so.0.9.7
FE9F0000      24K r-x--  /lib/libgen.so.1
FEA06000       4K rw---  /lib/libgen.so.1
FEA10000     216K r-x--  /lib/libresolv.so.2
FEA56000       8K rw---  /lib/libresolv.so.2
FEA60000      24K r-x--  /lib/librt.so.1
FEA76000       4K rw---  /lib/librt.so.1
FEA80000      60K r-x--  /usr/lib/libz.so.1
FEA9E000       4K rwx--  /usr/lib/libz.so.1
FEAA0000     352K r-x--  /opt/coolstack/mysql_32bit/lib/mysql/libmysqlclient.so.16.0.0
FEB00000       4K rwx--
FEB07000    1040K rwx--  /opt/coolstack/mysql_32bit/lib/mysql/libmysqlclient.so.16.0.0
FEC0B000       4K rwx--  /opt/coolstack/mysql_32bit/lib/mysql/libmysqlclient.so.16.0.0

Since the problem occurred in the invocation of a MySQL function, with the "pmap" command, I've detected that my Perl used a DBD:: mysql library which was linked with a 32-bit MySQL library. My OS is 64 bits, however that is not a problem since 32 Bits libmysqlclient_32_bits is compatible with 64 bits MySQL DB's.
Despite all efforts to detect the problem in the program that I was developing, it was found that the problem was in the instance of a 64 bits MySQL DB that was using 32 bits libraries.