Saturday, February 18, 2012

Visualising COE (Certificate of Entitlement)

I managed to web scrap all the COE (Certificate of Entitlement) from LTA site at http://www.lta.gov.sg/content/lta/en/corporate/corp_info/index_corp_press.html.
With gnuplot's multiplot feature, I am able to combine the COE quota premium ($) and quota bidding in a single plot. Also, I am able to put Categories A, B and E together in an animated GIF file. Here is the animation:









It is clear that there are far less COE quota given for the past 2 years and therefore the COE price keeps going up. Anyway my objective is on visualisation rather than trying to analyse COE trend. Here is my gnuplot code:
set terminal png size 800,480 font "Arial,10"
set xdata time
set timefmt "%Y-%m-%d"
set format y '%6.0f'
set xrange [:"2012-08-01"]
set xtic offset 4


# -- 1st plot
set output 'coe-a.png'
set multiplot
set title "Singapore C.O.E. (Category A <1600cc), last update 2012-07-19"
set ylabel "Quota Premium ($)"
set size 1,0.65
set origin 0,0.35
set bmargin 0
unset key
set format x ''
set grid
set yrange [0:100000]
set ytics 10000
plot 'coe.txt' using 1:3 with lines

unset title
set key right box
set bmargin
set size 1,0.3
set origin 0,0
set tmargin 0
set ylabel 'Quota'
set format x '%Y'
set yrange [0:6000]
set ytics 1000
plot \
'coe.txt' using 1:4 with filledcurves x1 linetype 1 title 'Total Bids', \
'coe.txt' using 1:2 with filledcurves x1 linetype 2 title 'Quota', \
'coe.txt' using 1:4 with lines linetype -1 notitle, \
'coe.txt' using 1:2 with lines linetype -1 notitle


reset 

unset multiplot
set output 'coe-b.png'
set xdata time
set timefmt "%Y-%m-%d"
set format y '%6.0f'
set xrange [:"2012-08-01"]
set xtic offset 4

# -- 2nd plot
set multiplot
set title "Singapore C.O.E. (Category B >1600cc), last update 2012-07-19"
set ylabel "Quota Premium ($)"
set size 1,0.65
set origin 0,0.35
set bmargin 0
unset key
set format x ''
set grid
set yrange [0:100000]
set ytics 10000
plot 'coe.txt' using 1:8 with lines


unset title
set key right box
set bmargin
set size 1,0.3
set origin 0,0
set tmargin 0
set ylabel 'Quota'
set format x '%Y'
set yrange [0:6000]
set ytics 1000
plot \
'coe.txt' using 1:9 with filledcurves x1 linetype 1 title 'Total Bids', \
'coe.txt' using 1:7 with filledcurves x1 linetype 2 title 'Quota', \
'coe.txt' using 1:9 with lines linetype -1 notitle, \
'coe.txt' using 1:7 with lines linetype -1 notitle


reset

unset multiplot
set xdata time
set output 'coe-e.png'
set timefmt "%Y-%m-%d"
set format y '%6.0f'
set xrange [:"2012-08-01"]
set xtic offset 4

# -- 3rd plot
set multiplot
set title "Singapore C.O.E. (Category E -  Open), last update 2012-07-19"
set ylabel "Quota Premium ($)"
set size 1,0.65
set origin 0,0.35
set bmargin 0
unset key
set format x ''
set grid
set yrange [0:100000]
set ytics 10000
plot 'coe.txt' using 1:23 with lines


unset title
set key right box
set bmargin
set size 1,0.3
set origin 0,0
set tmargin 0
set ylabel 'Quota'
set format x '%Y'
set yrange [0:6000]
set ytics 1000
plot \
'coe.txt' using 1:24 with filledcurves x1 linetype 1 title 'Total Bids', \
'coe.txt' using 1:22 with filledcurves x1 linetype 2 title 'Quota', \
'coe.txt' using 1:24 with lines linetype -1 notitle, \
'coe.txt' using 1:22 with lines linetype -1 notitle
Interested in the data ? It is in the source. :-)
FYI, I will update the graph regularly to reflect the COE trend

Labels:

Wednesday, February 15, 2012

Visualising Age Distribution of Motor Vehicles

Today's newspaper has an ad on our Government web sites, and one of them caught my attention. That's data.gov.sg. The data from LTA on Age Distribution of Motor Vehicles is the one that I want to visualise using gnuplot

The animated image clearly showed that more new cars were introduced in 2006. After that the new car business gradually dropped to a record low in 2011. This definitely tally with the COE (Certificate of Entitlement) price and quota.

I am still trying to get hold of the COE data so that I can combine all these information in a single plot. It would be really nice if the data from data.gov.sg is given in its raw format.

Here is my gnuplot code to generate the animated gif file

set terminal gif size 800,480 animate delay 100
set output 'car.gif'
set auto x
set yrange [0:120000]
set style data histogram
set style histogram cluster gap 3
set style fill solid border -1
set boxwidth 0.9
set xtic rotate by -90 scale 0 font ",8"
set key box
set grid
set title 'Age Distribution of Motor Vehicle - 2000'
plot '2000' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2001'
plot '2001' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2002'
plot '2002' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2003'
plot '2003' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2004'
plot '2004' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2005'
plot '2005' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2006'
plot '2006' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2007'
plot '2007' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2008'
plot '2008' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2009'
plot '2009' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2010'
plot '2010' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'
#
set title 'Age Distribution of Motor Vehicle - 2011'
plot '2011' using 2:xtic(1) title 'Cars', '' u 4 title 'Motorcycles', '' u 6 title 'Buses', '' u 8 title 'Goods & Other Vehicles'

