Skip to content

Commit

Permalink
Merge pull request #52 from mcneel/luis/warm
Browse files Browse the repository at this point in the history
Luis/warm
  • Loading branch information
fraguada authored Oct 7, 2020
2 parents 4572056 + dc8c392 commit 20db92d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 11 deletions.
16 changes: 10 additions & 6 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
let data = {}
data.definition = 'BranchNodeRnd.gh'
data.inputs = {
'Length':document.getElementById('length').value,
'Count':document.getElementById('count').value,
'Radius':document.getElementById('radius').value
'Count':document.getElementById('count').valueAsNumber,
'Radius':document.getElementById('radius').valueAsNumber,
'Length':document.getElementById('length').valueAsNumber
}

// set this to the target appserver url
Expand All @@ -30,11 +30,15 @@ async function compute(){
let t0 = performance.now()
const timeComputeStart = t0

console.log(data.inputs)

const request = {
'method':'POST',
'body': JSON.stringify(data),
'headers': {'Content-Type': 'application/json'}
}


let response = await fetch(url, request)

// Request finished. Do processing here.
Expand Down Expand Up @@ -95,9 +99,9 @@ function onSliderChange () {

// get slider values
data.inputs = {
'Length':document.getElementById('length').value,
'Count':document.getElementById('count').value,
'Radius':document.getElementById('radius').value
'Count':document.getElementById('count').valueAsNumber,
'Radius':document.getElementById('radius').valueAsNumber,
'Length':document.getElementById('length').valueAsNumber
}
compute()
}
Expand Down
6 changes: 3 additions & 3 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
<div id="container">
<div id="overlay">
<div>
<input type="range" id="count" name="count" min="1" max="100" value="100" step="1" onmouseup="onSliderChange()" ontouchend="onSliderChange()">
<input type="range" id="count" name="count" min="0" max="100" value="100" step="10" onmouseup="onSliderChange()" ontouchend="onSliderChange()">
<label for="count">Count</label>
</div>
<div>
<input type="range" id="radius" name="radius" min="0" max="10.00" value="5.00" step="0.1" onmouseup="onSliderChange()" ontouchend="onSliderChange()">
<input type="range" id="radius" name="radius" min="1" max="10.00" value="5" step="1" onmouseup="onSliderChange()" ontouchend="onSliderChange()">
<label for="radius">Radius</label>
</div>
<div>
<input type="range" id="length" name="length" min="0" max="10.00" value="3.50" step="0.1" onmouseup="onSliderChange()" ontouchend="onSliderChange()">
<input type="range" id="length" name="length" min="1" max="10.00" value="3" step="1" onmouseup="onSliderChange()" ontouchend="onSliderChange()">
<label for="length">Length</label>
</div>
</div>
Expand Down
112 changes: 112 additions & 0 deletions example/warm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<html>
<head>
<meta charset="utf-8">
<link rel="apple-touch-icon" sizes="180x180" href="favicon/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon/favicon-16x16.png">
<link rel="manifest" href="favicon/site.webmanifest">
<title>Compute RestHopper Test</title>
</head>
<body>
<button id="solveButton" name="button" onclick="run()">Press me</button>
<div id="log" style="font-family: sans-serif;"></div>
<script>

let logDiv = document.getElementById('log')
let total = 0
let params = {}

init()

function init() {

// set this to the target appserver url
let url = window.location.href
url = url.substring(0, url.lastIndexOf('/'))
url = url.substring(0, url.lastIndexOf('/')) + '/solve'

params.definition = 'BranchNodeRnd.gh'
params.url = url

// The min, max, and step values below should match the values used for the input sliders in index.html

//count
params.count = {}
params.count.min = 0
params.count.max = 100
params.count.step = 10
params.count.cnt = ( params.count.max - params.count.min ) / params.count.step

//radius
params.radius = {}
params.radius.min = 1
params.radius.max = 10
params.radius.step = 1
params.radius.cnt = ( params.radius.max - params.radius.min ) / params.radius.step

//length
params.length = {}
params.length.min = 1
params.length.max = 10
params.length.step = 1
params.length.cnt = ( params.length.max - params.length.min ) / params.length.step

total = ( ( params.count.cnt + 1 ) * ( params.radius.cnt + 1 ) * ( params.length.cnt + 1 ) )

logDiv.textContent = 'Solutions to solve: ' + total

}

function run() {

document.getElementById('solveButton').disabled = true

warmUp().then( ()=>{
console.log('done')
logDiv.textContent = 'done'
} )
}

async function warmUp() {

let cnt = 1

for (let i = params.count.min; i <= params.count.max; i = i + params.count.step) {

for (let j = params.radius.min; j <= params.radius.max; j = j + params.radius.step) {

for (let k = params.length.min; k <= params.length.max; k = k + params.length.step) {

console.log(`Solution: ${cnt} / ${total}`)
logDiv.textContent = `Solution: ${cnt} / ${total}`
console.log(`Params: Count ${i} - Radius ${j} - Length ${k}`)

let data = {}
data.definition = params.definition
data.inputs = {
'Count': i,
'Radius': j,
'Length': k
}

const request = {
'method':'POST',
'body': JSON.stringify(data),
'headers': {'Content-Type': 'application/json'}
}

let response = await fetch(params.url, request)
let responseJson = await response.json()

cnt ++

}

}

}

}
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mcneel/compute.rhino3d.appserver",
"version": "0.1.8",
"version": "0.1.9",
"keywords": [
"node.js",
"expressjs",
Expand Down
9 changes: 8 additions & 1 deletion routes/solve.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,21 @@ function collectParams (req, res, next){

function checkCache (req, res, next){

res.locals.cacheKey = JSON.stringify(res.locals.params)
const key = {}
key.definition = { 'name': res.locals.params.definition.name, 'id': res.locals.params.definition.id }
key.inputs = res.locals.params.inputs
res.locals.cacheKey = JSON.stringify(key)
res.locals.cacheResult = null

if(mc === null){
// use node cache
//console.log('using node-cache')
const result = cache.get(res.locals.cacheKey)
res.locals.cacheResult = result !== undefined ? result : null
next()
} else {
// use memcached
//console.log('using memcached')
if(mc !== null) {
mc.get(res.locals.cacheKey, function(err, val) {
if(err == null) {
Expand Down Expand Up @@ -104,12 +109,14 @@ function commonSolve (req, res, next){

if(res.locals.cacheResult !== null) {
//send
//console.log(res.locals.cacheResult)
const timespanPost = Math.round(performance.now() - timePostStart)
res.setHeader('Server-Timing', `cacheHit;dur=${timespanPost}`)
res.send(res.locals.cacheResult)
return
} else {
//solve
//console.log('solving')
// set parameters
let trees = []
if(res.locals.params.inputs !== undefined) { //TODO: handle no inputs
Expand Down

0 comments on commit 20db92d

Please sign in to comment.