20
Speed up ZingMe-NTVV2 with PHP extension module 18/12/2010 By Nguyen Trung Thanh ZingMe Team leader Web Technical - VNG

Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

Embed Size (px)

DESCRIPTION

Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

Citation preview

Page 1: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

Speed up ZingMe-NTVV2 with PHP extension module

18/12/2010

By Nguyen Trung ThanhZingMe Team leader

Web Technical - VNG

Page 2: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

Contents

Introduction

Why?

PHP extension basic

SWIG

Sample

Page 3: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

INTRODUCTION - ntvv2

Page 4: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

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 ZingMe-Nông trại vui vẻ 2 with PHP extension module

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 ZingMe-Nông trại vui vẻ 2 with PHP extension module

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 ZingMe-Nông trại vui vẻ 2 with PHP extension module

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 ZingMe-Nông trại vui vẻ 2 with PHP extension module

Lifecycle

Page 9: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

Setting up build environment

- Setup PHP-devel

- Sample extension

Page 10: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

Is it complicated?

●Is there any sexier and easier way?●We can use SWIG

Page 11: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

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 ZingMe-Nông trại vui vẻ 2 with PHP extension module

How SWIG works

Page 13: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

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 ZingMe-Nông trại vui vẻ 2 with PHP extension module

Using SWIG

Defile module in swig file

Generate source code

Create module project, build it

Page 15: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

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 ZingMe-Nông trại vui vẻ 2 with PHP extension module

Exposure

•Swig recognizes C/C++ declarations

'struct' or 'class'

functions

•Hiding elements

%ignore solver::noupdate;

%include “satsolver/solver.h”

Page 17: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

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 ZingMe-Nông trại vui vẻ 2 with PHP extension module

SWIG steps

Generate code

Create project, compiling– Add genterated code to project

Running– Add extension module to PHP

Page 19: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

Sample

Cache data in PHP module

Strictly Confidential – Do Not Distribute

Page 20: Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module

Q&A