Here is all the data

$ for i in 20*
do
echo ==$i==
cat $i
echo
done
==2000==
0-<1 58097 (14.8%) 11620 (8.9%) 1347 (11.0%) 22706 (18.2%)
1-<2 38441 (9.8%) 9887 (7.5%) 887 (7.2%) 11211 (9.0%)
2-<3 27856 (7.1%) 8916 (6.8%) 807 (6.6%) 9293 (7.4%)
3-<4 24160 (6.1%) 7210 (5.5%) 1019 (8.3%) 8459 (6.8%)
4-<5 28211 (7.2%) 6581 (5.0%) 731 (5.9%) 7725 (6.2%)
5-<6 29790 (7.6%) 6044 (4.6%) 1164 (9.5%) 7539 (6.0%)
6-<7 29543 (7.5%) 9011 (6.9%) 978 (8.0%) 6149 (4.9%)
7-<8 38186 (9.7%) 6777 (5.2%) 949 (7.7%) 7342 (5.9%)
8-<9 28030 (7.1%) 5083 (3.9%) 875 (7.1%) 7460 (6.0%)
9-<10 25588 (6.5%) 3601 (2.7%) 831 (6.8%) 6549 (5.2%)
10-<11 8378 (2.1%) 2985 (2.3%) 768 (6.2%) 5927 (4.7%)
11-<12 8675 (2.2%) 4320 (3.3%) 603 (4.9%) 6617 (5.3%)
12-<13 2634 (0.7%) 3843 (2.9%) 395 (3.2%) 3477 (2.8%)
13-<14 2287 (0.6%) 3097 (2.4%) 329 (2.7%) 1601 (1.3%)
14-<15 2151 (0.5%) 2917 (2.2%) 180 (1.5%) 1116 (0.9%)
15-<16 1895 (0.5%) 4901 (3.7%) 156 (1.3%) 1652 (1.3%)
16-<17 4161 (1.1%) 8388 (6.4%) 84 (0.7%) 3035 (2.4%)
17-<18 6681 (1.7%) 8678 (6.6%) 78 (0.6%) 3247 (2.6%)
18-<19 9235 (2.4%) 7654 (5.8%) 82 (0.7%) 2520 (2.0%)
19-<20 3781 (1.0%) 4539 (3.5%) 37 (0.3%) 1020 (0.8%)
20- 15181 (3.9%) 4912 (3.8%) 0 (0.0%) 209 (0.2%)

==2001==
0-<1 67134 (16.6%) 13980 (10.7%) 861 (6.8%) 13895 (10.9%)
1-<2 58000 (14.3%) 11496 (8.8%) 1348 (10.7%) 22696 (17.8%)
2-<3 38210 (9.4%) 9752 (7.4%) 887 (7.0%) 11163 (8.8%)
3-<4 27614 (6.8%) 8757 (6.7%) 807 (6.4%) 9259 (7.3%)
4-<5 19420 (4.8%) 6908 (5.3%) 1015 (8.0%) 8044 (6.3%)
5-<6 25157 (6.2%) 6078 (4.6%) 728 (5.8%) 7519 (5.9%)
6-<7 25574 (6.3%) 5357 (4.1%) 1157 (9.2%) 7291 (5.7%)
7-<8 23843 (5.9%) 8348 (6.4%) 967 (7.7%) 5893 (4.6%)
8-<9 34102 (8.4%) 6121 (4.7%) 938 (7.4%) 6975 (5.5%)
9-<10 24297 (6.0%) 4245 (3.2%) 848 (6.7%) 7061 (5.5%)
10-<11 14480 (3.6%) 2747 (2.1%) 751 (5.9%) 5203 (4.1%)
11-<12 8238 (2.0%) 2819 (2.2%) 727 (5.8%) 5109 (4.0%)
12-<13 8521 (2.1%) 4035 (3.1%) 571 (4.5%) 5598 (4.4%)
13-<14 2556 (0.6%) 3371 (2.6%) 379 (3.0%) 2769 (2.2%)
14-<15 2135 (0.5%) 2482 (1.9%) 296 (2.3%) 1097 (0.9%)
15-<16 1956 (0.5%) 2230 (1.7%) 54 (0.4%) 512 (0.4%)
16-<17 1791 (0.4%) 4271 (3.3%) 129 (1.0%) 1199 (0.9%)
17-<18 3916 (1.0%) 7339 (5.6%) 66 (0.5%) 2149 (1.7%)
18-<19 5694 (1.4%) 7355 (5.6%) 51 (0.4%) 2222 (1.7%)
19-<20 5450 (1.3%) 6085 (4.6%) 44 (0.3%) 1386 (1.1%)
20- 7266 (1.8%) 7134 (5.4%) 0 (0.0%) 233 (0.2%)

