Initializing
First of all I have created the environment for the test. On the previously cloned machine the database has been imported and started. The data I used is from one of my webshops’ database called gumibomba_dev. It has more than 20k products which I can test the framework with.
1 2 3 4 5 6 7 8 9 10 11 |
/etc/init.d/mysql start mysql -e 'show databases' [root@localhost ~]# mysql -e 'show databases' +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ |
1 2 |
mysql -e 'create database gumibomba_dev' mysql gumibomba_dev < import.sql |
Install PERL and Catalyst framework
You’re gonna need make, gcc and cc to be able to do this.
1 |
# yum install make gcc |
Method followed by the description below http://wiki.catalystframework.org/wiki/installingcatalyst
Check perl installation on server
1 2 |
# rpm -qa perl perl-5.10.1-119.el6_1.1.x86_64 |
We need cpan for installing further CPAN packages
1 |
# yum install perl-CPAN perl-YAML |
Install Catalyst and its dependencies
1 |
# cpan Catalyst::Runtime Catalyst::Devel |
This will ask for some confirmation (typically “Shall I follow them and prepend them to the queue of modules we are processing right now?”). Read those and answer yes. Sometimes it just want to install temporary basis for testing.
This will take some time.
First steps
When the installation finished (on the test box almost took more then an hour) we can create or Catalyst project.
1 2 3 |
# mkdir -p /var/www/ # cd /var/www/ # catalyst.pl sandbox |
This has created our new project. We can run the development server now to see it’s working.
1 |
# script/sandbox_server.pl -r |
And it’s working very well. In my humble opinion installation process was a pain in the ass. But you have to do this just once.
Import module definition from database
We’re gonna need to install some helper for this.
1 |
# cpan Catalyst::Helper::View::TT Catalyst::Helper::Model::DBIC::Schema DBIx::Class::Schema::Loader MooseX::MarkAsMethods MooseX::NonMoose DBD::mysql |
After finished the installation we’re able to import schema from database and create the scaffold around the desired model.
1 2 3 |
# script/sandbox_create.pl model DB DBIC::Schema sandbox::Schema create=static dbi:mysql:gumibomba_dev # script/sandbox_create.pl controller Gumi # script/sandbox_create.pl view HTML TT |
Important!
If you use UTF-8 charset in your DB you should be aware of the followings:
http://wiki.catalystframework.org/wiki/tutorialsandhowtos/using_unicode
Create the first application
In the lib/sandbox/Controller/Gumi.pm we create two methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
sub index :Path :Args(0) { my ( $self, $c ) = @_; $c->stash(gumis => [$c->model('DB::Gumi')->search({}, {rows=> 10})]); $c->stash(template => 'gumi/index.tt2'); } =head2 show =cut sub show :Local :Args(1) { my ( $self, $c, $id ) = @_; $c->stash( id => $id, gumi => $c->model('DB::Gumi')->single({g_id=>$id}) ); $c->stash(template => 'gumi/show.tt2'); } |
The index will list 10 elements from “gumi” table through the DBIx ORM. The show method will show one element from “gumi” table. There’s no explicit query either of them. All object are provided by ORM layer.
The templates are
1 2 3 4 5 6 7 8 9 10 11 12 |
# cat root/src/gumi/index.tt2 <table> <tr><th>Márka</th><th>Típus</th></tr> [% # Display each book in a table row %] [% FOREACH gumi IN gumis -%] <tr> <td>[% gumi.g_marka %]</td> <td>[% gumi.g_tipus %]</td> <td><a href="[% c.uri_for(c.controller.action_for('show'), [gumi.g_id] ) %]">show</a></td> </tr> [% END -%] </table> |
1 2 |
# cat root/src/gumi/show.tt2 <h1>[% gumi.g_marka %] [% gumi.g_tipus %]</h1> |
Nginx and FastCGI
To be able to run as fastcgi we’re gonna need FCGI/ProcManager.pm.
1 |
# cpan FCGI::ProcManager |
I don’t want to go in to details how to make fastcgi socket and use nginx to work with it. If you’re searching for nginx+fastcgi+perl+catalyst in your favorite engine you will find a lot of results describing it properly.
Results
The test was made by apache benchmark. I did 1000 requests for index and show pages with 5, 20 and 50 concurrency level. The numbers are the requests/sec. The memory and cpu usage was almost the same no matter how many thread was set for the ab test.
Index | Show | |
AB 5 conc | 93,7 | 122,76 |
AB 20 conc | 94,39 | 122,18 |
AB 50 conc | 94,51 | 120,24 |
Memory usage | 11,40% | 11,40% |
CPU usage | 27,20% | 25,10% |
Recent comments