Speed up zing me – ntvv2 code with PHP extension module
-
Upload
vo-duy-tuan -
Category
Documents
-
view
67.389 -
download
0
Transcript of Speed up zing me – ntvv2 code with PHP extension module
![Page 1: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/1.jpg)
Speed up ZingMe-NTVV2 with PHP extension module
18/12/2010
By Nguyen Trung ThanhZingMe Team leader
Web Technical - VNG
![Page 2: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/2.jpg)
Contents
Introduction
Why?
PHP extension basic
SWIG
Sample
![Page 3: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/3.jpg)
INTRODUCTION - ntvv2
![Page 4: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/4.jpg)
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
![Page 5: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/5.jpg)
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
![Page 6: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/6.jpg)
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
![Page 7: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/7.jpg)
What is an extension?
Have you ever used PHP extension?
PHP consist of many extensions
All function we used is from extensions
![Page 8: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/8.jpg)
Lifecycle
![Page 9: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/9.jpg)
Setting up build environment
- Setup PHP-devel
- Sample extension
![Page 10: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/10.jpg)
Is it complicated?
●Is there any sexier and easier way?●We can use SWIG
![Page 11: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/11.jpg)
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
![Page 12: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/12.jpg)
How SWIG works
![Page 13: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/13.jpg)
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
![Page 14: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/14.jpg)
Using SWIG
Defile module in swig file
Generate source code
Create module project, build it
![Page 15: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/15.jpg)
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"
![Page 16: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/16.jpg)
Exposure
•Swig recognizes C/C++ declarations
'struct' or 'class'
functions
•Hiding elements
%ignore solver::noupdate;
%include “satsolver/solver.h”
![Page 17: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/17.jpg)
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)
![Page 18: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/18.jpg)
SWIG steps
Generate code
Create project, compiling– Add genterated code to project
Running– Add extension module to PHP
![Page 19: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/19.jpg)
Sample
Cache data in PHP module
Strictly Confidential – Do Not Distribute
![Page 20: Speed up zing me – ntvv2 code with PHP extension module](https://reader034.fdocuments.net/reader034/viewer/2022042518/555a009bd8b42ad00a8b4eb8/html5/thumbnails/20.jpg)
Q&A