==2002==
0-<1 62935 (15.6%) 17078 (13.0%) 648 (5.1%) 10317 (8.2%)
1-<2 67066 (16.6%) 13870 (10.6%) 859 (6.8%) 13892 (11.0%)
2-<3 57110 (14.1%) 11322 (8.6%) 1346 (10.6%) 22611 (18.0%)
3-<4 36747 (9.1%) 9562 (7.3%) 885 (7.0%) 11023 (8.8%)
4-<5 26719 (6.6%) 8509 (6.5%) 804 (6.3%) 9197 (7.3%)
5-<6 13305 (3.3%) 6441 (4.9%) 996 (7.8%) 7363 (5.8%)
6-<7 20309 (5.0%) 5442 (4.1%) 718 (5.7%) 7249 (5.8%)
7-<8 18122 (4.5%) 4571 (3.5%) 1148 (9.0%) 6858 (5.4%)
8-<9 15608 (3.9%) 7356 (5.6%) 946 (7.4%) 5446 (4.3%)
9-<10 21537 (5.3%) 5129 (3.9%) 907 (7.1%) 6338 (5.0%)
10-<11 13222 (3.3%) 2984 (2.3%) 747 (5.9%) 4674 (3.7%)
11-<12 14418 (3.6%) 2573 (2.0%) 734 (5.8%) 4916 (3.9%)
12-<13 7913 (2.0%) 2595 (2.0%) 679 (5.3%) 4377 (3.5%)
13-<14 8230 (2.0%) 3639 (2.8%) 523 (4.1%) 4713 (3.7%)
14-<15 2411 (0.6%) 2735 (2.1%) 320 (2.5%) 2176 (1.7%)
15-<16 1821 (0.5%) 1639 (1.2%) 226 (1.8%) 449 (0.4%)
16-<17 1870 (0.5%) 1900 (1.4%) 51 (0.4%) 396 (0.3%)
17-<18 1654 (0.4%) 3586 (2.7%) 102 (0.8%) 906 (0.7%)
18-<19 3357 (0.8%) 5857 (4.5%) 47 (0.4%) 1533 (1.2%)
19-<20 3096 (0.8%) 5415 (4.1%) 21 (0.2%) 1260 (1.0%)
20- 6824 (1.7%) 9234 (7.0%) 0 (0.0%) 237 (0.2%)

==2003==
0-<1 81244 (20.0%) 14926 (11.1%) 699 (5.5%) 13742 (11.0%)
1-<2 62827 (15.5%) 17009 (12.6%) 648 (5.1%) 10273 (8.2%)
2-<3 66234 (16.3%) 13682 (10.2%) 857 (6.8%) 13770 (11.0%)
3-<4 47358 (11.7%) 11052 (8.2%) 1336 (10.6%) 22139 (17.7%)
4-<5 27250 (6.7%) 9291 (6.9%) 868 (6.9%) 10236 (8.2%)
5-<6 22390 (5.5%) 8189 (6.1%) 797 (6.3%) 8978 (7.2%)
6-<7 8327 (2.1%) 5861 (4.3%) 975 (7.7%) 6129 (4.9%)
7-<8 12810 (3.2%) 4811 (3.6%) 703 (5.6%) 6407 (5.1%)
8-<9 10545 (2.6%) 3871 (2.9%) 1072 (8.5%) 5971 (4.8%)
9-<10 6747 (1.7%) 6312 (4.7%) 923 (7.3%) 4746 (3.8%)
10-<11 4207 (1.0%) 4280 (3.2%) 842 (6.7%) 5261 (4.2%)
11-<12 13164 (3.2%) 2643 (2.0%) 728 (5.8%) 4001 (3.2%)
12-<13 14236 (3.5%) 2275 (1.7%) 707 (5.6%) 4266 (3.4%)
13-<14 6678 (1.6%) 2255 (1.7%) 612 (4.8%) 3321 (2.7%)
14-<15 6834 (1.7%) 3043 (2.3%) 442 (3.5%) 2937 (2.3%)
15-<16 1849 (0.5%) 1808 (1.3%) 256 (2.0%) 611 (0.5%)
16-<17 1638 (0.4%) 1336 (1.0%) 64 (0.5%) 330 (0.3%)
17-<18 1629 (0.4%) 1604 (1.2%) 31 (0.2%) 284 (0.2%)
18-<19 1157 (0.3%) 3039 (2.3%) 71 (0.6%) 620 (0.5%)
19-<20 1079 (0.3%) 4855 (3.6%) 22 (0.2%) 800 (0.6%)
20- 7125 (1.8%) 12625 (9.4%) 0 (0.0%) 201 (0.2%)

