Archive for the ‘PHP’ Category
How to ease Drupal development with Capistrano
Posted by Giovanni Intini | Filed under Capistrano, Drupal, PHP
Drupal is a great piece of software, unfortunately it stores so much stuff in the db that people struggle keeping in sync the development server/box and a staging server to show their customers how the work is proceeding.
Today I will share the Capistrano tasks I use to sync my development box with the staging server. What I basically do is dumping the development db, sending it to the server via capistrano and then use the dump to replace the server’s database.
The following tasks should be used together with the tasks in my Deploying drupal with Capistrano article. I took advantage of deploy:cold not being needed with Drupal, and added a callback to it, so if you want to do a deploy that also updated the database you should use deploy:cold.
You should also have two settings files (usually stored in drupal_root/sites/default), one called settings.development.php, with your local database setup and one called settings.production.php with the remote database setup, the capistrano tasks will take care of choosing the correct one.
# Callbacks before 'deploy:start', 'drupal:db:import:production' before 'deploy:restart', 'drupal:configure:production' before 'deploy:start', 'drupal:configure:production' before 'deploy:cold', 'drupal:db:dump:development' # DB Stuff set :mysqldump, "/path/to/mysqldump" set :local_db_user, "local_mysql_username" set :local_db_password, "local_mysql_password" set :local_db_name, "local_db_name" set :db_user, "remote_mysql_username" set :db_password, "remote_mysql_password" set :db_name, "remote_db_name" namespace :drupal do namespace :configure do task :production do sudo "cp #{latest_release}/sites/default/settings.production.php #{latest_release}/sites/default/settings.php" end task :development do sudo "cp #{latest_release}/sites/default/settings.development.php #{latest_release}/sites/default/settings.php" end end namespace :db do namespace :dump do task :development do raise RuntimeError.new("failed dump") unless system "#{mysqldump} -u #{local_db_user} --password=#{local_db_password} #{local_db_name} > dump.sql" end end namespace :import do task :production do ENV["FILES"] = "dump.sql" deploy::upload run "mysql -u #{db_user} --password=#{db_password} #{db_name} < #{latest_release}/dump.sql" end end end end
Deploying Drupal with Capistrano
Posted by Giovanni Intini | Filed under Capistrano, Drupal, PHP, Ruby
Mikamai, the company I work for, has just released Montalbano.tv, the companion site to one of the most successful TV shows in Italy.
I was the technical director of this Drupal based project, and while I was happy we chose Drupal, because it allowed us to deliver all the features they needed on time, I almost panicked when they told us the production setup would have two servers, both with database and web serving duties.
The database replication was standard MySql master-master setup, but I had to develop a strategy to keep the two code-bases on the two servers synchronized.
Being a Ruby programmer at heart, I selected the only tool that never fails me in circumstances like the one we had: Capistrano.
Unfortunately, while Capistrano is all easy to use with Rails, I had to write a custom Drupal-tailored Capfile.
Here it is, in its entirety, in case you ever need to deploy Drupal with cap (now I always deploy Drupal with cap, since I have the recipe ready
):
load 'deploy' if respond_to?(:namespace) # cap2 differentiator # Standard configuration set :user, "username" set :password, "password" set :application, "application.name" # I like to deploy the code in /var/apps # and then link it to the webserver directory set :deploy_to, "/var/apps/#{application}" # SCM Stuff configure to taste, just remember the repository # here I used github as main repository set :repository, "git@github.com:username/project.git" set :scm, :git set :branch, "master" set :repository_cache, "git_master" set :deploy_via, :remote_cache set :scm_verbose, true # Two servers, double fun # You really don't need app, web and db here, # but I used all of them just to be sure. # Usually only web is ok. role :app, "first.server.address.com" role :app, "second.server.address.com", :primary => true role :web, "first.server.address.com" role :web, "second.server.address.com", :primary => true role :db, "first.server.address.com" role :db, "second.server.address.com", :primary => true after 'deploy:setup', 'drupal:setup' # Here we setup the shared files directory after 'deploy:symlink', 'drupal:symlink' # After symlinking the code we symlink the shared dirs # Before restarting the webserver we fix all the # permissions and then symlink it to production before 'deploy:restart', 'mikamai:permissions:fix', 'mikamai:symlink:application' namespace :drupal do # shared directories task :setup, :except => { :no_release => true } do sudo "mkdir -p #{shared_path}/files" sudo "chown -R #{user}:#{user} #{deploy_to}" end # symlink shared directories task :symlink, :except => { :no_release => true } do sudo "ln -s #{shared_path}/files #{latest_release}" end end namespace :deploy do # adjusted finalize_update, removed non rails stuff task :finalize_update, :except => { :no_release => true } do sudo "chmod -R g+w #{latest_release}" if fetch(:group_writable, true) end task :restart do # nothing to do here since we're on mod-php end end namespace :mikamai do # symlinking to production namespace :symlink do task :application, :except => { :no_release => true } do sudo "rm -rf /var/www/montalbano" sudo "ln -s #{latest_release} /var/www/montalbano" end end # change ownership namespace :permissions do task :fix, :except => { :no_release => true } do sudo "chown -R www-data:www-data #{latest_release}" end end end
For the first time ever Apple failed me. A PHP horror story on mac os x.
Posted by Giovanni Intini | Filed under Apple, PHP
Earlier today I was on the verge of smashing my macbook pro keyboard. I lost one whole day trying to make mssql support work on php5.
Apple ships a working php5, but obviously without any support for mssql. So I had to rebuild php from scratch.
I battle for 2 hours trying to compile freetds, then I discovered the latest version of freetds doesn’t work with php5.
When I finally compiled freetds I discovered the infamous iconv bug that doesn’t allow php to compile on mac os x with iconv support. I disabled iconv, and it compiled.
I was not finished though: apple shipped apache doesn’t want any recompiled PHP, so I had to recompile apache too.
When I thought it was all ok I discovered that symfony needs iconv so I threw all away and installed Macports (now I have macports and fink), and am trying to compile php5.
Wish me good luck.
The battle of the languages part II
Posted by Giovanni Intini | Filed under Java, Javascript, Lua, PHP, Programming, Ruby
My previous post about a programming contest I had with my friends was wildly popular. Much much more than I even thought possible.
I started with a simple comparison of PHP, C, Ruby, Erlang and Javascript and ended up with lots of comments where people posted interesting implementations in more and more languages, even obscure ones (to me) like Haskell and Ocaml.
I owe everyone a fairer benchmark so I will do the test again, this time testing the following languages:
- C
- Common Lisp
- Erlang
- Haskell
- Javascript
- Java
- Ocaml
- Perl
- PHP
- Python
- Ruby
I need submissions for most of these languages, and I actually am quite open to submissions for the languages I speak
The rules are simple, the program has to store the results in an array (not just count them) and has to run without any strange libraries or compilers or compiler switches. To avoid interpreter starting time problems, please submit programs that autotime themselves.
As usual we’ll find the pythagorean triplets up to 5000. The reference implementation is the following, in Ruby:
t = Time.now result = [] 2.upto(5000) do |c| 1.upto(c-1) do |b| a = Math.sqrt(c*c - b*b) result << [a.to_i, b, c] if a.to_i == a end end puts Time.now - t
This code takes 15.70s to run on my macbook pro.
Now it’s your turn!
Update: Lua version by Sindisil
local tt = os.clock() local result = {} local c for c = 2, 5000 do local b for b = 1, c-1 do local a = math.sqrt(c*c - b*b) if a == math.floor(a) then result[#result+1] = { a, b, c } end end end print(os.clock() - tt)
It takes 5.48s to run on my box.
Update: new Ruby version.
include Math a, b, c = 0 result = [] 2.upto(5000) do |c| d = c * c 1.upto(c-1) do |b| a = sqrt(d - b*b) result << [a.floor, b, c] if a.floor == a end end puts Process.times[0]
Following Gabriele Renzi’s suggestions we now have a faster ruby version: 12.30s
Update: faster Lua version from Chris Swetenham, it runs in 3.44s
local tt = os.clock() local result = {} local c local n = 1 local math = math local sqrt = math.sqrt local floor = math.floor for c = 2, 5000 do local b for b = 1, c-1 do local a = sqrt(c*c - b*b) if a == floor(a) then result[n] = { a, b, c } n = n + 1 end end end print(os.clock() - tt)
Update: PHP version by Fullo
<?php $start = microtime(true); $a = $b = $c = 0; $result = array(); for ($c = 2; $c <= 5000; $c++) { for ($b = 1; $b < $c; $b++) { $a = sqrt($c*$c - $b*$b); if (intval($a) == $a) $result[] = array($a,$b,$c); } } echo (microtime(true) - $start); ?>
It runs in 9.73s.
Update: Python version from ludo, it runs in 11.2s
from time import clock start = clock() from math import sqrt results = [] for c in xrange(2, 5001): d = c * c for b in xrange(1, c): a = sqrt(d - b * b) if a % 1 == 0.0: results.append((a, b, c)) print clock() - start
The results for now:
- Lua 3.44
- PHP 9.73
- Python 11.20
- Ruby 12.30
PHP @ Yahoo!
Posted by Giovanni Intini | Filed under PHP, Yahoo
As I said in my last post yesterday I attended PHP Day, mainly for the presentation given by my friend Federico Feroldi, Senior Architect Engineer at an important internet company (not the most important one though).
The presentation was really nice, because it’s an insight in Yahoo’s architecture and it’s helpful to understand certain best practices for really-high traffic websites.
You can find the slides here.
Erlang, Ruby and PHP battle it out!
Posted by Giovanni Intini | Filed under Erlang, Javascript, PHP, Programming, Ruby
Update: here’s the followup!
Yesterday I attended PHP Day, in the beautiful city of Verona, and while going there by train, together with my friend Federico Feroldi, I told him that in a test I made a week ago Erlang was much slower than ruby in finding pythagorean triplets.
That was a partial lie, since the ruby implementation was somewhat optimized, while the erlang implementation wasn’t at all. So we decided to rewrite both programs using the same optimized algorithm and then test their speeds.
This is the ruby implementation of a method that counts the number of pythagorean triplets up to N:
def pythag(n) result = [] (2..n).each do |c| (1...c).each do |b| a = Math.sqrt(c*c - b*b) result << [a.to_i, b, c] if a.to_i == a end end result end
Then we coded it in Erlang. The code I’m going to show you is more complex than the ruby code because we took advantage of the parallelization features of the Erlang language:
-module(pythag). -export([bard/1, pbard/1]). my_is_integer(X) -> round(X) == X. pbard(N) -> pmap(fun(C) -> [ {C, B, trunc(A)} || B <- lists:seq(C, N), A <- [math:sqrt(C*C + B*B)], my_is_integer(A) ] end, lists:seq(2, N)). bard(N) -> lists:map(fun(C) -> [ {C, B, trunc(A)} || B <- lists:seq(C, N), A <- [math:sqrt(C*C + B*B)], my_is_integer(A) ] end, lists:seq(2, N)). pmap(F, L) -> S = self(), %% make_ref() returns a unique reference %% we'll match on this later Ref = erlang:make_ref(), Pids = lists:map(fun(I) -> spawn(fun() -> do_f(S, Ref, F, I) end) end, L), %% gather the results gather(Pids, Ref). do_f(Parent, Ref, F, I) -> Parent ! {self(), Ref, (catch F(I))}. gather([Pid|T], Ref) -> receive {Pid, Ref, Ret} -> [Ret|gather(T, Ref)] end; gather([], _) -> [].
The pythag function has been named bard, as in The Bard
The pbard function fires up a thread for each iteration of the outer loop and then gathers all the result back. This allows for up to 50% performance increase when using dual core CPUs (like my Macbook Pro
).
Once we coded it in two languages we couldn’t stop, so here’s a PHP implementation, a Javascript one and, last, but not least, a C implementation, because you can never forget C when you need speed
PHP:
<?php $i=0; for ($c = 2; $c <= 5000; $c++) { for ($b = 1; $b < $c; $b++) { $a = sqrt($c*$c - $b*$b); if (is_int($a)) { $i++; } } } print("$i"); ?>
JavaScript:
function pythag(n) { var a,b,c,i for (c = 2; c <= n; c++) { for (b = 1; b < c; b++) { a = Math.sqrt(c*c - b*b); if (Math.floor(a) == a) { i++; } } } return i; }
C:
#include <stdio.h> #include <math.h> #define MAX 5000 int main() { double c, b; int i = 0; double a; for (c = 2; c <= MAX; c++) { for (b = 1; b < c; b++) { a = sqrt(c*c - b*b); if (trunc(a) == a) { i++; } } } printf("%d\n", i); }
To test the various relative speeds I ran them from the shell using time (php ruby and c), or used the internal timer (erlang), or ran them in firefox (js only actually).
The results were interesting. At the first run, using 5000 as N in the pythag functions the order was:
- C (obviously): 0.40s
- Erlang (smp): 3.95s
- Erlang (non-smp): 5.66s
- Ruby: 15.31s
- PHP: 19s (or so)
- Javascript: couldn’t even finish running because firefox said it was taking too much time
Fullo passed by and asked what we were doing, I told him I was testing languages and that PHP was dog-slow. He said it couldn’t be and asked me what version of PHP I was using. I checked and it was PHP 4.x.x.
After downloading and compiling PHP5 I ran the tests again and the final result was this:
- C (obviously): 0.40s
- Erlang (smp): 3.95s
- Erlang (non-smp): 5.66s
- PHP5: 8.9s (or so)
- Ruby: 15.31s
- Javascript: couldn’t even finish running because firefox said it was taking too much time
Now the numbers make sense. Doing these comparations was a nice experience, because when you implement the same thing in different languages you always learn something about optimization and code beauty.