From 907c39ecf76ebc9ddccfd941212cb65a0657a4a6 Mon Sep 17 00:00:00 2001 From: buphmin Date: Mon, 27 May 2019 14:41:52 -0600 Subject: [PATCH 1/9] moved sort to file, added buildArray --- bubble-sort-core.js | 47 ++++++++++++++++++++++++++++++++++++++++ bubble-sort-core.php | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 bubble-sort-core.js create mode 100644 bubble-sort-core.php 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 From 7878e02b089fc0b97b403141445065fc7ecd0125 Mon Sep 17 00:00:00 2001 From: buphmin Date: Mon, 27 May 2019 14:42:24 -0600 Subject: [PATCH 2/9] moved sort to file, added buildArray --- bubble-sort-v2.php | 39 ++------------------------------------- 1 file changed, 2 insertions(+), 37 deletions(-) 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); From 6e014ce98ff4451dbb6f0b60e87ca974f2f0627b Mon Sep 17 00:00:00 2001 From: buphmin Date: Mon, 27 May 2019 14:42:30 -0600 Subject: [PATCH 3/9] moved sort to file, added buildArray --- bubble-sort-v2.js | 43 +++---------------------------------------- 1 file changed, 3 insertions(+), 40 deletions(-) diff --git a/bubble-sort-v2.js b/bubble-sort-v2.js index 3450063..b2cfea5 100755 --- a/bubble-sort-v2.js +++ b/bubble-sort-v2.js @@ -1,46 +1,9 @@ -const bubbleSort = (a) => { - 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(); From dc801c30570b2e13a83d82ec6a838fe6eba7f23c Mon Sep 17 00:00:00 2001 From: buphmin Date: Mon, 27 May 2019 14:43:07 -0600 Subject: [PATCH 4/9] created web server versions --- bubble-sort-v2-http.js | 18 ++++++++++++++++++ bubble-sort-v2-http.php | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 bubble-sort-v2-http.js create mode 100755 bubble-sort-v2-http.php 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 @@ + Date: Mon, 27 May 2019 14:43:34 -0600 Subject: [PATCH 5/9] added docker config to run in webservers --- .gitignore | 2 ++ DockerConfig/sorttest.com.conf | 12 ++++++++++++ Dockerfilenode | 5 +++++ Dockerfilephp | 14 ++++++++++++++ docker-compose.yml | 17 +++++++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 .gitignore create mode 100644 DockerConfig/sorttest.com.conf create mode 100644 Dockerfilenode create mode 100644 Dockerfilephp create mode 100644 docker-compose.yml 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..2df4bff --- /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/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 From bbbddc5e9d389f6e66080edc57f44e3623ec085c Mon Sep 17 00:00:00 2001 From: buphmin Date: Mon, 27 May 2019 14:52:02 -0600 Subject: [PATCH 6/9] added note for running test as a webserver. --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index e997bab..7f46600 100755 --- a/README.md +++ b/README.md @@ -25,4 +25,27 @@ 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. + +```bash +docker-compose up +``` + +Open browser and go to each url. + +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 From 97904f0fa82abb5d027786e64448ed6d28c46076 Mon Sep 17 00:00:00 2001 From: buphmin Date: Mon, 27 May 2019 14:54:19 -0600 Subject: [PATCH 7/9] added note for adjusting param --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7f46600..115cea0 100755 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ whole picture. docker-compose up ``` -Open browser and go to each url. +Open browser and go to each url. Feel free to change the "num" param to suit various tests. For PHP: From 9ca5e169ee8785aaa419aeba476ef80ca18a84ca Mon Sep 17 00:00:00 2001 From: buphmin Date: Mon, 27 May 2019 14:57:32 -0600 Subject: [PATCH 8/9] expanded on web server summary. --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 115cea0..6aeb642 100755 --- a/README.md +++ b/README.md @@ -29,7 +29,10 @@ 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. +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 @@ -48,4 +51,5 @@ For Node.js: http://127.0.0.1:8080/?num=4096 ``` + Thanks! \ No newline at end of file From 456c6a7c0b8d9e5da901846efb53d24ff370eae0 Mon Sep 17 00:00:00 2001 From: buphmin Date: Mon, 27 May 2019 15:05:10 -0600 Subject: [PATCH 9/9] fixed conf --- DockerConfig/sorttest.com.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DockerConfig/sorttest.com.conf b/DockerConfig/sorttest.com.conf index 2df4bff..caef262 100644 --- a/DockerConfig/sorttest.com.conf +++ b/DockerConfig/sorttest.com.conf @@ -4,7 +4,7 @@ ServerAlias sorttest.com DocumentRoot "/var/www/html" - + AllowOverride None Order Allow,Deny Allow from All