diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d48c759 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.vscode \ No newline at end of file diff --git a/DockerConfig/sorttest.com.conf b/DockerConfig/sorttest.com.conf new file mode 100644 index 0000000..caef262 --- /dev/null +++ b/DockerConfig/sorttest.com.conf @@ -0,0 +1,12 @@ + + # This first-listed virtual host is also the default for *:80 + ServerName localhost + ServerAlias sorttest.com + DocumentRoot "/var/www/html" + + + AllowOverride None + Order Allow,Deny + Allow from All + + \ No newline at end of file diff --git a/Dockerfilenode b/Dockerfilenode new file mode 100644 index 0000000..d2f236c --- /dev/null +++ b/Dockerfilenode @@ -0,0 +1,5 @@ +FROM node:12-alpine +WORKDIR /var/www/html +COPY . . +EXPOSE 80 +CMD [ "node", "bubble-sort-v2-http" ] \ No newline at end of file diff --git a/Dockerfilephp b/Dockerfilephp new file mode 100644 index 0000000..b7d88a8 --- /dev/null +++ b/Dockerfilephp @@ -0,0 +1,14 @@ +FROM php:7.3-apache + +RUN docker-php-ext-install pdo pdo_mysql +RUN docker-php-ext-install opcache + +RUN rm -rf /etc/apache2/sites-enabled/* +COPY DockerConfig/sorttest.com.conf /etc/apache2/sites-available + +RUN a2ensite sorttest.com.conf + + +EXPOSE 80 + +RUN service apache2 restart \ No newline at end of file diff --git a/README.md b/README.md index e997bab..6aeb642 100755 --- a/README.md +++ b/README.md @@ -25,4 +25,31 @@ At 1,000 elements PHP is 5 times slower than Node If I've made a mistake in time calculations, please let me know, and I'll correct the issue. + +## Web Server Performance + +Due to the nature of the V8 engine being just in time compiled simply running node.js as a command does not show the +whole picture. So let us boot up some webservers in docker to get another look. For this I will assume you have docker +installed and knowledge how to use docker and docker-compose. + +Boot up web servers: + +```bash +docker-compose up +``` + +Open browser and go to each url. Feel free to change the "num" param to suit various tests. + +For PHP: + +``` +http://127.0.0.1/bubble-sort-v2-http.php?num=4096 +``` + +For Node.js: +``` +http://127.0.0.1:8080/?num=4096 +``` + + Thanks! \ No newline at end of file diff --git a/bubble-sort-core.js b/bubble-sort-core.js new file mode 100644 index 0000000..e972640 --- /dev/null +++ b/bubble-sort-core.js @@ -0,0 +1,47 @@ +const bubbleSort = (a) => { + const len = a.length; + + let sorted = false; + while(!sorted) { + sorted = true; + for (let i = 0; i < len; i++) { + let current = a[i]; + let next = a[i + 1]; + + if(next < current) { + a[i] = next; + a[i + 1] = current; + sorted = false; + } + } + } +}; + +const buildArray = (numberPool) => { + let myArray = []; + // add numbers divisible by 2 + for (let x = numberPool; x >= 0; x--) { + if (x % 2 === 0) { + myArray.push(x); + } + } + + // add numbers divisible by 3 + for (let x = numberPool; x >= 0; x--) { + if (x % 3 === 0) { + myArray.push(x); + } + } + + // add numbers divisible by 7 + for (let x = numberPool; x >= 0; x--) { + if (x % 7 === 0) { + myArray.push(x); + } + } + + return myArray; +}; + +module.exports.bubbleSort = bubbleSort; +module.exports.buildArray = buildArray; \ No newline at end of file diff --git a/bubble-sort-core.php b/bubble-sort-core.php new file mode 100644 index 0000000..5c77484 --- /dev/null +++ b/bubble-sort-core.php @@ -0,0 +1,51 @@ += 0; $x--) { + if ($x % 2 === 0) { + $myArray[] = $x; + } + } + + // add numbers divisible by 3 + for ($x = $numberPool; $x >= 0; $x--) { + if ($x % 3 === 0) { + $myArray[] = $x; + } + } + + // add numbers divisible by 7 + for ($x = $numberPool; $x >= 0; $x--) { + if ($x % 7 === 0) { + $myArray[] = $x; + } + } + + return $myArray; +} \ No newline at end of file diff --git a/bubble-sort-v2-http.js b/bubble-sort-v2-http.js new file mode 100644 index 0000000..66fb86c --- /dev/null +++ b/bubble-sort-v2-http.js @@ -0,0 +1,18 @@ +const bubbleSort = require('./bubble-sort-core').bubbleSort; +const buildArray = require('./bubble-sort-core').buildArray; +const http = require('http'); +const url = require('url'); + +http.createServer(async function (req, res) { + const query = url.parse(req.url,true).query; + const numberPool = parseInt(query.num); + const myArray = buildArray(numberPool); + + const startTime = process.hrtime.bigint(); + bubbleSort(myArray); + const endTime = process.hrtime.bigint(); + + const totalTime = Number(endTime - startTime) / 1000000; + res.write(`[V8] array contains ${myArray.length} elements, execution time: ${totalTime} ms`); + res.end(); +}).listen(8080); \ No newline at end of file diff --git a/bubble-sort-v2-http.php b/bubble-sort-v2-http.php new file mode 100755 index 0000000..eeadf63 --- /dev/null +++ b/bubble-sort-v2-http.php @@ -0,0 +1,15 @@ + { - const len = a.length; +const bubbleSort = require('./bubble-sort-core').bubbleSort; +const buildArray = require('./bubble-sort-core').buildArray; - let sorted = false; - while(!sorted) { - sorted = true; - for (let i = 0; i < len; i++) { - let current = a[i]; - let next = a[i + 1]; - - if(next < current) { - a[i] = next; - a[i + 1] = current; - sorted = false; - } - } - } -}; - -const myArray = []; const numberPool = 4096; -// add numbers divisible by 2 -for (let x = numberPool; x >= 0; x--) { - if (x % 2 === 0) { - myArray.push(x); - } -} - -// add numbers divisible by 3 -for (let x = numberPool; x >= 0; x--) { - if (x % 3 === 0) { - myArray.push(x); - } -} - -// add numbers divisible by 7 -for (let x = numberPool; x >= 0; x--) { - if (x % 7 === 0) { - myArray.push(x); - } -} - +const myArray = buildArray(numberPool); console.log(); const startTime = process.hrtime.bigint(); diff --git a/bubble-sort-v2.php b/bubble-sort-v2.php index bc8423c..421e482 100755 --- a/bubble-sort-v2.php +++ b/bubble-sort-v2.php @@ -1,45 +1,10 @@ = 0; $x--) { - if ($x % 2 === 0) { - $myArray[] = $x; - } -} - -// add numbers divisible by 3 -for ($x = $numberPool; $x >= 0; $x--) { - if ($x % 3 === 0) { - $myArray[] = $x; - } -} - -// add numbers divisible by 7 -for ($x = $numberPool; $x >= 0; $x--) { - if ($x % 7 === 0) { - $myArray[] = $x; - } -} +$myArray = buildArray($numberPool); $startTime = hrtime(true); bubbleSort($myArray); diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6a8309a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3' +services: + php: + network_mode: host + build: + context: ./ + dockerfile: Dockerfilephp + volumes: + - $PWD:/var/www/html + container_name: phpsort + node: + + build: + context: ./ + dockerfile: Dockerfilenode + network_mode: host + container_name: nodesort \ No newline at end of file