Created
December 3, 2014 16:48
-
-
Save fauve-/acab05e1b79b4a9cf808 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 main | |
import ( | |
"fmt" | |
"github.com/robertkrimen/otto" | |
"log" | |
"testing" | |
) | |
var ( | |
newton = `function newton(thing){ | |
var start = new Date().getTime(); | |
var abs = function(x){return x > 0 ? x : x * -1;}; | |
var improve = function(guess ,x){ | |
var average = function(x,y){ | |
return (x + y) / 2; | |
}; | |
return average(guess,x/guess); | |
}; | |
var square_iter = function(guess, x){ | |
if(good_enough(guess,x)){ | |
return guess; | |
}else{ | |
return square_iter(improve(guess,x),x); | |
} | |
}; | |
var square = function(x){ | |
return (x*x); | |
}; | |
var good_enough = function(guess,val){ | |
var tmp = square(guess) - val; | |
return abs(tmp) < 0.001; | |
}; | |
var sqrt = function(x){ | |
return square_iter(1.0,x); | |
}; | |
var the_square_root = sqrt(thing); | |
return the_square_root | |
}` | |
newton_object = ` | |
({ | |
newton: function(thing){ | |
var abs = function(x){return x > 0 ? x : x * -1;}; | |
var improve = function(guess ,x){ | |
var average = function(x,y){ | |
return (x + y) / 2; | |
}; | |
return average(guess,x/guess); | |
}; | |
var square_iter = function(guess, x){ | |
if(good_enough(guess,x)){ | |
return guess; | |
}else{ | |
return square_iter(improve(guess,x),x); | |
} | |
}; | |
var square = function(x){ | |
return (x*x); | |
}; | |
var good_enough = function(guess,val){ | |
var tmp = square(guess) - val; | |
return abs(tmp) < 0.001; | |
}; | |
var sqrt = function(x){ | |
return square_iter(1.0,x); | |
}; | |
var the_square_root = sqrt(thing); | |
return the_square_root | |
} | |
}) | |
` | |
) | |
func Benchmark_Allocs(b *testing.B) { | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
_ = otto.New() | |
} | |
} | |
func BenchmarkCompileNewton(b *testing.B) { | |
vm := otto.New() | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_, err := vm.Compile("", newton) | |
if err != nil { | |
log.Println(err) | |
} | |
} | |
} | |
func BenchmarkCompileAndExecuteNewton(b *testing.B) { | |
vm := otto.New() | |
obj, err := vm.Object(newton_object) | |
if err != nil { | |
b.Error(err.Error()) | |
} | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_, err := obj.Call("newton", 9) | |
if err != nil { | |
b.Error(err) | |
} | |
} | |
} | |
func Test_CompileAndGetObject(t *testing.T) { | |
vm := otto.New() | |
obj, err := vm.Object(newton_object) | |
if err != nil { | |
t.Error(err.Error()) | |
} | |
res, err := obj.Call("newton", 9) | |
if err != nil { | |
t.Error(err.Error()) | |
} | |
intf, err := res.Export() | |
if err != nil { | |
t.Error(err.Error()) | |
} | |
if intf.(float64) != 3.00009155413138 { | |
t.Error(fmt.Sprintf("got %f instead of 3", intf.(float64))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment