asg017/observable-prerender

Feature request/idea - server compute...

CobusT opened this issue · 2 comments

I am now very curious about whether you can imagine using this to offload computing to a server instead of your own computer... since it is headless Chrome, conceivably, this could be a way to have a notebook run remotely...
Just a thought.

lol yeah, I was actually just working on some flags for this. Puppeteer can connect to remote headless browsers using a browserURL or browserWSEndpoint. If you're using the observable-prerender Node APIs, you can do something like:

const browser = await puppteer.connect({
  browserWSEndpoint: "ws://127.0.0.1:64556/devtools/browser/ca689dca-ac6d7sc6-aucy8ac"
})
const notebook = await load("@d3/bar-chart", "chart", {browser})

where the browserWSEndpoint can be found from a remote puppeteer instance with console.log(browser.wsEndpoint()). Now, all the operations will occur on the remote instance rather than your local computer, which should be much faster than launching/tearing down your own instance every time.

I'll be adding some new CLI flags (--browser-wsendpoint and others) that will make this much easier to do w/ the observable-prerender CLI

kk so v0.4.2 now has this new flag, you can upgrade with

npm i -g @alex.garcia/observable-prerender@v0.4.1

I did a quick benchmark with hyperfine, results are below. #1 remotely connects a headless browser, #2 remotely connects to a non-headless browser, and #3 launches a new browser everytime. --browser-wsendpoint is much faster, especially on headless (380ms) vs non-headless (830ms), but both are much faster than launching a new instance every time (1.128s).

Benchmark #1: ./bin/observable-prerender @d3/pie-chart chart -o /tmp/chart --browser-wsendpoint ws://127.0.0.1:63978/devtools/browser/ec404914-74ee-4833-b1da-61fa220be226
  Time (mean ± σ):     386.9 ms ± 295.9 ms    [User: 140.6 ms, System: 22.8 ms]
  Range (min … max):   273.6 ms … 1227.0 ms    10 runs
 
 
Benchmark #2: ./bin/observable-prerender @d3/pie-chart chart -o /tmp/chart --browser-wsendpoint ws://127.0.0.1:63032/devtools/browser/d9eeb9d8-342a-4e5c-91b5-8467cb549455
  Time (mean ± σ):     836.1 ms ±  73.4 ms    [User: 152.7 ms, System: 26.9 ms]
  Range (min … max):   778.1 ms … 976.9 ms    10 runs
 
 
Benchmark #3: time ./bin/observable-prerender @d3/pie-chart chart -o /tmp/chart
  Time (mean ± σ):      1.128 s ±  0.064 s    [User: 217.0 ms, System: 59.2 ms]
  Range (min … max):    1.068 s …  1.287 s    10 runs
 
Summary
  './bin/observable-prerender @d3/pie-chart chart -o /tmp/chart --browser-wsendpoint ws://127.0.0.1:63978/devtools/browser/ec404914-74ee-4833-b1da-61fa220be226' ran
    2.16 ± 1.66 times faster than './bin/observable-prerender @d3/pie-chart chart -o /tmp/chart --browser-wsendpoint ws://127.0.0.1:63032/devtools/browser/d9eeb9d8-342a-4e5c-91b5-8467cb549455'
    2.91 ± 2.24 times faster than 'time ./bin/observable-prerender @d3/pie-chart chart -o /tmp/chart'