Upload
nguyen-thanh
View
6.847
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module
Citation preview
Speed up ZingMe-NTVV2 with PHP extension module
18/12/2010
By Nguyen Trung ThanhZingMe Team leader
Web Technical - VNG
Contents
Introduction
Why?
PHP extension basic
SWIG
Sample
INTRODUCTION - ntvv2
Introduction
• Ntvv2 (http://me.zing.vn/apps/ntvv2)– 1.M daily active user– Average 800 rps (per webserver) max 1100
rps– 9300 rps (all database servers)– 3 webservers
• Use Membase for cache and storage• Use other db for secondary storage• All bussiness function is moved to PHP
extension
Strictly Confidential – Do Not Distribute
Why to write PHP module?
Make complicated Bussiness functions run faster, consume less memory
PHP high-level overhead cpu/mem
Advance facility with PHP-FPM
Cache something in PHP process
PHP extension basic
What is an extension?
Lifecycle
Setting up a build environmenthttp://devzone.zend.com/article/4486
http://devzone.zend.com/article/1021-Extension-Writing-Part-I-Introduction-to-PHP-and-Zend
What is an extension?
Have you ever used PHP extension?
PHP consist of many extensions
All function we used is from extensions
Lifecycle
Setting up build environment
- Setup PHP-devel
- Sample extension
Is it complicated?
●Is there any sexier and easier way?●We can use SWIG
Strictly Confidential – Do Not Distribute
SWIGSWIG is an interface compiler that connects
programs written in C and C++ with scripting languages such as PHP, Python, Ruby...
How SWIG works
Using SWIG
Install module to PHP extensions
Sample
How SWIG works
What does SWIG do for you ?
Namespace
Constants
Type conversion
For simple types (int, float, char *, enum)
Wraps complex types
Pointers to structs and classes
Exposes functions
Using SWIG
Defile module in swig file
Generate source code
Create module project, build it
Define module
Data type
Wrapper class/functions
Strictly Confidential – Do Not Distribute
%module ntvv2module%{#include <string>#include <vector>#include <list>#include <string>#include “ntvvgame.h"%}struct LandInfo{ int id; std::string data;} ;LandInfo updateLandInfo(int uid, int landID);
%include "std_map.i"%include "std_vector.i"%include "std_string.i"%include "std_pair.i"
Exposure
•Swig recognizes C/C++ declarations
'struct' or 'class'
functions
•Hiding elements
%ignore solver::noupdate;
%include “satsolver/solver.h”
Useful commands•Renaming
%rename("to_s") asString();
%rename( "name=" ) set_name( const char *name );
%rename("empty?") empty();
•Aliasing
%alias get "[]";
•Constants
%constant int Script = C_CONSTANT;
•Defines
%define YUILogComponent "bindings"
%enddef
%define %macro(PARAMETER)
SWIG steps
Generate code
Create project, compiling– Add genterated code to project
Running– Add extension module to PHP
Sample
Cache data in PHP module
Strictly Confidential – Do Not Distribute
Q&A