==2004==
0-<1 96670 (23.2%) 12046 (8.8%) 684 (5.3%) 14901 (11.8%)
1-<2 81164 (19.5%) 14855 (10.9%) 699 (5.4%) 13736 (10.8%)
2-<3 60289 (14.5%) 16825 (12.4%) 644 (5.0%) 9992 (7.9%)
3-<4 56374 (13.5%) 13448 (9.9%) 853 (6.6%) 13360 (10.5%)
4-<5 27860 (6.7%) 10732 (7.9%) 1316 (10.2%) 20725 (16.4%)
5-<6 13038 (3.1%) 8927 (6.6%) 846 (6.6%) 8897 (7.0%)
6-<7 15284 (3.7%) 7762 (5.7%) 784 (6.1%) 8686 (6.9%)
7-<8 4311 (1.0%) 5272 (3.9%) 947 (7.3%) 5003 (3.9%)
8-<9 6970 (1.7%) 4241 (3.1%) 691 (5.4%) 5662 (4.5%)
9-<10 4538 (1.1%) 3178 (2.3%) 1046 (8.1%) 5459 (4.3%)
10-<11 811 (0.2%) 4843 (3.6%) 890 (6.9%) 4190 (3.3%)
11-<12 4165 (1.0%) 3788 (2.8%) 829 (6.4%) 4969 (3.9%)
12-<13 12979 (3.1%) 2263 (1.7%) 704 (5.5%) 3305 (2.6%)
13-<14 13490 (3.2%) 1969 (1.4%) 688 (5.3%) 3505 (2.8%)
14-<15 4395 (1.1%) 1877 (1.4%) 567 (4.4%) 2256 (1.8%)
15-<16 3856 (0.9%) 2481 (1.8%) 373 (2.9%) 655 (0.5%)
16-<17 1565 (0.4%) 1509 (1.1%) 242 (1.9%) 433 (0.3%)
17-<18 1298 (0.3%) 1096 (0.8%) 26 (0.2%) 224 (0.2%)
18-<19 1248 (0.3%) 1340 (1.0%) 26 (0.2%) 200 (0.2%)
19-<20 753 (0.2%) 2589 (1.9%) 37 (0.3%) 378 (0.3%)
20- 6045 (1.4%) 15081 (11.1%) 0 (0.0%) 173 (0.1%)

==2005==
0-<1 109165 (24.9%) 12122 (8.7%) 776 (5.9%) 14138 (11.0%)
1-<2 96518 (22.0%) 11976 (8.6%) 684 (5.2%) 14898 (11.6%)
2-<3 78754 (18.0%) 14712 (10.6%) 699 (5.3%) 13689 (10.7%)
3-<4 46496 (10.6%) 16599 (12.0%) 639 (4.8%) 9394 (7.3%)
4-<5 34396 (7.8%) 13127 (9.5%) 840 (6.4%) 12379 (9.7%)
5-<6 10562 (2.4%) 10358 (7.5%) 1290 (9.8%) 18348 (14.3%)
6-<7 6644 (1.5%) 8500 (6.1%) 819 (6.2%) 7718 (6.0%)
7-<8 8462 (1.9%) 7326 (5.3%) 767 (5.8%) 8133 (6.3%)
8-<9 2284 (0.5%) 4794 (3.5%) 922 (7.0%) 4245 (3.3%)
9-<10 3250 (0.7%) 3661 (2.6%) 674 (5.1%) 5160 (4.0%)
10-<11 644 (0.1%) 2218 (1.6%) 1006 (7.6%) 4944 (3.9%)
11-<12 787 (0.2%) 4241 (3.1%) 879 (6.6%) 4035 (3.1%)
12-<13 4003 (0.9%) 3271 (2.4%) 812 (6.1%) 4514 (3.5%)
13-<14 12431 (2.8%) 1903 (1.4%) 666 (5.0%) 2595 (2.0%)
14-<15 11822 (2.7%) 1661 (1.2%) 664 (5.0%) 2437 (1.9%)
15-<16 1823 (0.4%) 1483 (1.1%) 504 (3.8%) 690 (0.5%)
16-<17 2859 (0.7%) 2214 (1.6%) 342 (2.6%) 454 (0.4%)
17-<18 1113 (0.3%) 1299 (0.9%) 211 (1.6%) 194 (0.2%)
18-<19 879 (0.2%) 945 (0.7%) 16 (0.1%) 52 (0.0%)
19-<20 844 (0.2%) 1154 (0.8%) 10 (0.1%) 42 (0.0%)
20- 4458 (1.0%) 15024 (10.8%) 0 (0.0%) 134 (0.1%)

==2006==
0-<1 116741 (24.7%) 11456 (8.1%) 985 (7.1%) 13358 (10.1%)
1-<2 109075 (23.1%) 12047 (8.5%) 778 (5.6%) 14133 (10.6%)
2-<3 93240 (19.7%) 11848 (8.4%) 686 (5.0%) 14908 (11.2%)
3-<4 63124 (13.4%) 14511 (10.2%) 701 (5.1%) 13655 (10.3%)
4-<5 26056 (5.5%) 16349 (11.5%) 629 (4.5%) 8451 (6.4%)
5-<6 15655 (3.3%) 12805 (9.0%) 816 (5.9%) 11285 (8.5%)
6-<7 5823 (1.2%) 9934 (7.0%) 1249 (9.0%) 17076 (12.9%)
7-<8 3398 (0.7%) 8052 (5.7%) 789 (5.7%) 6918 (5.2%)
8-<9 4456 (0.9%) 6862 (4.8%) 748 (5.4%) 7732 (5.8%)
9-<10 1174 (0.2%) 4366 (3.1%) 901 (6.5%) 3830 (2.9%)
10-<11 1131 (0.2%) 2953 (2.1%) 640 (4.6%) 4625 (3.5%)
11-<12 634 (0.1%) 1975 (1.4%) 1004 (7.3%) 4806 (3.6%)
12-<13 746 (0.2%) 3696 (2.6%) 865 (6.3%) 3800 (2.9%)
13-<14 3617 (0.8%) 2849 (2.0%) 784 (5.7%) 3969 (3.0%)
14-<15 10967 (2.3%) 1587 (1.1%) 639 (4.6%) 1885 (1.4%)
15-<16 8972 (1.9%) 1318 (0.9%) 625 (4.5%) 1219 (0.9%)
16-<17 1295 (0.3%) 1344 (0.9%) 483 (3.5%) 556 (0.4%)
17-<18 1914 (0.4%) 1985 (1.4%) 312 (2.3%) 343 (0.3%)
18-<19 697 (0.1%) 1135 (0.8%) 193 (1.4%) 134 (0.1%)
19-<20 554 (0.1%) 825 (0.6%) 4 (0.0%) 30 (0.0%)
20- 3039 (0.6%) 13984 (9.9%) 0 (0.0%) 128 (0.1%)

