İki günlük bir aradan sonra serinin yeni bölümüyle(!) karşınızdayım.
Bu yazının ana konusu pcntl destekli, daemon olarak çalışan Azra Web Server. Değişiklik olsun diye önce benchmarklara bakalım. Aşağıdaki tabloda açıkça görülebildiği gibi Azra ağır yük altında bile Apache’den çok iyi, Nginx’ten biraz daha iyi.
| X | Ortalama (ms) | real | user | sys |
|---|---|---|---|---|
| Apache | 0.00938119351864 | 0m2.247s | 0m0.068s | 0m0.028s |
| Nginx | 0,00461062550545 | 0m4.443s | 0m0.152s | 0m0.036s |
| Azra | 0,00429985105991 | 0m1.992s | 0m0.112s | 0m0.024s |
Benchmark yöntemi:
Apache, Nginx ve Azra kullanarak aşağıdaki kodu 400 kere çağırıyoruz.
test.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ve test için kullandığımız python kodu (benchmark.py):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import urllib2 from threading import Thread class testSuite: def testConnection(self , url , thread = 0): page = urllib2.urlopen(url) data = page.read() page.close() print("Thread:%d" % thread) def simpleConnection(self , url , tourNum): for i in range(tourNum): self.testConnection(url) def threadedConnection(self , url , tourNum , threadNum): for i in range(tourNum): myThread = i % threadNum t = Thread(target=self.testConnection , args=(url , myThread)) t.start() myTestSuite = testSuite() myTestSuite.threadedConnection('http://localhost:8114/test.php' , 400 , 10) #azra #myTestSuite.threadedConnection('http://localhost/webserver/test.php' , 400 , 10) #apache #myTestSuite.threadedConnection('http://localhost/test.php' , 400 , 10) #nginx |
Gelişmeler:
Artık Azra, Ubuntu altında daemon olarak çalışabiliyor.
/etc/init.d/azra
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #! /bin/sh ### BEGIN INIT INFO # Provides: azra # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start and stop azra web server # Description: Start and stop azra web server ### END INIT INFO # Author: Can Ince <[EMAIL PROTECTED]> # Do NOT "set -e" PATH=/sbin:/usr/sbin:/bin:/usr/bin DESC="AZRA WEB SERVER" NAME=Azra DAEMON=/var/www/webserver/azra.php PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME PHP_CONFIG_FILE=/etc/php5/cgi/php.ini # Exit if the package is not installed [ -x "$DAEMON" ] || exit 0 # Read configuration variable file if it is present [ -r /etc/default/$NAME ] && . /etc/default/$NAME # Load the VERBOSE setting and other rcS variables . /lib/init/vars.sh # Define LSB log_* functions. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. . /lib/lsb/init-functions # If the daemon is not enabled, give the user a warning and then exit, # unless we are stopping the daemon if [ "$START" != "yes" -a "$1" != "stop" ]; then log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes" exit 0 fi # Process configuration export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS DAEMON_ARGS="-q -b $FCGI_HOST:$FCGI_PORT -c $PHP_CONFIG_FILE" do_start() { # Return # 0 if daemon has been started # 1 if daemon was already running # 2 if daemon could not be started start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ || return 1 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \ --background --make-pidfile --chuid $EXEC_AS_USER www-data $DAEMON -- \ $DAEMON_ARGS \ || return 2 } do_stop() { # Return # 0 if daemon has been stopped # 1 if daemon was already stopped # 2 if daemon could not be stopped # other if a failure occurred killall azra.php start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null # --name $DAEMON RETVAL="$?" [ "$RETVAL" = 2 ] && return 2 # Wait for children to finish too if this is a daemon that forks # and if the daemon is only ever run from this initscript. # If the above conditions are not satisfied then add some other code # that waits for the process to drop all resources that could be # needed by services started subsequently. A last resort is to # sleep for some time. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON [ "$?" = 2 ] && return 2 # Many daemons don't delete their pidfiles when they exit. rm -f $PIDFILE return "$RETVAL" } case "$1" in start) [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" do_start case "$?" in 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; esac ;; stop) [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" do_stop case "$?" in 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; esac ;; restart|force-reload) log_daemon_msg "Restarting $DESC" "$NAME" do_stop case "$?" in 0|1) do_start case "$?" in 0) log_end_msg 0 ;; 1) log_end_msg 1 ;; # Old process is still running *) log_end_msg 1 ;; # Failed to start esac ;; *) # Failed to stop log_end_msg 1 ;; esac ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 exit 3 ;; esac |
… ve /etc/defaults/Azra:
1 2 | START=yes VERBOSE=yes |
Azra multi-process olarak çalışabiliyor.
Ana kod (azra.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | #! /usr/bin/php-cgi <?php /** * Azra Web Server * * Web Server coded in pure PHP * * @package Azra Web Server * @author Can Ince * @copyright Copyright (c) 2010, Logikit / Can Ince. * @version 0.0.2 * @license http://www.opensource.org/licenses/mit-license.php * @link http://can.logikit.net */ set_time_limit(0); interface server { public function setWebRoot($path); public function setPort($port); public function serve(); } final class webserver implements server { public $socket; private $_port , $_webroot , $_queryString , $_webRootWithoutSlash, $_input , $_file , $_verbose , $_remoteAddr , $_remotePort; public function __construct() { $this->_verbose = 0; $_SERVER = array(); $_COOKIE = array(); } public function setPort($port) { $this->_port = $port; } public function verbose() { $this->_verbose = 1; } public function setWebRoot($path) { $this->_webroot = $path . '/'; $this->_webRootWithoutSlash = $path; } private function _mimeHandler($input) { $fileinfo = pathinfo($input); switch ($fileinfo['extension']) { case 'png': $mime = "text/png"; break; case 'jpg': case 'jpeg': $mime = "text/jpeg"; break; case 'gif': $mime = "text/gif"; break; case 'css': $mime = "text/css"; break; case 'swf': $mime = "application/x-shockwave-flash"; break; default: $mime = "text/html; charset=UTF-8"; } return $mime; } private function _handleCode($contents) { //eval() edilecek kodun başında <?php ya da <? varsa çakar. halledelim. $contents = trim($contents); if(substr($contents , 0 , 5) == '<?php') { $contents = substr($contents , 5); } elseif(substr($contents , 0 , 2) == '<?') { $contents = substr($contents , 2); } return $contents; } private function _parseGet($str) { $_GET = array(); $array = explode('&' , $str); foreach($array as $requestItem) { $itemArray = explode('=' , $requestItem); if(!empty($itemArray[0])) $_GET[$itemArray[0]] = $itemArray[1]; } unset($_GET['hyperTextServer_php']); //bunu $_GET'e enjekte ediyor. neden, bilmiyorum... } private function _parsePost($str) { $_POST = array(); $array = explode('&' , $str); foreach($array as $requestItem) { $itemArray = explode('=' , $requestItem); if(!empty($itemArray[0])) $_POST[$itemArray[0]] = $itemArray[1]; } } private function _parseCookie($str) { $array = explode('; ' , $str); foreach($array as $requestItem) { $itemArray = explode('=' , $requestItem); if(!empty($itemArray[0])) $_COOKIE[$itemArray[0]] = $itemArray[1]; } } private function _parseRequest($str) { $lines = explode("\n" , $str); $rawData = explode(' ' , $lines[0]); $_SERVER['REQUEST_METHOD'] = $rawData[0]; if($_SERVER['REQUEST_METHOD'] == 'POST') $this->_parsePost(end($lines)); $_SERVER['PHP_SELF'] = $this->_webRootWithoutSlash . $rawData[1]; $_SERVER['HTTP_HOST'] = substr($lines[1] , 6); $_SERVER['HTTP_USER_AGENT'] = substr($lines[2] , 12); $_SERVER['HTTP_ACCEPT'] = substr($lines[3] , 8); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = substr($lines[4] , 17); $_SERVER['HTTP_ACCEPT_ENCODING'] = substr($lines[5] , 17); $_SERVER['HTTP_ACCEPT_CHARSET'] = substr($lines[6] , 16); $_SERVER['HTTP_KEEP_ALIVE'] = substr($lines[7] , 12); $_SERVER['HTTP_CONNECTION'] = substr($lines[8] , 12); if(strstr($lines[9] , 'Referer')) { $_SERVER['HTTP_REFERER'] = substr($lines[9] , 9); $_SERVER['HTTP_COOKIE'] = substr($lines[10] , 8); } else $_SERVER['HTTP_COOKIE'] = substr($lines[9] , 8); if(!empty($_SERVER['HTTP_COOKIE'])) $this->_parseCookie($_SERVER['HTTP_COOKIE']); $_SERVER['SERVER_SIGNATURE'] = ' <address>Azra/0.0.1 Server at ' . $_SERVER['HTTP_HOST'] . ' Port ' . $this->_port . '</address>'; $_SERVER['SERVER_SOFTWARE'] = 'Azra/0.0.1'; $_SERVER['DOCUMENT_ROOT'] = $this->_webroot; $_SERVER['SCRIPT_FILENAME'] = $this->_webRootWithoutSlash . str_replace('./' , '/' , $this->_file); $_SERVER['SCRIPT_NAME'] = $this->_file; $_SERVER['REQUEST_TIME'] = time(); $_SERVER['REMOTE_ADDR'] = $this->_remoteAddr; $_SERVER['REMOTE_PORT'] = $this->_remotePort; if(!empty($this->_queryString)) $_SERVER['QUERY_STRING'] = $this->_queryString; } private function _handle404() { return "<title>UHU 404</title><h2>UHU 404</h2>Iın-ıın-ıın-ıın-cav-cav-cav-cav-oooooo - oooooo - zört - zört - zört -zört (araba alarmı efekti) Çok afedersin müdürüm ama yok öyle bir dosya."; } public function openSocket() { $this->socket = @socket_create_listen($this->_port); if (!$this->socket) { echo "Soket olamiyor!\n"; exit; } $this->_loadConfigHandler(); } private function _loadCOnfigHandler() { $directoryConfigCandidate = $this->_webroot . '/.ConfigHandler.php'; if ($directoryConfigCandidate && is_readable($directoryConfigCandidate)) { require ($directoryConfigCandidate); } } public function serve() { while (1) //daire-i ilanihaye! { $client = socket_accept($this->socket); socket_getpeername($client , $remoteAddr , $remotePort); $this->_remoteAddr = $remoteAddr; $this->_remotePort = $remotePort; $input = $this->_input = trim(socket_read ($client, 4096)); if($this->_verbose == 1) echo $input; $input = explode(" " , $input); $input = $input[1]; if(class_exists('ConfigHandler')) { $configHandler = new ConfigHandler($input , $this->_webroot); //konvansiyonumuza göre rewrite işi $configHandler'ın url özelliğinde bulunmalı if($configHandler->url != NULL) $input = realpath($configHandler->url); } $mime = $this->_mimeHandler($input); if ($input == "" || $input == '/') { $input = "/index.html"; } $input = ".$input"; $input = str_replace('//' , '/' , $input); $inputArray = explode('?' , $input); $this->_file = $inputArray[0]; $input = $this->_webroot . $this->_file; if (file_exists($input) && is_readable($input)) { if($this->_verbose == 1) echo "Dosya: $input\n"; $contents = file_get_contents($input); if(strstr($input , '.php')) { if(!empty($inputArray[1])) $this->_queryString = $inputArray[1]; $this->_parseGet($this->_queryString); $this->_parseRequest($this->_input); $_REQUEST = array_merge($_GET , $_POST , $_COOKIE); $contents = $this->_handleCode($contents); //PHP çalıştıktan sonra sonucu echo etmesini değil, $contents stringine atmasını istiyoruz. ob_start(); eval($contents); //gidip $contents'in üstüne yazmak doğru alışkanlık değil ama yeni değişken register etmek istemiyorum $contents = ob_get_contents(); ob_end_clean(); } $output = "HTTP/1.0 200 OK\r\nServer: HyperTextServer\r\nConnection: close\r\nContent-Type: $mime\r\n\r\n$contents"; } else { $contents = $this->_handle404(); $output = "HTTP/1.0 404 OBJECT NOT FOUND\r\nServer: HyperTextServer\r\nConnection: close\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n$contents"; } socket_write($client, $output); socket_close ($client); } socket_close ($this->socket); } public function start() { $pids = array(); $this->openSocket(); for($i = 0; $i < 10; $i++) { $pids[$i] = pcntl_fork(); if(!$pids[$i]) { // child process $this->serve(); exit(); } } for($i = 0; $i < 10; $i++) { pcntl_waitpid($pids[$i], $status, WUNTRACED); } } } $server = new webserver(); $server->setWebRoot('/var/www/act'); $server->verbose(); //ayrıntıları stdout'a echo et. $server->setPort(8115); $server->start(); |
Ara-sonuçlar:
- Bir kaç sorun hala devam ediyor.
– Session’lar düzgün halledilemiyor.
– include’larda sorun yaşanıyor zira dirname(__FILE__) Azra’nın çalıştığı dizini döndürüyor.
- log işine çözüm bulmak lazım.
– header() kullanarak 302 yollanırken path düzgün alınamıyor.
Bu sorunlar da halledildikten sonra şaka olarak başlayan denememiz oldukça ilginç hale gelecek ve bu benchmarkları korumayı başarabilirsem 0.0.3 sürümüne Launchpad yolları görünüyor.
Etiketler: Azra Web Server, PHP, Web Server


1 yorum
Bu yazının yorumlarına abone ol
İzleme linki: http://can.logikit.net/2010/04/27/bir-web-sunucusu-kodlama-denemesi-–-4-azra-web-server/trackback/