Permalink
Browse files

added timers

  • Loading branch information...
1 parent 9a2c2d6 commit 24b5b3cad1ab871d1e7d64599375826e6a539eae @fernandezpablo85 committed Nov 24, 2011
View
55 src/main/scala/com/linkedin/frameworks/Dust.scala
@@ -7,29 +7,19 @@ import java.io._
class Dust
{
val context = Context.enter
- def render (template : InputStream, data : InputStream) =
+ val scope = context.initStandardObjects
+ loadDust(scope)
+
+ def compile (template : InputStream)
{
val templateString = getString(template)
- val dataString = getString(data)
- val scope = context.initStandardObjects()
-
- val dust = classOf[Dust].getResourceAsStream("dust.js")
- context.evaluateString(scope, getString(dust), "<dust-loading>", 1, null)
- val script = """
- var compiled = dust.compile("%s", "%s");
- dust.loadSource(compiled);
- """.format(templateString, "name")
- context.evaluateString(scope, script, "<template-loading>", 2, null);
+ loadTemplate(templateString)
+ }
- val script2 = """
- var result = 'not finished';
- dust.render("%s", %s, function(err, out) {
- result = out;
- });
- result;
- """.format("name", dataString)
- val rendered = context.evaluateString(scope, script2, "<template-rendering>", 2, null)
-
+ def render (data : InputStream, template : String = "template") =
+ {
+ val dataString = getString(data)
+ val rendered = renderTemplate(dataString, template)
rendered.toString
}
@@ -45,22 +35,33 @@ class Dust
private def loadDust (scope : Scriptable) =
{
-
+ val dust = classOf[Dust].getResourceAsStream("dust.js")
+ context.evaluateString(scope, getString(dust), "<dust-loading>", 1, null)
}
- private def loadTemplate(scope : Scriptable, template : String, name : String = "template")
+ private def loadTemplate(template : String, name : String = "template")
{
-
+ val script = """
+ var compiled = dust.compile("%s", "%s");
+ dust.loadSource(compiled);
+ """.format(template, name)
+ context.evaluateString(scope, script, "<template-loading>", 2, null);
}
- private def renderTemplate(scope : Scriptable, data : String, name : String = "template") =
+ private def renderTemplate(data : String, name : String) =
{
-
+ val script2 = """
+ var result = 'not finished';
+ dust.render("%s", %s, function(err, out) {
+ result = out;
+ });
+ result;
+ """.format(name, data)
+ context.evaluateString(scope, script2, "<template-rendering>", 2, null)
}
}
object Dust
{
- def render (template : InputStream, data : InputStream) = new Dust().render(template, data)
- def example = new Dust().example
+
}
View
21 src/test/scala/com/linkedin/frameworks/DustSpec.scala
@@ -5,22 +5,35 @@ import org.scalatest.matchers._
class DustSpec extends Spec with ShouldMatchers
{
+ val dust = new Dust()
+
describe("First dust example")
{
it ("should make a simple script")
{
- val result = Dust.example
+ val result = dust.example
result should be ("3.0")
}
it("should render the first example correctly")
{
val template = classOf[DustSpec].getResourceAsStream("hello.dust")
val data = classOf[DustSpec].getResourceAsStream("hello.data.json")
-
- val result = Dust.render(template, data)
-
+ time ("compile") {
+ dust.compile(template)
+ }
+ var result = ""
+ time("render") {
+ result = dust.render(data)
+ }
result should be ("Hello Mick! You have 30 new messages.")
}
}
+
+ def time(title : String = "script")(f : => Unit)
+ {
+ val then = System.currentTimeMillis
+ f
+ println("%s took %d milliseconds".format(title, System.currentTimeMillis - then))
+ }
}

0 comments on commit 24b5b3c

Please sign in to comment.