==2007==
0<1 106502 (20.7%) 10343 (7.2%) 775 (5.5%) 10652 (7.7%)
1<2 116656 (22.7%) 11338 (7.9%) 981 (6.9%) 13378 (9.7%)
2<3 108606 (21.1%) 11897 (8.3%) 777 (5.5%) 14204 (10.2%)
3<4 81376 (15.8%) 11704 (8.2%) 687 (4.8%) 14926 (10.8%)
4<5 42069 (8.2%) 14297 (10.0%) 695 (4.9%) 13583 (9.8%)
5<6 12678 (2.5%) 16098 (11.2%) 611 (4.3%) 7833 (5.7%)
6<7 10607 (2.1%) 12396 (8.6%) 798 (5.6%) 10740 (7.7%)
7<8 3638 (0.7%) 9397 (6.5%) 1225 (8.6%) 16386 (11.8%)
8<9 2024 (0.4%) 7418 (5.2%) 768 (5.4%) 6677 (4.8%)
9<10 2288 (0.4%) 6068 (4.2%) 729 (5.1%) 7647 (5.5%)
10-<11 502 (0.1%) 3405 (2.4%) 885 (6.2%) 3724 (2.7%)
11-<12 1125 (0.2%) 2725 (1.9%) 630 (4.4%) 4516 (3.3%)
12-<13 621 (0.1%) 1732 (1.2%) 999 (7.0%) 4632 (3.3%)
13-<14 698 (0.1%) 3142 (2.2%) 856 (6.0%) 3499 (2.5%)
14-<15 3223 (0.6%) 2475 (1.7%) 761 (5.4%) 3341 (2.4%)
15-<16 9311 (1.8%) 1249 (0.9%) 604 (4.3%) 907 (0.7%)
16-<17 6982 (1.4%) 1209 (0.8%) 615 (4.3%) 1021 (0.7%)
17-<18 980 (0.2%) 1224 (0.9%) 458 (3.2%) 463 (0.3%)
18-<19 1380 (0.3%) 1760 (1.2%) 281 (2.0%) 264 (0.2%)
19-<20 445 (0.1%) 992 (0.7%) 57 (0.4%) 94 (0.1%)
20- 2974 (0.6%) 12613 (8.8%) 0 (0.0%) 117 (0.1%)

==2008==
0<1 96945 (17.6%) 10336 (7.1%) 1506 (10.1%) 8630 (6.0%)
1<2 106440 (19.3%) 10212 (7.0%) 778 (5.2%) 10640 (7.4%)
2<3 116471 (21.2%) 11162 (7.7%) 980 (6.5%) 13364 (9.3%)
3<4 102520 (18.6%) 11740 (8.1%) 775 (5.2%) 14192 (9.9%)
4<5 60442 (11.0%) 11508 (7.9%) 686 (4.6%) 14910 (10.4%)
5<6 23981 (4.4%) 14094 (9.7%) 695 (4.6%) 13510 (9.4%)
6<7 8570 (1.6%) 15795 (10.9%) 598 (4.0%) 7393 (5.2%)
7<8 7668 (1.4%) 11928 (8.2%) 783 (5.2%) 10315 (7.2%)
8<9 2474 (0.4%) 8761 (6.0%) 1198 (8.0%) 15908 (11.1%)
9<10 1131 (0.2%) 6491 (4.5%) 747 (5.0%) 6446 (4.5%)
10-<11 594 (0.1%) 4381 (3.0%) 691 (4.6%) 7226 (5.1%)
11-<12 498 (0.1%) 3203 (2.2%) 874 (5.8%) 3711 (2.6%)
12-<13 1113 (0.2%) 2513 (1.7%) 627 (4.2%) 4428 (3.1%)
13-<14 604 (0.1%) 1561 (1.1%) 993 (6.6%) 4494 (3.1%)
14-<15 649 (0.1%) 2737 (1.9%) 837 (5.6%) 3155 (2.2%)
15-<16 2698 (0.5%) 2156 (1.5%) 718 (4.8%) 2303 (1.6%)
16-<17 7810 (1.4%) 1121 (0.8%) 589 (3.9%) 813 (0.6%)
17-<18 5353 (1.0%) 1094 (0.8%) 443 (3.0%) 872 (0.6%)
18-<19 746 (0.1%) 1039 (0.7%) 378 (2.5%) 390 (0.3%)
19-<20 851 (0.2%) 1257 (0.9%) 80 (0.5%) 153 (0.1%)
20- 2897 (0.5%) 12199 (8.4%) 0 (0.0%) 113 (0.1%)

