01 November 2014

Identify slow queries

for MySQL (Percona)

What’s happen if you found database queries are slow sometime. How to config your logs for mysql to identify the causes. It’s not easy, but some ways you can do first if there is no any other specific reason from resource shortage.

Check your database status if any slow queries are there.

$ mysqladmin -u root -p status
Enter password:
Uptime: 126005  Threads: 1  Questions: 109  Slow queries: 0  Opens: 327  Flush tables: 1  Open tables: 80  Queries per second avg: 0.000

or for Percona, you can issue following command;

$ sudo service mysql status
 * /usr/bin/mysqladmin  Ver 8.42 Distrib 5.5.38-35.2, for debian-linux-gnu on x86_64
...
Uptime:     76 days 9 hours 17 min 56 sec

Threads: 41  Questions: 148972423  Slow queries: 5894447  Opens: 1489  Flush tables: 1  Open tables: 552  Queries per second avg: 22.572

If you found many slow queries in last line, you can decide to enable slow queries log for tracking this issue. Unmark following two lines in your /etc/mysql/my.cnf file to enable logs for slow queries.

log_slow_queries        = /var/log/mysql/mysql-slow.log
long_query_time = 2

where 2 means only log mysql queries exec longer than 2 seconds. And then, restart your mysql service. There will log some slow queries for you to tune your code for database access. Of course, for the performance issue, you can disable it when it is not necessaryi anymore.

21 October 2014

How to test gem

locally

If you want to test the functionality of your gem before pushing it to public, you can specify the path of your local gem in your Gemfile.

Like this

gem 'foo', :path => 'path/to/gem'
21 October 2014

How to detach a process

from terminal

Sometimes there is a need to start a long running job on a remote server via ssh. In the case you forgot to use tmux or screen, the job on remote server will be automatically killed as soon as you close the terminal.

To avoid the process it being killed after your ssh session is over, you can detach it from the shell. In this case the job will remain running after you disconnect from the server.

In order to do this, first make sure that the job is running in background and then type disown. Disown will guarantee that SIGHUP signal will not be sent to the process, hence the process remains running.

Here is a short example:

# start your script that runs in foreground
$ ./myscript.sh
my script output
my script output

# press Ctrl+z to interrupt it
^Z
[1]+  Stopped                 ./myscript.sh

# make sure it runs in background by typing bg
$ bg
[1]+ ./myscript.sh &

# now type disown followed by job number. In our case it is number 1
$ disown %1

Now you can disconnect from the server without interrupting your process.

21 October 2014

Run previous commands

in your console

There are few ways to run previous commands in your console.

1. You can keep pressing UP key to brows your previous cmd and just hit ENTER to re run it.

2. !! will run the last command you executed in the console.

3. If you have a blur impression but not sure what you have run. No worries. First run history then grep for something you still remember

history | grep rest

then you will get something like

316  touch tmp/restart.txt
321  sudo service nginx restart

now if you want to run #316 simply run

!316

then #316 will run again!

01 October 2014

Network throttling

with trickle

Have you ever thought about how to limit bandwidth per a process the similar way “nice” is doing for limiting CPU usage?

There is a nice utility that can do exactly what you need. It is called trickle. It helps you to limit upload and download bandwidth for a process.

For example, if you run:

trickle -u 10 ncftp

Trickle will run ncftp and limit its download capacity to 10kB/s. Option -d here means “download”.

If you need to limit upload capacity, just pass -u option to trickle:

trickle -d 10 -u 20 ncftp

In this case trickle limits ncftp’s both upload and download capacity.

Here is a real example of how trickle limits download bandwidth. Let’s run wget without trickle:

