You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/data/markdown/docs/02 javascript api/04 k6-data/1-SharedArray.md
+19-27Lines changed: 19 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,18 @@
2
2
title: SharedArray
3
3
---
4
4
5
-
`SharedArray` is an array-like object that shares the underlying memory between VUs. Its constructor
6
-
takes a name for the `SharedArray` and a function which needs to return an array object itself. The
7
-
function will be executed only once and its result will then be saved in memory once and copies
8
-
of the elements will be given when requested. The name is needed as VUs are completely separate JS
9
-
VMs and k6 needs some way to identify the `SharedArray`s that it needs to return.
5
+
`SharedArray` is an array-like object that shares the underlying memory between VUs. Its constructor takes a name for the `SharedArray` and a function which needs to return an array object itself. The function will be executed only once and its result will then be saved in memory once and copies of the elements will be given when requested. The name is needed as VUs are completely separate JS VMs and k6 needs some way to identify the `SharedArray`s that it needs to return.
10
6
11
-
This does mean that you can have multiple such ones and even only load some of them for given VUs, although that is
12
-
unlikely to have any performance benefit.
7
+
This does mean that you can have multiple such ones and even only load some of them for given VUs, although that is unlikely to have any performance benefit.
13
8
14
-
Everything about `SharedArray` is read-only once it is constructed, so it is not possible to
15
-
communicate between VUs using it.
9
+
Everything about `SharedArray` is read-only once it is constructed, so it is not possible to communicate between VUs using it.
16
10
17
11
Supported operations include:
18
12
1. getting the number of elements with `length`
19
13
2. getting an element by its index using the normal syntax `array[index]`
20
14
3. using `for-of` loops
21
15
22
-
Which means that for the most part if you currently have an array data structure that you want to
23
-
take less memory you can just wrap it in `SharedArray` and it should work for most cases.
16
+
Which means that for the most part if you currently have an array data structure that you want to take less memory you can just wrap it in `SharedArray` and it should work for most cases.
24
17
25
18
### Examples
26
19
@@ -30,7 +23,7 @@ take less memory you can just wrap it in `SharedArray` and it should work for mo
30
23
import { SharedArray } from"k6/data";
31
24
32
25
var data =newSharedArray("some name", function() {
33
-
// here you can open files, and then do additional processing on them or just generate the data dynamically
26
+
// here you can open files, and then do additional processing on them or just generate the data dynamically
34
27
var f =JSON.parse(open("./somefile.json")).;
35
28
return f; // f must be an array
36
29
});
@@ -45,12 +38,9 @@ export default () => {
45
38
46
39
## Performance characteristics
47
40
48
-
As the `SharedArray` is keeping the data marshalled as JSON and unmarshals elements when requested it
49
-
does take additional time to unmarshal JSONs and generate objects that then need to be garbage collected.
41
+
As the `SharedArray` is keeping the data marshalled as JSON and unmarshals elements when requested it does take additional time to unmarshal JSONs and generate objects that then need to be garbage collected.
50
42
51
-
This in general should be unnoticeable compared to whatever you are doing with the data, but might
52
-
mean that for small sets of data it is better to not use `SharedArray`, although your mileage may
53
-
vary.
43
+
This in general should be unnoticeable compared to whatever you are doing with the data, but might mean that for small sets of data it is better to not use `SharedArray`, although your mileage may vary.
54
44
55
45
As an example the following script:
56
46
@@ -86,18 +76,20 @@ export default function () {
86
76
87
77
</div>
88
78
89
-
Which was ran with v0.30.0 and 100 VUs started to even out the CPU usage around 10k elements, but also was using 1/3 of the memory. At 100k `SharedArray` was the clear winner, while for lower numbers it is possible that not using it will help with performance.
79
+
Which was ran with v0.31.0 and 100 VUs. As can be seen from the table below, there isn't much of a difference at lower numbers of data lines - up until around 1000 data lines there is little benefit in memory usage. But also there is little to no difference in CPU usage as well. At 10k and above, the memory savings start to heavily translate to CPU ones.
90
80
91
-
| data lines | shared | wall time | CPU % |mem usage | http requests |
81
+
| data lines | shared | wall time | CPU % |MEM usage | http requests |
In v0.30.0 the difference in CPU usage at lower numbers was around 10-15%, but it also started to even out at around 10k data lines and was a clear winner at 100k.
101
93
102
94
The CPU/memory data comes from using `/usr/bin/time` and the raw data can be found [here](https://gist.github.com/MStoykov/1181cfa6f00bc56b90915155f885e2bb).
0 commit comments