==2009==
0-<1 68464 (11.9%) 8827 (6.0%) 1376 (8.8%) 5552 (3.8%)
1-<2 96927 (16.8%) 10248 (7.0%) 1505 (9.6%) 8624 (6.0%)
2-<3 106281 (18.4%) 10076 (6.9%) 778 (5.0%) 10631 (7.3%)
3-<4 116043 (20.1%) 10982 (7.5%) 978 (6.2%) 13352 (9.2%)
4-<5 93610 (16.2%) 11540 (7.9%) 773 (4.9%) 14172 (9.8%)
5-<6 44002 (7.6%) 11323 (7.7%) 681 (4.3%) 14865 (10.3%)
6-<7 17511 (3.0%) 13800 (9.4%) 687 (4.4%) 13337 (9.2%)
7-<8 5936 (1.0%) 15437 (10.5%) 575 (3.7%) 7057 (4.9%)
8-<9 5465 (0.9%) 11277 (7.7%) 759 (4.8%) 9947 (6.9%)
9-<10 1574 (0.3%) 7901 (5.4%) 1165 (7.4%) 15447 (10.7%)
10-<11 505 (0.1%) 4889 (3.3%) 704 (4.5%) 6136 (4.2%)
11-<12 586 (0.1%) 4118 (2.8%) 684 (4.4%) 7140 (4.9%)
12-<13 488 (0.1%) 2967 (2.0%) 870 (5.6%) 3678 (2.5%)
13-<14 1096 (0.2%) 2329 (1.6%) 618 (3.9%) 4263 (2.9%)
14-<15 577 (0.1%) 1365 (0.9%) 979 (6.3%) 4187 (2.9%)
15-<16 550 (0.1%) 2313 (1.6%) 812 (5.2%) 2548 (1.8%)
16-<17 2377 (0.4%) 1919 (1.3%) 683 (4.4%) 2121 (1.5%)
17-<18 6618 (1.1%) 1021 (0.7%) 515 (3.3%) 686 (0.5%)
18-<19 4297 (0.7%) 985 (0.7%) 372 (2.4%) 658 (0.5%)
19-<20 661 (0.1%) 842 (0.6%) 145 (0.9%) 290 (0.2%)
20- 3420 (0.6%) 12178 (8.3%) 0 (0.0%) 111 (0.1%)

==2010==
0-<1 41407 (7.0%) 8228 (5.6%) 1088 (6.8%) 3905 (2.7%)
1-<2 68503 (11.5%) 8744 (5.9%) 1376 (8.6%) 5547 (3.9%)
2-<3 96887 (16.3%) 10139 (6.9%) 1509 (9.5%) 8623 (6.0%)
3-<4 105917 (17.8%) 9973 (6.8%) 781 (4.9%) 10633 (7.4%)
4-<5 115583 (19.4%) 10846 (7.4%) 976 (6.1%) 13331 (9.3%)
5-<6 88437 (14.9%) 11317 (7.7%) 773 (4.9%) 14154 (9.9%)
6-<7 37564 (6.3%) 11092 (7.5%) 678 (4.3%) 14838 (10.3%)
7-<8 14014 (2.4%) 13529 (9.2%) 673 (4.2%) 13238 (9.2%)
8-<9 4713 (0.8%) 14983 (10.2%) 559 (3.5%) 6879 (4.8%)
9-<10 3790 (0.6%) 10391 (7.1%) 730 (4.6%) 9380 (6.5%)
10-<11 558 (0.1%) 5958 (4.0%) 1063 (6.7%) 12946 (9.0%)
11-<12 501 (0.1%) 4648 (3.2%) 688 (4.3%) 6116 (4.3%)
12-<13 581 (0.1%) 3875 (2.6%) 680 (4.3%) 7105 (4.9%)
13-<14 475 (0.1%) 2779 (1.9%) 862 (5.4%) 3662 (2.5%)
14-<15 1082 (0.2%) 2140 (1.5%) 604 (3.8%) 4068 (2.8%)
15-<16 526 (0.1%) 1174 (0.8%) 959 (6.0%) 3678 (2.6%)
16-<17 533 (0.1%) 2068 (1.4%) 790 (5.0%) 2430 (1.7%)
17-<18 2018 (0.3%) 1706 (1.2%) 618 (3.9%) 1973 (1.4%)
18-<19 5121 (0.9%) 920 (0.6%) 436 (2.7%) 563 (0.4%)
19-<20 3217 (0.5%) 851 (0.6%) 93 (0.6%) 437 (0.3%)
20- 3758 (0.6%) 11921 (8.1%) 0 (0.0%) 107 (0.1%)