wget -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test10.zip
--2014-10-01 08:19:13--
http://speedtest.wdc01.softlayer.com/downloads/test10.zip
Resolving speedtest.wdc01.softlayer.com
(speedtest.wdc01.softlayer.com)... 208.43.102.250
Connecting to speedtest.wdc01.softlayer.com
(speedtest.wdc01.softlayer.com)|208.43.102.250|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11536384 (11M) [application/zip]
Saving to: `/dev/null'

100%[================================================================>]
11,536,384  9.89M/s   in 1.1s

As you can see, we got 9.89M/s without any bandwidth limitations.

Lets’ run the same command with trickle and see what happens:

trickle -d 20 wget -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test10.zip
trickle: Could not reach trickled, working independently: No such file or directory
--2014-10-01 08:19:32--
http://speedtest.wdc01.softlayer.com/downloads/test10.zip
Resolving speedtest.wdc01.softlayer.com
(speedtest.wdc01.softlayer.com)... 208.43.102.250
Connecting to speedtest.wdc01.softlayer.com
(speedtest.wdc01.softlayer.com)|208.43.102.250|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11536384 (11M) [application/zip]
Saving to: `/dev/null'

100%[================================================================>]
11,536,384  20.3K/s   in 9m 1s

Trickle successfully limited download bandwidth to ~20 KB/s

trickle can be installed on linux using yum or apt-get, just run:

yum install trickle
# or
apt-get install trickle

(note, on mac, it doesn’t seem to compile well, you may have to use throttled rather)

01 October 2014

Git clean branches

when git flow gets messy

Anyone using git with gitflow may have faced this problem: many branches are not removed when feature or hotfix is done (especially when you merge manually and don’t use git flow feature finish).

Firtunately, there is an easy way to remove all branch in same namespace like feature/, hotfix/

git branch | awk -F. '/feature/{print $1}'| xargs -I {} git branch -D {}

or you can write it as a git alias and use stdin to assign the namespace, in your .git/config:

[alias]
  mdb = "!f() { if [ -z $1 ]; then echo 'Please assign branch namespace.'; else git branch | awk -F. '/'"$1"'/{print}' | xargs -I {} git branch -D {}; fi }; f"

Then use it

$ git mdb feature
01 October 2014

Keydown event

on input fields

Sometimes we need to get the input’s value when the keydown event is triggered.

But we will see the text we type and the result we get is not displayed immediately.

So we can add setTimeout or defer function to postpone the function to the last of current call stack. Then we can get a consistent result with what we type in the input real time. The value of the input will be already modified with the type key at the time the function is called.

Here’s a simple example http://codepen.io/Oliverl/pen/HwhIp

See the Pen HwhIp by Oliver - Frontend Developer (@Oliverl) on CodePen.

01 October 2014

Stats on tables

in the browser

Sometime we need to work on some analytics feature. totalizer is a chrome extension that can help with that on displayed tables in the browser.

When you have a table you want to work on, click the ‘Totalizer’ icon in the toolbar. You can now right click on a table column and choose a simple spreadsheet function like sum, average, etc. Simple and useful.

01 October 2014

Join twice

the same table

Sometimes we need to join the same table twice, for example

# Table name: attendances
#
#  student_id          :integer
#  teacher_id          :integer
#  status              :string

class Attendance < ActiveRecord::Base
  belongs_to :student
  belongs_to :teacher
end

class Student < User
end

class Teacher < User
end

Then you could use as to rename the query table name to join same table twice.

Attendances.
   joins("join users as students on attendances.student_id = students.id
          join users as teachers on attendances.teacher_id = teachers.id")
30 September 2014

Themes for devtools

in chrome

Chrome announced this experimental feature in December 2013, and there is a lot of themes already on the Chrome Web Store for download.

To enable this feature, just

  • open your DevTools (ctrl/cmd)-alt-i,
  • go to settings,
  • click the Experiments tab,
  • check the “Allow Custom UI themes”,
  • then install a theme from web store,

I like this one https://chrome.google.com/webstore/detail/devtools-theme-zero-dark/bomhdjeadceaggdgfoefmpeafkjhegbo.

When you installed a theme, you have to restart chrome, and when open the devtools again, you can see the inspector applied the theme that you installed.

chrome themed devtools