Mobil Mewah

Salah satu sumber Inspirasi.

Mobil Sport terbaik

Anda pasti bisa memilikinya.

Bermain dengan pesawat

Salah satu ide yang gila, Balapan di udara.

Bermain di angkasa

Apakah ini salah satu Goals dalam hidup anda? anda pasti bisa mencapainya

Wednesday 12 March 2014

Upgrade from Django15 to Django16

Upgrade Django 1.5 yo Django 1.6
Here again I am trying to upgrade my application from Django1.5 to Django1.6 which I documented here.

The bump I found in my code was the url import in urls.py

Before we import urls like this :

from django.conf.urls.defaults import *


In Django1.5 there was warning that this will be deprecated. Now in Django1.6 , the app wont start.

Instead we must use this for the import :

from django.conf.urls import *


The package moved to other place. This was a bit wow for a big applications where the urls.py spread all over the application.

For backward compatibility I use this import :

try:

    from django.conf.urls.defaults import *

except:

    from django.conf.urls import *


Then the other bump was the cache_page decorator. In Django 1.3 the usage was :

from django.views.decorators.cache import cache_page

urlpatterns = ('',
    (r'^foo/(\d{1,2})/$', cache_page(my_view, 60 * 15)),
)

In Django 1.4 the usage was :
from django.views.decorators.cache import cache_page

urlpatterns = ('',
    (r'^foo/(\d{1,2})/$', cache_page(60 * 15)(my_view)),
)

when in Django1.5 there were deprecated warning for the cache decorator. It said :

The cache_page decorator must be called like: cache_page(timeout, [cache=cache name], [key_prefix= key prefix])

And in Django1.6 there error came out in using cache decorator. How I use the cache decorator was  :

url(r'^sitemap\.xml$', cache_page(sitemap,LONG_CACHE), {'sitemaps': sitemaps}),

So to fix it, we need to change the calling of cache_page decorator. We call using the cache_page(timeout, viewfunc) so here are the fixed url :


url(r'^sitemap\.xml$', cache_page(LONG_CACHE)(sitemap), {'sitemaps': sitemaps}),

With this all working in my application.One thing which i concert is that johnnycache not working in Django1.6 which needed for cache all database query.

The most version updated version i use now will be Django 1.6.2 as per today 9 March 2014.

Hope this give hint for you who need upgrading also.

Tuesday 11 March 2014

Testing web application performance

For developers, we need to know how to test the performance of our application. Some tools are available to hit your application and know about the maximum performance you can get. Of course the test done on the same machine of production or just in your local development machine.

Why we need to make this test ? Actually is it a simple question. For most of the test done, we need to know the baseline of the application, and if we make some improvement, we need to know how it is affect the application performance.

For new application baked from your development lab, of course the result give use some baseline for the application performance.

After the application running for a while, we got hit and seems something can be done better. We update, and we can test again with the same testing parameters. Then we can compare the result with the previous results.

So the testing can be done in linux using apache bench, or httperf. With the tool we can test with concurrent connection and state it in the command.

Because I done some code in python, I found some neat tools to test the web application performance, using python. The application used was Pylot, if you have heard of it. It comes with 2 mode, GUI and console mode, and have a nice html report.

So lets get started to use it. Get and extract it to your folder.

Then you can config the pylot default behavior, inside pylot folder, go to core, you will find config.py , then contents are :

AGENTS = 1
DURATION = 200  # secs
RAMPUP = 0  # secs
INTERVAL = 0  # millisecs
TC_XML_FILENAME = 'testcases.xml'
OUTPUT_DIR = None
TEST_NAME = None
LOG_MSGS = False

GENERATE_RESULTS = True
SHUFFLE_TESTCASES = False  # randomize order of testcases per agent
WAITFOR_AGENT_FINISH = True  # wait for last requests to complete before stopping
SMOOTH_TP_GRAPH = 1  # secs.  smooth/dampen throughput graph based on an interval
SOCKET_TIMEOUT = 300  # secs
COOKIES_ENABLED = True

HTTP_DEBUG = False  # only useful when combined with blocking mode  
BLOCKING = False  # stdout blocked until test finishes, then result is returned as XML
GUI = False

You can change the content of the config as you wish. Then to run it from pylot root folder, the run.py you can call from command line.