==2011==
0-<1 27748 (4.6%) 7991 (5.5%) 1502 (9.0%) 5175 (3.6%)
1-<2 41426 (6.9%) 8175 (5.6%) 1089 (6.5%) 3903 (2.7%)
2-<3 68512 (11.3%) 8677 (6.0%) 1376 (8.3%) 5545 (3.8%)
3-<4 96877 (16.0%) 10037 (6.9%) 1509 (9.1%) 8616 (5.9%)
4-<5 105783 (17.5%) 9880 (6.8%) 781 (4.7%) 10630 (7.3%)
5-<6 115335 (19.1%) 10714 (7.4%) 975 (5.9%) 13317 (9.2%)
6-<7 87554 (14.5%) 11118 (7.6%) 773 (4.6%) 14142 (9.7%)
7-<8 34178 (5.7%) 10837 (7.4%) 672 (4.0%) 14809 (10.2%)
8-<9 11710 (1.9%) 13134 (9.0%) 658 (4.0%) 13135 (9.0%)
9-<10 3376 (0.6%) 13623 (9.4%) 530 (3.2%) 6647 (4.6%)
10-<11 588 (0.1%) 6836 (4.7%) 670 (4.0%) 7895 (5.4%)
11-<12 558 (0.1%) 5701 (3.9%) 1053 (6.3%) 12857 (8.9%)
12-<13 501 (0.1%) 4404 (3.0%) 683 (4.1%) 6098 (4.2%)
13-<14 575 (0.1%) 3638 (2.5%) 671 (4.0%) 7072 (4.9%)
14-<15 464 (0.1%) 2577 (1.8%) 841 (5.1%) 3610 (2.5%)
15-<16 1016 (0.2%) 1913 (1.3%) 580 (3.5%) 3565 (2.5%)
16-<17 519 (0.1%) 1084 (0.7%) 941 (5.7%) 3604 (2.5%)
17-<18 512 (0.1%) 1831 (1.3%) 701 (4.2%) 2313 (1.6%)
18-<19 1094 (0.2%) 1499 (1.0%) 543 (3.3%) 1787 (1.2%)
19-<20 1149 (0.2%) 768 (0.5%) 104 (0.6%) 319 (0.2%)
20- 4248 (0.7%) 11243 (7.7%) 0 (0.0%) 119 (0.1%)

Labels:

Monday, July 28, 2008

An Old Task in Python

Finding a real problem to brush up my skill on Python is not an easy task. Instead of waiting for a new problem to come, I look for old problem that I still have the input data set. This also enables me to compare Python with my previous solution.

Couple of months ago, my colleague passed me IIS web access log from a rather busy web server. I managed to extract the session concurrency information and visualised the result using Gnuplot. This trick was derived some years ago when I was doing a performance testing project. Basically I extracted all the timestamps for individual session ID, assuming that the session IDs are unique. It is possible to 'stack up' all the sessions by increment the per second counter between the start and end of the session duration. The end result will be the session concurrency.

The input data is a 170MB web access log with 227K lines of log based on a single day web traffic. My previous solution was based on AWK script and the run time was 3min 6sec. With Python 2.5.2, the run time is 20 sec, almost 10 folds in performance gain.

Here is my python script:

#! /usr/bin/python

import datetime
import time
import sys


if len(sys.argv) != 2:
 print "Usage:", sys.argv[0], "<web-log>"
 exit(1)


# convert string to integer, leading zeros are stripped
# '00'->0, '08'->8, '11'->11
def str2int(s):
 t=s.lstrip('0')
 if t=='': t=0
 return int(t)


# determine epoch from web access log timestamp
# 2008-07-21 00:00:07 myserver 1.2.3.4 GET / .....
def findEpoch(line):
 yr=str2int(line[0:4])
 mth=str2int(line[5:7])
 day=str2int(line[8:10])
 hh=str2int(line[11:13])
 mm=str2int(line[14:16])
 ss=str2int(line[17:19])
 t=datetime.datetime(yr,mth,day,hh,mm,ss)
 epoch=int(time.mktime(t.timetuple()))
 return epoch


sessions={}
concurrency={}


# start time of log
# read first line (no need to determine first line in for loop)
fp=open(sys.argv[1],"r")
line=fp.readline()
starttime=findEpoch(line)
fp.close()


sess="PROD_JSESSION_UID"
sessN=len(sess)
for line in open(sys.argv[1],"r"):
  
 cookie=line.rstrip().split(' ')[12]
 if sess in cookie:

  epoch=findEpoch(line)


  # get session id
  i1=cookie.index(sess)
  try:
   i2=cookie.index(";",i1)
  except:
   i2=len(cookie)
  uid=cookie[i1+sessN+2:i2]


  # store sessions
  try:
   sessions[uid]="%s,%s" % (sessions[uid],str(epoch))
  except:
   sessions[uid]="%s" % str(epoch)

endtime=epoch


# initialise to zero
count=starttime
while count<=endtime:
 concurrency[count]=0
 count+=1


# add up concurrency for all sessions
for key in sessions.keys():
 ltime=sessions[key].split(',')
 t0=int(ltime[0])
 t1=int(ltime[-1])
 count=t0
 while count<=t1:
  concurrency[count]+=1
  count+=1


count=starttime
while count<=endtime:
 print count,concurrency[count]
 count+=1

The plot from Python looked the same as the AWK-based program:

Labels: , ,

Thursday, July 12, 2007

Network Statistics

"Netstat -s -P tcp" provides you with detail summary of the Tcp statistics. See below
$ netstat -s -P tcp

