Skip to content

Commit

Permalink
[OZ-0008] initial code for cache testing
Browse files Browse the repository at this point in the history
  • Loading branch information
zaknuces committed May 22, 2017
1 parent b641437 commit 6c3adee
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 6 deletions.
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ app
.use(morgan('combined', {
stream: accessLogStream
}))
//.use(express.static('public', { maxAge: 86400000 /* 1d */ }))
.use(express.static('public'))
.use('/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist/'))
.use('/jquery', express.static(__dirname + '/node_modules/jquery/dist/'))
Expand All @@ -62,4 +63,49 @@ app
.get('/quote', (req, res) => {
randomQuote()
.then(quote => res.json(quote));
})

.get('/travelLogs', (req, res) => {
console.log(req.query);
if (req.query && req.query.serverPush !== undefined && res.push) {
console.log('Serve Push resources');
let imageFilePath = path.join(__dirname, '/public', '/images/porto1.jpg');
res
.push('/images/porto1.jpg', {})
.end(fs.readFileSync(imageFilePath));

imageFilePath = path.join(__dirname, '/public', '/images/porto2.jpg');
res
.push('/images/porto2.jpg', {})
.end(fs.readFileSync(imageFilePath));

imageFilePath = path.join(__dirname, '/public', '/images/porto3.jpg');
res
.push('/images/porto3.jpg', {})
.end(fs.readFileSync(imageFilePath));

imageFilePath = path.join(__dirname, '/public', '/images/porto4.jpg');
res
.push('/images/porto4.jpg', {})
.end(fs.readFileSync(imageFilePath));

imageFilePath = path.join(__dirname, '/public', '/images/porto5.jpg');
res
.push('/images/porto5.jpg', {})
.end(fs.readFileSync(imageFilePath));
}
res.json([{
details: "Trip to Porto, Protugal",
images: [{
url: 'images/porto1.jpg'
}, {
url: 'images/porto2.jpg'
}, {
url: 'images/porto3.jpg'
}, {
url: 'images/porto4.jpg'
}, {
url: 'images/porto5.jpg'
}]
}])
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"bootstrap": "^3.3.7",
"express": "^5.0.0-alpha.5",
"express-http2-workaround": "^1.1.2",
"http2": "^3.3.6",
"http2": "https://github.com/node-apn/node-http2#apn-2.1.4",
"jquery": "^3.2.1",
"morgan": "^1.8.1",
"random-quote": "0.0.1"
Expand Down
74 changes: 74 additions & 0 deletions public/cacheTest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap-theme.css">

<link rel="stylesheet" type="text/css" href="css/theme.css">

<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>

<script type="text/javascript" src="js/TravelLogs.js"></script>
<script>
// TODO: documentation.
function setUp() {
var application = new TravelLogs();
$('#submitBtn').on('click', application.clickEventHandler.bind(application));
}
</script>
</head>
<body onload="setUp()">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand glyphicon glyphicon-home" href="/">Home</a>
</div>
</div>
</nav>
<div class="page-header">
<h1>1: Caching Test</h1>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<!-- Default panel contents -->
<div class="panel-heading">Choose Test Scenario</div>
<div class="panel-body">
<input type="radio" name="quantity" value="1" checked> Fetch <br>
<input type="radio" name="quantity" value="2" checked> Server Push <br>
<button id="submitBtn" class="btn btn-default" type="submit">Fetch Quotations</button>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-10">
<div class="panel panel-info">
<!-- Default panel contents -->
<div class="panel-heading">Travel Blog:</div>
<div id="travel-log" class="list-group">
</div>
</div>
</div>
</div>
</div>
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container-fluid">
<span class="navbar-text"> Page Statistics : </span>
<span id="loadtimes" class="navbar-text"></span>
</div>
</nav>
</body>
</html>
Binary file added public/images/porto1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/porto2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/porto3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/porto4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/porto5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/js/Application.js → public/js/Quote.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: documentation.
class Application {
class Quote {
constructor() {}

clickEventHandler(event) {
Expand Down
52 changes: 52 additions & 0 deletions public/js/TravelLogs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// TODO: documentation.
class TravelLogs {
constructor() {}

clickEventHandler(event) {
this.startTime = new Date().getTime();
//this.count = 0;
this.option = parseInt($('input[name=quantity]:checked').get(0).value);
//for (let i = 0; i < this.option; i++) {
this.fetch(this.option === 2 ? "?serverPush": "");
//}
}

fetch(queryParam) {
$("#travel-log").html('');
$.get( `/travelLogs${queryParam}`, (data) => {
this.createTemplate(data);
this.checkTime();
});
}

checkTime() {
//this.count++;

//if (this.count === this.option) {
let endDate = new Date().getTime();
let diff = (endDate - this.startTime) / 1000;
$('#loadtimes').html(`Total Time ~ ${diff} seconds`);
//}
}

createTemplate(data) {
data.forEach(travelLog => {
let images = '';
travelLog.images.forEach(image => {
images += `
<div class="col-md-4">
<div class="thumbnail">
<img src="${image.url}">
</div>
</div>
`;
})
$("#travel-log").append(`
<div>${travelLog.details}</div>
<div class="row">
${images}
</div>
`);
})
}
};
6 changes: 3 additions & 3 deletions public/loadTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>

<script type="text/javascript" src="js/Application.js"></script>
<script type="text/javascript" src="js/Quote.js"></script>
<script>
// TODO: documentation.
function setUp() {
var application = new Application();
$('#submitBtn').on('click', application.clickEventHandler.bind(application));
var quoteDataModel = new Quote();
$('#submitBtn').on('click', application.clickEventHandler.bind(quoteDataModel));
}
</script>
</head>
Expand Down
2 changes: 1 addition & 1 deletion public/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h1>High Performance Web Apps Using Http 2 <small>by Owais Zahid</small></h1>
<div class="panel-heading">Test Scenarios</div>
<div class="list-group">
<a href="/loadTest.html" class="list-group-item">Load Test</a>
<a href="https://http2.golang.org/serverpush" class="list-group-item">Server Push</a>
<a href="/cacheTest.html" class="list-group-item">Server Push</a>
</div>
</div>
</div>
Expand Down

0 comments on commit 6c3adee

Please sign in to comment.