Calculate an approximation of a server response time with Rails and Apache

This post was born from the need of calculating some kind of server response time during a page request. Although this is not perfect, because doesn’t account for the time that the response takes to reach the user, it’s pretty accurate on the server side.

In order to have the request start time I added this header in the Apache configuration server:

RequestHeader append Request-Started "%t"

I didn’t use a simple before_filter because it would have not considered the time spent in rack middlewares, for example.

Now in my action I can count on a request header like this:

Request-Started: t=1460473552166899

Note that this timestamp is in microseconds.

Now in the page view template I can get the total request time doing some simple math:


server_response_time = current_time - time_from_header

def current_time
  Time.now.utc
end

def time_from_header
  Time.at(request.headers['Request-Started'].split("=").last.to_i / 1_000_000).utc
end

Improve your developer skills with Codewars

Some weeks ago in Mikamai we started using codewars, a website that aims to improve your programming skills via gamification with code challenges.
Since I’m a junior developer willing to improve my Ruby knowledge I have used it a lot lately, so let’s see how it works.

After signing up and completing a simple code problem, you can start browsing some kata and boost your programming skills.

Why kata? Codewars has this oriental, martial arts flavor so you’ll find words like kata and train for quizzes, kyu and dan to classify user rankings.

Personally I’m really enjoying the site and the learning process.
The number of Ruby katas is huge, and I find expecially nice the chance to see other users solutions after you complete a kata, so you can see super-compact solutions such as:

def anagrams(word, words)
  words.select { |w| w.chars.sort == word.chars.sort }
end

or regular expressions usage to solve a wide range of problems:

def pig_it text
  text.gsub(/(w)(w+)*/, '21ay')
end

and you can write your own tests while you’re training a kata.

You can choose among many programming languages (I’m looking forward to see Elixir or Rust in the future).