TCP     tcpRtoAlgorithm     =     4     tcpRtoMin           =   400
        tcpRtoMax           = 60000     tcpMaxConn          =    -1
        tcpActiveOpens      =   105     tcpPassiveOpens     =  6929
        tcpAttemptFails     =     9     tcpEstabResets      =    24
        tcpCurrEstab        =    10     tcpOutSegs          =14831657
        tcpOutDataSegs      =14504882   tcpOutDataBytes     =1579349845
        tcpRetransSegs      =   398     tcpRetransBytes     =116003
        tcpOutAck           =326541     tcpOutAckDelayed    =204020
        tcpOutUrg           =     0     tcpOutWinUpdate     =     0
        tcpOutWinProbe      =     0     tcpOutControl       = 14170
        tcpOutRsts          =    14     tcpOutFastRetrans   =    14
        tcpInSegs           =14736303
        tcpInAckSegs        =14290274   tcpInAckBytes       =1579351049
        tcpInDupAck         = 10033     tcpInAckUnsent      =     0
        tcpInInorderSegs    =14467627   tcpInInorderBytes   =681280146
        tcpInUnorderSegs    =    60     tcpInUnorderBytes   =  3120
        tcpInDupSegs        =    62     tcpInDupBytes       =  2184
        tcpInPartDupSegs    =     0     tcpInPartDupBytes   =     0
        tcpInPastWinSegs    =     0     tcpInPastWinBytes   =     0
        tcpInWinProbe       =     0     tcpInWinUpdate      =     0
        tcpInClosed         =     0     tcpRttNoUpdate      =   103
        tcpRttUpdate        =14283487   tcpTimRetrans       =   300
        tcpTimRetransDrop   =    12     tcpTimKeepalive     =  9353
        tcpTimKeepaliveProbe=  2967     tcpTimKeepaliveDrop =     0
        tcpListenDrop       =     0     tcpListenDropQ0     =     0
        tcpHalfOpenDrop     =     0     tcpOutSackRetrans   =    49

It is possible to timestamp each of these network metrics. If you collect them over a period of time, you can plot it with Gnuplot and study the trend of these TCP parameters.

This script should be able to do this job as a normal user and it will append timestamp and metrics to individual parameter files. So all you have to do is to loop through the script with a while loop. I will leave this to the reader as an exercise.

$ls
net.sh

$ cat net.sh
#! /bin/sh

timestamp=`date '+%Y%m%d%H%M%S'`


netstat -s -P tcp | \
sed -e 's/=/ /g' -e 's/TCP//' | \
nawk -v ts=$timestamp '
NF==2 {
        cmd=sprintf("echo %s %s >> %s",ts,$2,$1)
        system(cmd)
}
NF==4 {
        cmd=sprintf("echo %s %s >> %s",ts,$2,$1)
        system(cmd)
        cmd=sprintf("echo %s %s >> %s",ts,$4,$3)
        system(cmd)
}'

$ ls
net.sh                tcpInDupBytes         tcpInWinUpdate tcpOutSackRetrans     tcpRttNoUpdate
tcpActiveOpens        tcpInDupSegs          tcpListenDrop tcpOutSegs            tcpRttUpdate
tcpAttemptFails       tcpInInorderBytes     tcpListenDropQ0 tcpOutUrg             tcpTimKeepalive
tcpCurrEstab          tcpInInorderSegs      tcpMaxConn tcpOutWinProbe        tcpTimKeepaliveDrop
tcpEstabResets        tcpInPartDupBytes     tcpOutAck tcpOutWinUpdate       tcpTimKeepaliveProbe
tcpHalfOpenDrop       tcpInPartDupSegs      tcpOutAckDelayed tcpPassiveOpens       tcpTimRetrans
tcpInAckBytes         tcpInPastWinBytes     tcpOutControl tcpRetransBytes       tcpTimRetransDrop
tcpInAckSegs          tcpInPastWinSegs      tcpOutDataBytes tcpRetransSegs
tcpInAckUnsent        tcpInUnorderBytes     tcpOutDataSegs tcpRtoAlgorithm
tcpInClosed           tcpInUnorderSegs      tcpOutFastRetrans     tcpRtoMax
tcpInDupAck           tcpInWinProbe         tcpOutRsts            tcpRtoMin

$ more tcpActiveOpens
20070712081804 29

Labels: ,

Tuesday, May 15, 2007

Timestamp your monitoring output

Some of the system / application monitoring commands (eg, vmstat, mpstat, iostat, ...) allow user to continuously monitor based on certain time interval. However, they do not have option to 'timestamp' the sampled data.

I used to do that with Tcl with 'fileevent readable' to trap the output and prepend the 'clock format.

It is possible to do all these with nawk (You cannot use awk in Solaris because it does not support "system" execution).

Run this and have the result append to the log file

vmstat 1 | nawk '{system("date +%Y-%m-%dT%H:%M:%S|tr -d \"\n\"");printf(" = %s\n",$0)}' | tee -a vmstat.log
and you will get this
2007-05-11T13:46:01 =  kthr      memory            page            disk          faults      cpu
2007-05-11T13:46:01 =  r b w   swap  free  re  mf pi po fr de sr f0 m1 m1 m1   in   sy   cs us sy id
2007-05-11T13:46:01 =  0 0 0 474432 58664   0   2  2  1  1  0  0  0  0 0  0  418   51  115  0  1 99
2007-05-11T13:46:02 =  0 0 0 318976  9368  68 600 150 0  0  0  0  0  8 0  2  481 1050  273  3  9 88
2007-05-11T13:46:03 =  0 0 0 318976  9368  24 190 40  0  0  0  0  0  2 0  0  437  438  207  1  5 94
2007-05-11T13:46:04 =  0 0 0 318976  9368  25 188 31  0  0  0  0  0  2 0  1  427  408  222  2  4 94

With the timestamp, you are able to plot it with Gnuplot with x-axis configured as time scale. Suppose we want to plot the kernal threads of runable and blocked.

set title 'Kthr - r and b'
set ylabel 'No. of Kernel Threads'
set xdata time
set timefmt '%Y-%m-%dT%H:%M:%S'
plot 'vmstat.log' using 1:3 title 'kthr-r' with linespoints, \
                  using 1:3 title 'kthr-b' with linespoints

Happy timestamp-ing :-)

Labels: ,