Skip to content

Add web server tests #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.vscode
12 changes: 12 additions & 0 deletions DockerConfig/sorttest.com.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName localhost
ServerAlias sorttest.com
DocumentRoot "/var/www/html"

<Directory /var/www/html>
AllowOverride None
Order Allow,Deny
Allow from All
</Directory>
</VirtualHost>
5 changes: 5 additions & 0 deletions Dockerfilenode
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM node:12-alpine
WORKDIR /var/www/html
COPY . .
EXPOSE 80
CMD [ "node", "bubble-sort-v2-http" ]
14 changes: 14 additions & 0 deletions Dockerfilephp
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
47 changes: 47 additions & 0 deletions bubble-sort-core.js
Original file line number Diff line number Diff line change
@@ -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;
51 changes: 51 additions & 0 deletions bubble-sort-core.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

function bubbleSort(array &$a)
{
$len = count($a) - 1;
$sorted = false;
while (!$sorted) {
$sorted = true;
for ($i = 0; $i < $len; $i++) {
$current = $a[$i];
$next = $a[$i + 1];
if ( $next < $current ) {
$a[$i] = $next;
$a[$i + 1] = $current;
$sorted = false;
}
}
}
}

/**
* Builds an array to be used in sort.
*
* @param int $numberPool
* @return array
*/
function buildArray(int $numberPool)
{
// add numbers divisible by 2
for ($x = $numberPool; $x >= 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;
}
18 changes: 18 additions & 0 deletions bubble-sort-v2-http.js
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 15 additions & 0 deletions bubble-sort-v2-http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require_once './bubble-sort-core.php';

$numberPool = (int) $_REQUEST['num'];
$myArray = buildArray($numberPool);

$startTime = hrtime(true);
bubbleSort($myArray);
$endTime = hrtime(true);

echo '[PHP] array contains ', count($myArray), ' elements, execution time: ',
($endTime - $startTime) / 1000000, ' ms', PHP_EOL;

// echo print_r($myArray, true), PHP_EOL;
43 changes: 3 additions & 40 deletions bubble-sort-v2.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
39 changes: 2 additions & 37 deletions bubble-sort-v2.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,10 @@
<?php
function bubbleSort(array &$a)
{
$len = count($a) - 1;
$sorted = false;
while (!$sorted) {
$sorted = true;
for ($i = 0; $i < $len; $i++) {
$current = $a[$i];
$next = $a[$i + 1];
if ( $next < $current ) {
$a[$i] = $next;
$a[$i + 1] = $current;
$sorted = false;
}
}
}
}
require_once './bubble-sort-core.php';

$myArray = [];
$numberPool = 4096;

// add numbers divisible by 2
for ($x = $numberPool; $x >= 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);
Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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