Skip to content
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

Add node-example #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Replaced plotly with matplotlib
  • Loading branch information
SergiyIvan committed Dec 2, 2019
commit cd75a37294122c2b79cf8d7fc93bd6ac2b5a8919
12 changes: 2 additions & 10 deletions node-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ git clone https://github.com/graalvm/graalvm-demos
cd graalvm-demos/node-example
```

Build the application before running. You can manually execute `npm install plotly`, but there's also a `build.sh` script included for your convenience.
Build the application before running. The application requires python libraries for plotting the graph. You can manually execute all the commands from `build.sh` file line-by-line, but that is more convenient to execute `build.sh` script as it is shown below.

Execute:
```
Expand Down Expand Up @@ -52,12 +52,4 @@ This will perform 50 requests to the server and print results of benchmarking.

## Notes about the application

This application uses [Plotly](https://plot.ly) library to plot the graph. Plotly provides API for plotting graphs, and requires username and API key indicated.

To get username and API key, proceed to the [Plotly](https://chart-studio.plot.ly/settings/api#/) site, log in or sign up, go to Settings->API Keys and click on `Regenerate Key` button. Then copy your username and API key and paste them there, on the top of the `taylor.js` file:

```
const plotly = require('plotly')('username', 'api_key');
```

This API key can be used 1000 times per 24h.
This application uses [matplotnode](https://www.npmjs.com/package/matplotnode) package to plot the graph. This package requires Python 2.7 and matplotlib installed. Scripts to install them and their dependencies are in `build.sh` file.
9 changes: 7 additions & 2 deletions node-example/build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#!/usr/bin/env bash

npm install plotly
sudo apt install python-dev
sudo apt-get build-dep python-matplotlib
sudo apt-get install python-pip
sudo apt-get install python-tk
python -m pip install -U pip
sudo python -m pip install -U matplotlib
npm install matplotnode
48 changes: 14 additions & 34 deletions node-example/taylor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const plotly = require('plotly')('username', 'api_key');
const http = require('http');
const plt = require('matplotnode');
const fs = require('fs');

const file = './plot.png';

//-----------------------Creating-server------------------------------

const server = http.createServer((req, res) => {
plt.close('all');
outputSinGraph(res);
});

Expand Down Expand Up @@ -53,47 +57,23 @@ function outputSinGraph(res) {
return d;
}

//-----------------------Plotting-graph-------------------------------

var layout = {
title: {
text: `Evaluations took ${time} nanoseconds.`,
font: {
family: 'Courier New, monospace',
size: 24
},
xref: 'paper',
x: 0.05,
}
}
//-----------------------Plotting-graph-------------------------------

var trace1 = {
x: data.x,
y: data.y,
type: "scatter"
};
plt.title(`Evaluations took ${time} nanoseconds.`);
plt.plot(data.x, data.y, 'color=b', 'label=sin(x)');
plt.legend();

var figure = {
'data': [trace1],
'layout' : layout
};
plt.save(file);

var imgOpts = {
format: 'png',
width: 1000,
height: 500
};

plotly.getImage(figure, imgOpts, function (error, imageStream) {
if (error) return console.log (error);
res.writeHead(200, {'Content-Type': 'image/png'});
imageStream.pipe(res);
});
let imageStream = fs.createReadStream(file);
res.writeHead(200, {'Content-Type': 'image/png'});
imageStream.pipe(res);
}

//-----------------------Time-measurement-----------------------------

function getNanoSecTime() {
var hrTime = process.hrtime();
let hrTime = process.hrtime();
return hrTime[0] * 1000000000 + hrTime[1];
}