-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is part of #532 The reason behind this not redefing ... how to open a file or load data from one is that, as in the majority of the cases after that there is some amount of ... transformation of the data, so that won't help much. The original idea was to have a shareable JSON ... but the more concrete array implementation is faster (in some cases by a lot) and also seems like a better fix for the first iteration of this, as that will be the majority of the data type. So it is very likely that the non array parts will be dropped in favor of doing them later and this getting merged faster. There are still more things to be done and depending on whether or not we want to support iterating (with for-of) it can probably be done without some of the js code.
- Loading branch information
Showing
4 changed files
with
307 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* | ||
* k6 - a next-generation load testing tool | ||
* Copyright (C) 2020 Load Impact | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package js | ||
|
||
// TODO move this to another package | ||
// it can possibly be even in a separate repo if the error handling is fixed | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/dop251/goja" | ||
"github.com/loadimpact/k6/js/common" | ||
) | ||
|
||
// TODO rename | ||
// TODO check how it works with setup data | ||
// TODO check how it works if logged | ||
// TODO maybe drop it and leave the sharedArray only for now | ||
type shared struct { | ||
value *goja.Object | ||
} | ||
|
||
func (s shared) Get(ctx context.Context, index goja.Value) (interface{}, error) { | ||
rt := common.GetRuntime(ctx) | ||
// TODO other index | ||
val := s.value.Get(index.String()) | ||
if val == nil { | ||
return goja.Undefined(), nil | ||
} | ||
b, err := val.ToObject(rt).MarshalJSON() | ||
if err != nil { // cache bytes, pre marshal | ||
return goja.Undefined(), err | ||
} | ||
var tmp interface{} | ||
if err = json.Unmarshal(b, &tmp); err != nil { | ||
return goja.Undefined(), err | ||
} | ||
return tmp, nil | ||
} | ||
|
||
type sharedArray struct { | ||
arr [][]byte | ||
} | ||
|
||
func (s sharedArray) Get(index int) (interface{}, error) { | ||
if index < 0 || index >= len(s.arr) { | ||
return goja.Undefined(), nil | ||
} | ||
|
||
var tmp interface{} | ||
if err := json.Unmarshal(s.arr[index], &tmp); err != nil { | ||
return goja.Undefined(), err | ||
} | ||
return tmp, nil | ||
} | ||
|
||
func (s sharedArray) Length() int { | ||
return len(s.arr) | ||
} | ||
|
||
type sharedArrayIterator struct { | ||
a *sharedArray | ||
index int | ||
} | ||
|
||
func (sai *sharedArrayIterator) Next() (interface{}, error) { | ||
if sai.index == len(sai.a.arr)-1 { | ||
return map[string]bool{"done": true}, nil | ||
} | ||
sai.index++ | ||
var tmp interface{} | ||
if err := json.Unmarshal(sai.a.arr[sai.index], &tmp); err != nil { | ||
return goja.Undefined(), err | ||
} | ||
return map[string]interface{}{"value": tmp}, nil | ||
} | ||
|
||
func (s sharedArray) Iterator() *sharedArrayIterator { | ||
return &sharedArrayIterator{a: &s, index: -1} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* | ||
* k6 - a next-generation load testing tool | ||
* Copyright (C) 2020 Load Impact | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package js | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/loadimpact/k6/lib" | ||
"github.com/loadimpact/k6/lib/testutils" | ||
"github.com/loadimpact/k6/stats" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInitContextNewShareable(t *testing.T) { | ||
data := ` | ||
function generateArray() { | ||
console.log("once"); | ||
var n = 50; | ||
var arr = new Array(n); | ||
for (var i = 0 ; i <n; i++) { | ||
arr[i] = "something" +i; | ||
} | ||
return arr; | ||
} | ||
var s = newShare("something", generateArray); | ||
exports.default = function() { | ||
if (s[2] !== "something2") { | ||
throw new Error("bad s[2]="+s[2]) | ||
} | ||
if (s.length != 50) { | ||
throw new Error("bad length " +_s.length) | ||
} | ||
var i = 0; | ||
for (var v of s) { | ||
if (v !== "something"+i) { | ||
throw new Error("bad v="+v+" for i="+i) | ||
} | ||
i++; | ||
} | ||
}` | ||
|
||
logger := logrus.New() | ||
logger.SetLevel(logrus.InfoLevel) | ||
logger.Out = ioutil.Discard | ||
hook := testutils.SimpleLogrusHook{ | ||
HookedLevels: []logrus.Level{logrus.InfoLevel, logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}, | ||
} | ||
logger.AddHook(&hook) | ||
|
||
r1, err := getSimpleRunner(t, "/script.js", data, logger) | ||
require.NoError(t, err) | ||
entries := hook.Drain() | ||
require.Len(t, entries, 1) | ||
assert.Equal(t, logrus.InfoLevel, entries[0].Level) | ||
assert.Equal(t, "once", entries[0].Message) | ||
|
||
r2, err := NewFromArchive(logger, r1.MakeArchive(), lib.RuntimeOptions{}) | ||
require.NoError(t, err) | ||
entries = hook.Drain() | ||
require.Len(t, entries, 1) | ||
assert.Equal(t, logrus.InfoLevel, entries[0].Level) | ||
assert.Equal(t, "once", entries[0].Message) | ||
|
||
testdata := map[string]*Runner{"Source": r1, "Archive": r2} | ||
for name, r := range testdata { | ||
t.Parallel() | ||
r := r | ||
t.Run(name, func(t *testing.T) { | ||
samples := make(chan stats.SampleContainer, 100) | ||
initVU, err := r.NewVU(1, samples) | ||
if assert.NoError(t, err) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx}) | ||
err := vu.RunOnce() | ||
assert.NoError(t, err) | ||
entries := hook.Drain() | ||
require.Len(t, entries, 0) | ||
} | ||
}) | ||
} | ||
} |