scvsoft / challenge-accepted

Juego que presentamos en la Rubyconf 2013
http://scvsoft.com/challenge-accepted
0 stars 0 forks source link

first attempt at running js solutions with phantomjs #28

Closed tepen closed 10 years ago

tepen commented 10 years ago

Testing running js solutions with Phantomjs. It is quite slow as is, tests for challenge 1 are taking 7 seconds in my machine.

leoasis commented 10 years ago

Is it slow because of Phantom startup? We should investigate alternatives for this, or perhaps have it preloaded somewhere. I wish Node already had something for this.

tepen commented 10 years ago

It is slow for the Fibo test as compared to the ruby version (7 seconds against 1). We can test with the math version but we have to take into account that users will probably use the recursive solution for this challenge. I am going to test some other challenge in js to compare with the Ruby version. Do you think a 10 second timeout will be something we can deal with for the challenges?

tepen commented 10 years ago

I added the math fibo solution and running the tests for the first challenge takes about 3.5 seconds with this solution.

leoasis commented 10 years ago

Hey guys, I've been playing a little bit with node's vm module, and here I found this:

http://nodejs.org/api/vm.html#vm_vm_runinnewcontext_code_sandbox_filename

It basically allows you to execute javascript in a sandbox. You can define the global object to set the code to, so you can just do this for example:

var util = require('util'),
    vm = require('vm'),
    sandbox = {
      console: {
        log: console.log
      },
      process: {
        argv: process.argv
      }
    };

var code = 'console.log(process.argv);throw new Error();';
vm.runInNewContext(code, sandbox, 'fakefile.js');

This sets the global object to a literal with only the console.log function (to output) and the process.argv array. No other thing is exposed, not even require. Also, I put there a fake file name so that the errors show that were caused in a fake file, just as an example.

I think this has enough security to handle what we need, what do you think?

tepen commented 10 years ago

Sounds good! It should not be too restrictive since the exercises we're doing don't need anything fancy. I'll give it a try and update this pull request.

tepen commented 10 years ago

This is working with node now, it's definitely faster here. Please check on executor how I call the node_sandbox.js file, not entirely sure if this can be done in a tidier way.

leoasis commented 10 years ago

:+1: Nice job!