Created
January 19, 2015 08:25
-
-
Save itang/75f3931455edf6b1c585 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.test; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.Callable; | |
import org.jruby.embed.LocalContextScope; | |
import org.jruby.embed.ScriptingContainer; | |
/* | |
$ mkdir gems | |
$ cd gems | |
$ java -jar /home/itang/.rvm/rubies/jruby-1.7.18/lib/jruby.jar -S gem install -i redis redis --no-rdoc --no-ri | |
*/ | |
public class App { | |
public static void main(String[] args) { | |
time("main", () -> { | |
App app = new App(); | |
app.init(); | |
app.test(); | |
return null; | |
}); | |
} | |
public void test() { | |
time("require", () -> require("redis")); | |
Object ret = time("script1:", () -> runScript("redis=Redis.new; redis.get 'a'")); | |
println(ret); | |
ret = time("script2:", () -> runScript("redis=Redis.new; redis.get 'a'")); | |
println(ret); | |
ret = time("script3:", () -> runScript("redis=Redis.new; redis.get 'a'")); | |
System.out.println(ret); | |
ret = time("script4:", () -> runScript("redis=Redis.new; redis.get 'a'")); | |
System.out.println(ret); | |
} | |
private ScriptingContainer rubyContainer; | |
private Object require(String gem) { | |
return this.rubyContainer.runScriptlet("require '" + gem + "'"); | |
} | |
private Object runScript(String script) { | |
return this.rubyContainer.runScriptlet(script); | |
} | |
private static Object time(String title, Callable<?> run) { | |
try { | |
long start = System.currentTimeMillis(); | |
Object ret = run.call(); | |
long end = System.currentTimeMillis(); | |
System.out.println(String.format("%s: %f", title, (end - start) / 1000.0)); | |
return ret; | |
} catch (Exception e) { | |
throw new RuntimeException("error", e); | |
} | |
} | |
private static void println(Object s) { | |
System.out.println(s); | |
} | |
private void init() { | |
List<String> paths = new ArrayList<>(); | |
paths.add("gems/redis/gems/redis-3.2.0/lib"); | |
this.rubyContainer = new ScriptingContainer(LocalContextScope.CONCURRENT); | |
this.rubyContainer.setLoadPaths(paths); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment