Friday, September 30, 2011

Send email in linux

In new walk-through environment, there are some configuration issues need look at application logs to see what is going on. SSH to application server (tomcat) and vi the application log is the common way. Ops team can alsomail the log file to other developers to analyze, so need sendmail. Luckily there is "sendmail" in Linux.

Command Line:

  1. Check if sendmail is running => ps ax | grep sendmail
  2. Check if sendmail is running => /etc/init.d/sendmail status
  3. Check sendmail listening info (IP and port) => netstat -tnlp | grep sendmail
  4. Send email (please use man mail for user guide)
    1. mail -s "Email subject" example@example.com < /tmp/test.txt
    2. cat /tmp/test.txt | mail -s "Email subject" example@example.com
    3. echo "The message in email body" | mail -s "Email subject" example@example.com
    4. (df -h;free -m) | mail -s "Disk and Memory Info" example@example.com
    5. less /proc/cpuinfo | mail -s "CPU Info" example@example.com -c ccto@example.com
Shell script:
#!/bin/sbin
#df -h | mail -s "disk space" example@example.com
#!/bin/bash
df -h > /tmp/mail_report.log
free -m >> /tmp/mail_report.log
mail -s "disk and RAM report" example@example.com < /tmp/mail_report.log
rm -f /tmp/mail_report.log

Friday, September 23, 2011

jQuery 10 performance tips

This is a summary of 10 jQuery performance tips from http://addyosmani.com/jqprovenperformance/
  1. Use the latest jquery release
  2. Know your selectors (ID, element, class, pseudo & attribute)
  3. Use .find() -> $parent.find('.child').show() is the fastest than others (scoped selector)
  4. Don't use jQuery unless it's absolutely necessary -> this.id is fater than $(this).attr('id')
  5. Caching -> storing the result of a selection for later re-use, no repeat selection
  6. Chaining
  7. Event delegation -> if possible, use delegate instead of bind and live
  8. Each DOM insertion is costly -> keep the use of .append(), .insertBefore() and .insertAfter() to a minimum; .data() is better than .text() or .html(); $.data('#elem', key, value) is faster than $('elem').data(key, value)
  9. Avoid loops -> Javascript for or while loop is faster than jQuery .each()
  10. Avoid constructing new jQuery object unless ncessary -> use $.method() rather than $.fn.method(); $.text($text) is faster than $text.text(), $.data() is faster than $().data
Update (11/30/2011):
In recent jQuery performance discussion, I summarized the following points based on different messages.
  1. Scope jquery selectors (use find)
  2. Know selectors performance => ID > element (tag) > class > pseudo & attribute
  3. Avoid loops =>  if loop is inevitable, consider for/while > $.each()  if possible
  4. Cache the result of a selection for later use (using local variables if possible)
  5. DOM operation is expensive (esp. in a loop)
  6. Don't use $ unless it's necessary => this.name > $(this).attr('name’) 

    Thursday, September 22, 2011

    Fundamentals of WPO

    There are two fundamental factors (action items) for Web Performance Optimization (WPO), request number and data size. There are top rules and plenty of tools to assist these two optimization. This is common sense and very straightforward to understand the importance. One metaphor about moving in real life, if you can throw away what you don't necessarily need, and keep your belongs to minimum say 1 U-haul truck can hold, then you can make the move fast because you only need 1 round trip with minimum stuff.


    Reduce Http request - Send it as infrequently as possible
    Http round trip is expensive esp. with a long RTT, or for new http connection. Reducing http request can be achieved using the following:
    • Merge static resources (CSS, JS, Image)
    • Add Cache control (including ajax?)
    • Combine dynamic requests
    • Build Single page application (using Ajax)
    • Avoid redirect
    • Fewer DNS lookup
    Reduce download size - Send as little data as possible
    Small data size saves bandwidth (tcp package numbers). It can be achieved using below ways:
    • Gzip resources
    • Minify JS and CSS (Code optimization, obfuscation, duplication removal)
    • Crush images
    • Add Cache control (caching in browser)
    • HTML5 local storage, local cache
    For more info, refer to 14 Rules for Faster-Loading Web Sites