And for easy test, you can create xml file for each of your test profile. for example the devtest.xml content for testing local Django application :


    
        http://127.0.0.1:8000
    


You can also specify the output directory of the report in run time using -o directive.

Now to run the test, just call :

$python run.py -o report -x devtest.xml

This will run by default config above for 200 second and finish. You will have the html report in the report directory you specify before.

Next I will post my test result with my app running Django 1.5 and Django 1.6 , as i have upgrade my previous version of Django application before .


Sunday 9 March 2014

Upgrade Django 1.4 to Django 1.5

Upgrade Django 1.4 to Django 1.5
After successfully upgrade my Django app from using Django 1.3 to Django 1.4, now i try to upgrade my app to be running Django 1.5.5

Some method i use are :

+ OS Debian 7
+ Python 2.7
+ Virtualenv

Django 1.5.5 released around October 2013. Here are the Official Release Notes

The deprecated feature was the new way of handling url in template.

What i got was the project template still the same with Django 1.4.10 .
After running I got bumped with deprecated feature which is url "new style" usage.

So before i use this :

{% url public_detail_billboard object.slug %}


It will give error :

'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.

So we need to change it to :

{% url "public_detail_billboard" object.slug %}


Lucky for me, my application only breaks in the url method in the template. But the url all over the template. so i need to update the url to add some " " .

But we can do some faster thing with this, we are programmer right?

we can use sed to the rescue, here are the sed command which will do your job :

find . -type f -print0 | xargs -0 sed -i 's/{% url \([^" >][^ >]*\)/{% url "\1"/g'

That will change your file from :

{% url something.else ass bas %}


to this one :

{% url "something.else" ass bas %}

And now my application can run in Django 1.5.5

Some additional info, i upgrade some of my tools which supported in Django 1.5.5, which are :

+ Django Debug Toolbar 1.0.1

Some warning if you upgrade to Django 1.5.5, your app won't work in Django 1.3, which the url problem we have here.

But to make it still work in Django 1.3.x you must add

{% load url from future %}

in the Template.

So the most annoying job will be change your url to using "urlname" to all you html. If you practice reusable code, cannot imagine how many url you will be added in html. Remember using the sed command.

So to old code compatible with Django 1.3 remember :

1. Add {% load url from future %} in your template using url
2. Always use {% url "urlname" %} in your template after Django 1.5.5

Hope this helps you which planning to migrate to newer Django Release.

 

Saturday 8 March 2014

Virtualenv with Virtualenvwrapper power

Work with python, you should know about virtualenv which enable us as python developer to build great thing with different setup.

For the newbie, a quick info is you can run your app with different python version and different package version without version conflict.

For example we have twitterclient application. It build on python2.7 and Django1.3 . Now i need to test it using Django1.4 . So how we do this ?

We not need to uninstall the Django1.3, but we use virtualenv, and use virtualenvwrapper on top of it. It makes developer life easier.

Lets dive in to the usage. First you have pip already installed, if not install it using :

# easy_install pip


Then you install the virtualenv and virtualenvwrapper using pip.

# pip install virtualenvwrapper


This will download also the virtualenv as the dependency.

First thing you must add in your bash environment. In you home directory open your .bashrc with your favorite editor and add :

export WORKON_HOME=$HOME/.virtualenvs

source /usr/local/bin/virtualenvwrapper.sh

For windows install, you add in your windows Environment Variables :

   WORKON_HOME=$USERPROFILE$\.virtualenvs
   PATH = C:\python2.7\Scripts\


The WORKON_HOME tells where the virtualenv will install. When you create a new virtualenv, it will put under WORKON_HOME you specified in the bashrc .
This also can work with zsh and csh.

And you can start using virtualenv. Lets make a new virtualenv :

super@devstack:$mkvirtualenv newbigthing

(newbigthing)super@devstack:$


The output will be like the above. To get out from the env, just type deactivate and you got your normal env.

If you have many environment for project, you can switch easily, just issue :

$ workon newbigthing

$ workon somebiggerthing


Inside the virtualenv you can install python package as usual using pip which is installed by default when the env created.

This is faster then using virtualenv alone, example when you need to go to a virtualenv you need to issue command like this :

$source /env/newbigthing/bin/activate


So long to type, with virtualenvwrapper, just issue workon yourproject

Happy coding with Virtualenvwrapper .

Twitter Delicious Facebook Digg Stumbleupon Favorites More