jrdnmdhl jrdnmdhl - 18 days ago 5
R Question

Opencpu call takes longer than expected

I'm trying to call a function from the flexsurv package using opencpu. I am running on OSX Sierra.

I have modified my opencpu .conf file to include flexsurv in the list of preloaded packages:

{
"enable.api.library": true,
"enable.api.apps": true,
"enable.api.bioc": true,
"enable.api.cran": true,
"enable.api.gist": true,
"enable.api.github": true,
"enable.api.user": true,
"enable.api.tmp": true,
"enable.cors" : true,
"enable.post.code": true,
"error.showcall": true,
"gist.cache": 300,
"github.cache": 86400,
"cran.cache": 86400,
"public.url" : "https://public.opencpu.org/ocpu",
"smtp.server" : "localhost",
"httpcache.post": 300,
"httpcache.lib": 86400,
"httpcache.git": 900,
"httpcache.gitapi": 120,
"httpcache.tmp": 86400,
"httpcache.cran": 86400,
"httpcache.static": 31536000,
"httpcache.bioc": 31536000,
"key.length" : 9,
"appspaths": "/usr/local/lib/opencpu/apps-library",
"repos": "http://cran.rstudio.com",
"rlimit.as": 2e9,
"rlimit.fsize": 1e8,
"rlimit.nproc": 50,
"timelimit.get": 600,
"timelimit.post": 90,
"timelimit.webhook": 900,
"preload": ["ggplot2", "lattice","survival","flexsurv"]


}

I then start open cpu and make the following call in curl:

curl localhost:7210/ocpu/library/flexsurv/R/pgompertz/json -d "q=[0,1,2,3]&shape=0.001&rate=0.001" -w "%{time_total}\n" -o /dev/null


This call takes about 1.4 seconds, despite being an extremely simple and fast function call that should take no time at all.

By comparison, calling rnorm to draw 10,000 random normal samples returns in less than half a second:

curl localhost:7210/ocpu/library/stats/R/rnorm/json -d "n=100000" -w "%{time_total}\n" -o /dev/null


Why does the comparatively simple flexsurv call take so much more time? Is preload not working? If so, how can I fix this? If not, what is the causing the additional latency?

Any help is much appreciated...

Answer

Problem was solved by using cloud version of opencpu which is sees much larger benefits from preloading.

Steps:

  1. Install docker for mac from https://docs.docker.com/engine/installation/mac/

  2. Install opencpu docker container docker pull opencpu/base

  3. Run docker w/ terminal session docker run -t -i -p 80:80 -p 8004:8004 opencpu/base sh -c 'service opencpu restart && /bin/bash'
  4. Call R from docker container, install packages as necessary
  5. Use apt-get to install text editor of choice to docker container (e.g. sudo apt-get nano)
  6. Use text editor to edit /etc/opencpu/server.conf and add necessary packages to preload.
  7. Restart docker and launch docker run -t -p 80:80 -p 8004:8004 opencpu/base
  8. Opencpu now up and running and calls to preloaded packages will return much faster.

Credit to answer belongs to opencpu developer Jeroen Ooms who assisted me on the google group: https://groups.google.com/forum/#!topic/opencpu/D49Adx6GSiE