even faster django

Post on 02-Nov-2014

1.932 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Even Faster Django shared at PyCon Taiwan 2012.

TRANSCRIPT

Even Faster DjangoMongodb + Jinja2 + Bigpipe

Gage Tseng / GenieCapitalgagedark@gmail.com

About GageYahoo! F2E Intern

TechCrunch Hackathon 北京

Start my Python life

2008

2009

2010

2011

2012

Genie Capital

Master of CS NTU

PYCON

Gulu.com

Web Framework

Easy Quick

Performance ? Hmm.... it is OK

Fast ?How to make it

Fast ?What is

Fast is about User Experience

and Response Time

http://fast.example.com GO

1 2

http://crazy.fast.example.com GO

0.1

It is almost

Impossible

We are talking aboutlarge scale and dynamic web site

Definition of FastPage start to render in 1 second

Show important content in 2 seconds

Request and Response

Web Server

HTTP Request

DNS Lookup Data Transfer

DB

Data Transfer

Response

Parse HTMLDownload CSS, JS

DB Query

Render Response

Render and Execute JS

50%Database &Template Engine

Database Good Query Performance

High Scalability

Easy to Use

High Scalability

NoSQL Database

BSONJavascript

Schema Free

Django-Nonrel

Django Mongodb Engine

Migration-Free

$ python manage.py migrateNo more South

Embedded ModelLess Query and lookupclass UserProfile(models.Model):

contact = EmbeddedModelField('ContactModel')...

> print my_profile.contact.phone> u"0919xxxxxx"

SetField / ListFiled / DictField

Easy to use & Query Fastclass UserProfile(models.Model):

skill = ListField(models.CharField(max_length=24))...

> print my_profile.skill

> UserProfile.objects.filter(skill="python")> [u"python", u"javascript", u"erlang"]

> [<UserProfile Gage>, <UserProfile Jason>]

Template Engine High Render Speed

Easy to Use

Jinja2FastExtensibleDjango template like

Jinja2

https://github.com/coffin/coffin

Coffin

With Jinja2 You can<h1>{{ user1.get_nickname() }}</h1>

<h1>{{ user1.fun().attr }}</h1>

No more Complicated template tags

Benchmark

It is faster now

http://faster.example.com GO

0.9

Even FasterCan it be

What is Even FasterPage start to render in 0.5 second

Show important content in 1 second

BigPipe !Facebook 2009

http://bigpipe.example.com GO

0.2 0.9

http://bigpipe.example.com GO

0.2 0.9

BigpipeBreak web pages into multiple chunks (Pagelet)

Server render a pagelet and response

Prepare CSS

Render Page (HTML DOM)

Execute JS when JS resources are ready

Request and Response

Web Server

HTTP Request

DNS Lookup Data Transfer

DB

Data Transfer

Response

Parse HTMLDownload CSS, JS

DB Query

Render Response

Render and Execute JS

Web Server

HTTP Request

DNS Lookup Data Transfer

DB

DB QueryRender and Execute JS

Data Transfer Render Response

Parse HTMLDownload CSS, JS

DB Query

DB Query

http://bigpipe.example.com GO

0.2 0.9

How fast is it (from facebook)

Facebook

BigPipe

Streaming HttpResponsehttps://code.djangoproject.com/ticket/7581

BigPipe Javascripthttps://github.com/msroot/bigpipe/blob/master/bigpipe.js

Django Partdef home(request):

def stream_generator():base = render_to_string("base.html")yield base.ljust(4096)yield main_content(request).ljust(4096) #pagelet

yield footer(request).ljust(4096) #pagelet yield "</body></html>"

return HttpResponse(stream_generator(), mimetype='text/html', \ stream_content=True)

main_content and footer will prepare CSS and JS resource for pagelet.

<script>bigPipe.onPageletArrive({"html":"...", "css":{...},"js":{...}})

</script>

<!DOCTYPE html><html>

<head><title>I'm BigPipe</title><script type="text/javascript" src="bigpipe.js"></script>

</head><body>

<script>bigPipe.onPageletArrive({"html":"...", "css":{...},"js":{...}})

</script></body>

</html>

yield base.ljust(4096)

yield main_content(request).ljust(4096)

yield footer(request).ljust(4096)

yield "</body></html>"

http://bigpipe.example.com GO

0.2 0.9

Even Faster DjangoMongoDB + Jinja2 + Bigpipe

Genie Dev Team+

top related