Commit 4c05a1f1 authored by vipwzw's avatar vipwzw

优化 runtime 的性能,加入 Long 函数到runtime

parent 04454590
This diff is collapsed.
...@@ -12,10 +12,16 @@ import ( ...@@ -12,10 +12,16 @@ import (
) )
var driverName = ptypes.JsX var driverName = ptypes.JsX
var basevm *otto.Otto
func init() { func init() {
ety := types.LoadExecutorType(driverName) ety := types.LoadExecutorType(driverName)
ety.InitFuncList(types.ListMethod(&js{})) ety.InitFuncList(types.ListMethod(&js{}))
basevm = otto.New()
_, err := basevm.Run(callcode)
if err != nil {
panic(err)
}
} }
//Init 插件初始化 //Init 插件初始化
...@@ -78,7 +84,7 @@ func (u *js) callVM(prefix string, payload *jsproto.Call, tx *types.Transaction, ...@@ -78,7 +84,7 @@ func (u *js) callVM(prefix string, payload *jsproto.Call, tx *types.Transaction,
} }
vm.Set("args", payload.Args) vm.Set("args", payload.Args)
callfunc := "callcode(context, f, args, loglist)" callfunc := "callcode(context, f, args, loglist)"
jsvalue, err := vm.Run(callcode + string(code) + "\n" + callfunc) jsvalue, err := vm.Run(string(code) + "\n" + callfunc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -195,7 +201,7 @@ func (u *js) createVM(name string, tx *types.Transaction, index int) (*otto.Otto ...@@ -195,7 +201,7 @@ func (u *js) createVM(name string, tx *types.Transaction, index int) (*otto.Otto
if err != nil { if err != nil {
return nil, err return nil, err
} }
vm := otto.New() vm := basevm.Copy()
vm.Set("context", string(data)) vm.Set("context", string(data))
u.getstatedbFunc(vm, name) u.getstatedbFunc(vm, name)
u.getlocaldbFunc(vm, name) u.getlocaldbFunc(vm, name)
......
...@@ -69,7 +69,7 @@ Query.prototype.hello = function(args) { ...@@ -69,7 +69,7 @@ Query.prototype.hello = function(args) {
} }
` `
func initExec(ldb db.DB, kvdb db.KVDB, t *testing.T) *js { func initExec(ldb db.DB, kvdb db.KVDB, t assert.TestingT) *js {
e := newjs().(*js) e := newjs().(*js)
e.SetEnv(1, time.Now().Unix(), 1) e.SetEnv(1, time.Now().Unix(), 1)
e.SetLocalDB(kvdb) e.SetLocalDB(kvdb)
...@@ -190,6 +190,20 @@ func TestBigInt(t *testing.T) { ...@@ -190,6 +190,20 @@ func TestBigInt(t *testing.T) {
assert.Equal(t, `{"balance":"9223372036854775807","balance1":"-9223372036854775808","balance2":9007199254740990,"balance3":-9007199254740990}`, string(kvs[0].Value)) assert.Equal(t, `{"balance":"9223372036854775807","balance1":"-9223372036854775808","balance2":9007199254740990,"balance3":-9007199254740990}`, string(kvs[0].Value))
} }
func BenchmarkBigInt(b *testing.B) {
dir, ldb, kvdb := util.CreateTestDB()
defer util.CloseTestDB(dir, ldb)
e := initExec(ldb, kvdb, b)
//test call error(invalid json input)
s := fmt.Sprintf(`{"balance":%d,"balance1":%d,"balance2":%d,"balance3":%d}`, math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990)
call, tx := callCodeTx("test", "hello", s)
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := e.callVM("exec", call, tx, 0, nil)
assert.Nil(b, err)
}
}
func TestRewriteJSON(t *testing.T) { func TestRewriteJSON(t *testing.T) {
s := fmt.Sprintf(`{"balance":%d,"balance1":%d,"balance2":%d,"balance3":%d}`, math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990) s := fmt.Sprintf(`{"balance":%d,"balance1":%d,"balance2":%d,"balance3":%d}`, math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990)
quota := fmt.Sprintf(`{"balance":"%d","balance1":"%d","balance2":%d,"balance3":%d}`, math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990) quota := fmt.Sprintf(`{"balance":"%d","balance1":"%d","balance2":%d,"balance3":%d}`, math.MaxInt64, math.MinInt64, 9007199254740990, -9007199254740990)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment