Best K6 code snippet using data.sharedArray
shared_array.go
Source:shared_array.go
1// Copyright 2016 Aleksandr Demakin. All rights reserved.2package array3import (4 "math"5 "unsafe"6 "github.com/nxgtw/go-ipc/internal/allocator"7)8const (9 indexEntrySize = unsafe.Sizeof(indexEntry{})10)11type indexEntry struct {12 len int3213 slotIdx int3214}15type index struct {16 entries []indexEntry17 bitmap []uint6418 headIdx *int3219}20func bitmapSize(sz int) int {21 bitmapSize := sz / 6422 if sz%64 != 0 {23 bitmapSize++24 }25 return bitmapSize26}27func indexSize(len int) int {28 // len of index, len of bitmap, current head index.29 return len*int(indexEntrySize) + bitmapSize(len)*8 + 430}31func newIndex(raw unsafe.Pointer, sz int) index {32 bitmapSz := bitmapSize(sz)33 rawIndexSlice := allocator.RawSliceFromUnsafePointer(raw, sz, sz)34 raw = allocator.AdvancePointer(raw, uintptr(sz)*indexEntrySize)35 rawBitmapSlize := allocator.RawSliceFromUnsafePointer(raw, bitmapSz, bitmapSz)36 raw = allocator.AdvancePointer(raw, 8*uintptr(bitmapSz))37 return index{38 entries: *(*[]indexEntry)(rawIndexSlice),39 bitmap: *(*[]uint64)(rawBitmapSlize),40 headIdx: (*int32)(raw),41 }42}43func lowestZeroBit(value uint64) uint8 {44 for bitIdx := uint8(0); bitIdx < 64; bitIdx++ {45 if value&1 == 0 {46 return bitIdx47 }48 value >>= 149 }50 return math.MaxUint851}52func (idx *index) reserveFreeSlot(at int) int32 {53 for i, b := range idx.bitmap {54 if b != math.MaxUint64 {55 bitIdx := lowestZeroBit(b)56 idx.bitmap[i] |= (1 << bitIdx)57 return int32(i*64 + int(bitIdx))58 }59 }60 panic("no free slots")61}62func (idx index) freeSlot(at int) {63 slotIdx := idx.entries[at].slotIdx64 bucketIdx, bitIdx := slotIdx/64, slotIdx%6465 idx.bitmap[bucketIdx] &= ^(1 << uint32(bitIdx))66}67// SharedArray is an array placed in the shared memory with fixed length and element size.68// It is possible to swap elements and pop them from any position. It never moves elements69// in memory, so can be used to implement an array of futexes or spin locks.70type SharedArray struct {71 data *mappedArray72 idx index73}74// NewSharedArray initializes new shared array with size and element size.75func NewSharedArray(raw unsafe.Pointer, size, elemSize int) *SharedArray {76 data := newMappedArray(raw)77 data.init(size, elemSize)78 idx := newIndex(allocator.AdvancePointer(raw, mappedArrayHdrSize+uintptr(size*elemSize)), size)79 return &SharedArray{80 data: data,81 idx: idx,82 }83}84// OpenSharedArray opens existing shared array.85func OpenSharedArray(raw unsafe.Pointer) *SharedArray {86 data := newMappedArray(raw)87 idx := newIndex(allocator.AdvancePointer(raw, mappedArrayHdrSize+uintptr(data.cap()*data.elemLen())), data.cap())88 return &SharedArray{89 data: data,90 idx: idx,91 }92}93// Cap returns array's cpacity94func (arr *SharedArray) Cap() int {95 return arr.data.cap()96}97// Len returns current length.98func (arr *SharedArray) Len() int {99 return arr.data.len()100}101// SafeLen atomically loads returns current length.102func (arr *SharedArray) SafeLen() int {103 return arr.data.safeLen()104}105// ElemSize returns size of the element.106func (arr *SharedArray) ElemSize() int {107 return arr.data.elemLen()108}109// PushBack add new element to the end of the array, merging given datas.110// Returns number of bytes copied, less or equal, than the size of the element.111func (arr *SharedArray) PushBack(datas ...[]byte) int {112 curLen := arr.Len()113 if curLen >= arr.Cap() {114 panic("index out of range")115 }116 physIdx := arr.logicalIdxToPhys(curLen)117 entry := indexEntry{118 slotIdx: arr.idx.reserveFreeSlot(physIdx),119 len: 0,120 }121 slData := arr.data.at(int(entry.slotIdx))122 for _, data := range datas {123 entry.len += int32(copy(slData[entry.len:], data))124 if int(entry.len) < len(data) {125 break126 }127 }128 arr.idx.entries[physIdx] = entry129 arr.data.incLen()130 return int(entry.len)131}132// At returns data at the position i. Returned slice references to the data in the array.133// It does not perform border check.134func (arr *SharedArray) At(i int) []byte {135 entry := arr.entryAt(i)136 return arr.data.at(int(entry.slotIdx))[:int(entry.len)]137}138// AtPointer returns pointer to the data at the position i.139// It does not perform border check.140func (arr *SharedArray) AtPointer(i int) unsafe.Pointer {141 entry := arr.entryAt(i)142 return arr.data.atPointer(int(entry.slotIdx))143}144// PopFront removes the first element of the array.145func (arr *SharedArray) PopFront() {146 curLen := arr.Len()147 if curLen == 0 {148 panic("index out of range")149 }150 arr.idx.freeSlot(int(*arr.idx.headIdx))151 arr.forwardHead()152 arr.data.decLen()153}154// PopBack removes the last element of the array.155func (arr *SharedArray) PopBack() {156 curLen := arr.Len()157 if curLen == 0 {158 panic("index out of range")159 }160 arr.idx.freeSlot(arr.logicalIdxToPhys(curLen - 1))161 if curLen == 1 {162 *arr.idx.headIdx = 0163 }164 arr.data.decLen()165}166// RemoveAt removes i'th element.167func (arr *SharedArray) RemoveAt(i int) {168 curLen := arr.Len()169 if i < 0 || i >= curLen {170 panic("index out of range")171 }172 arr.idx.freeSlot(arr.logicalIdxToPhys(i))173 if i <= curLen/2 {174 for j := i; j > 0; j-- {175 arr.idx.entries[arr.logicalIdxToPhys(j)] = arr.idx.entries[arr.logicalIdxToPhys(j-1)]176 }177 arr.forwardHead()178 } else {179 for j := i; j < curLen-1; j++ {180 arr.idx.entries[arr.logicalIdxToPhys(j)] = arr.idx.entries[arr.logicalIdxToPhys(j+1)]181 }182 if curLen == 1 {183 *arr.idx.headIdx = 0184 }185 }186 arr.data.decLen()187}188// Swap swaps two elements of the array.189func (arr *SharedArray) Swap(i, j int) {190 l := arr.Len()191 if i >= l || j >= l {192 panic("index out of range")193 }194 if i == j {195 return196 }197 i, j = arr.logicalIdxToPhys(i), arr.logicalIdxToPhys(j)198 arr.idx.entries[i], arr.idx.entries[j] = arr.idx.entries[j], arr.idx.entries[i]199}200func (arr *SharedArray) forwardHead() {201 if arr.Len() == 1 {202 *arr.idx.headIdx = 0203 } else {204 *arr.idx.headIdx = (*arr.idx.headIdx + 1) % int32(arr.Cap())205 }206}207func (arr *SharedArray) logicalIdxToPhys(log int) int {208 return (log + int(*arr.idx.headIdx)) % arr.Cap()209}210func (arr *SharedArray) entryAt(log int) indexEntry {211 return arr.idx.entries[arr.logicalIdxToPhys(log)]212}213// CalcSharedArraySize returns the size, needed to place shared array in memory.214func CalcSharedArraySize(size, elemSize int) int {215 return int(mappedArrayHdrSize) + // mq header216 indexSize(size) + // mq index217 size*elemSize // mq messages size218}...
data.go
Source:data.go
...29type (30 // RootModule is the global module instance that will create module31 // instances for each VU.32 RootModule struct {33 shared sharedArrays34 }35 // Data represents an instance of the data module.36 Data struct {37 vu modules.VU38 shared *sharedArrays39 }40 sharedArrays struct {41 data map[string]sharedArray42 mu sync.RWMutex43 }44)45var (46 _ modules.Module = &RootModule{}47 _ modules.Instance = &Data{}48)49// New returns a pointer to a new RootModule instance.50func New() *RootModule {51 return &RootModule{52 shared: sharedArrays{53 data: make(map[string]sharedArray),54 },55 }56}57// NewModuleInstance implements the modules.Module interface to return58// a new instance for each VU.59func (rm *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {60 return &Data{61 vu: vu,62 shared: &rm.shared,63 }64}65// Exports returns the exports of the data module.66func (d *Data) Exports() modules.Exports {67 return modules.Exports{68 Named: map[string]interface{}{69 "SharedArray": d.sharedArray,70 },71 }72}73// sharedArray is a constructor returning a shareable read-only array74// indentified by the name and having their contents be whatever the call returns75func (d *Data) sharedArray(call goja.ConstructorCall) *goja.Object {76 rt := d.vu.Runtime()77 if d.vu.State() != nil {78 common.Throw(rt, errors.New("new SharedArray must be called in the init context"))79 }80 name := call.Argument(0).String()81 if name == "" {82 common.Throw(rt, errors.New("empty name provided to SharedArray's constructor"))83 }84 fn, ok := goja.AssertFunction(call.Argument(1))85 if !ok {86 common.Throw(rt, errors.New("a function is expected as the second argument of SharedArray's constructor"))87 }88 array := d.shared.get(rt, name, fn)89 return array.wrap(rt).ToObject(rt)90}91func (s *sharedArrays) get(rt *goja.Runtime, name string, call goja.Callable) sharedArray {92 s.mu.RLock()93 array, ok := s.data[name]94 s.mu.RUnlock()95 if !ok {96 s.mu.Lock()97 defer s.mu.Unlock()98 array, ok = s.data[name]99 if !ok {100 array = getShareArrayFromCall(rt, call)101 s.data[name] = array102 }103 }104 return array105}106func getShareArrayFromCall(rt *goja.Runtime, call goja.Callable) sharedArray {107 gojaValue, err := call(goja.Undefined())108 if err != nil {109 common.Throw(rt, err)110 }111 obj := gojaValue.ToObject(rt)112 if obj.ClassName() != "Array" {113 common.Throw(rt, errors.New("only arrays can be made into SharedArray")) // TODO better error114 }115 arr := make([]string, obj.Get("length").ToInteger())116 stringify, _ := goja.AssertFunction(rt.GlobalObject().Get("JSON").ToObject(rt).Get("stringify"))117 var val goja.Value118 for i := range arr {119 val, err = stringify(goja.Undefined(), obj.Get(strconv.Itoa(i)))120 if err != nil {121 panic(err)122 }123 arr[i] = val.String()124 }125 return sharedArray{arr: arr}126}...
sharedArray
Using AI Code Generation
1import "data"2func main() {3}4import "data"5func main() {6}7import "data"8func main() {9}10import "data"11func main() {12}13import "data"14func main() {15}16import "data"17func main() {18}19import "data"20func main() {21}22import "data"23func main() {24}25import "data"26func main() {27}28import "data"29func main() {30}31import "data"32func main() {33}34import "data"35func main() {36}37import "data"38func main() {39}40import "data"
sharedArray
Using AI Code Generation
1import (2func main() {3 data := Data{}4 data.sharedArray = []int{1,2,3}5 fmt.Println(data.sharedArray)6}7import (8func main() {9 data := Data{}10 data.sharedArray = []int{4,5,6}11 fmt.Println(data.sharedArray)12}13import (14func main() {15 data := Data{}16 data.sharedArray = []int{7,8,9}17 fmt.Println(data.sharedArray)18}19import (20func main() {21 data := Data{}22 data.sharedArray = []int{10,11,12}23 fmt.Println(data.sharedArray)24}25import (26func main() {27 data := Data{}28 data.sharedArray = []int{13,14,15}29 fmt.Println(data.sharedArray)30}31import (32func main() {33 data := Data{}34 data.sharedArray = []int{16,17,18}35 fmt.Println(data.sharedArray)36}37import (38func main() {39 data := Data{}40 data.sharedArray = []int{19,20,21}41 fmt.Println(data.sharedArray)42}43import (44func main() {45 data := Data{}46 data.sharedArray = []int{22,23,24}47 fmt.Println(data.sharedArray)48}49import (50func main() {51 data := Data{}
sharedArray
Using AI Code Generation
1import (2func main() {3 fmt.Println(data.SharedArray())4}5import (6func main() {7 fmt.Println(data.SharedMap())8}9import (10func main() {11 fmt.Println(data.SharedStruct())12}13import (14func main() {15 fmt.Println(data.SharedFunc())16}17import (18func main() {19 fmt.Println(data.SharedConst())20}21import (22func main() {23 fmt.Println(data.SharedVar())24}25import (26func main() {27 fmt.Println(data.SharedInterface())28}29import (30func main() {31 fmt.Println(data.SharedType())32}33import (34func main() {35 fmt.Println(data.SharedChan())36}37import (38func main() {39 fmt.Println(data.SharedMapChan())40}
sharedArray
Using AI Code Generation
1import (2func main() {3 sharedArray.Data.Add("Hello", 0)4 sharedArray.Data.Add("World", 1)5 sharedArray.Data.Add("!", 2)6 fmt.Println(sharedArray.Data.Get(0))7 fmt.Println(sharedArray.Data.Get(1))8 fmt.Println(sharedArray.Data.Get(2))9}10import (11func main() {12 fmt.Println(sharedArray.Data.Get(0))13 fmt.Println(sharedArray.Data.Get(1))14 fmt.Println(sharedArray.Data.Get(2))15}16I have a sharedArray package that is imported by two files. The Data class in the package has a method to add a string to a slice and a method to get a string from the slice. I want to be able to add strings to the slice in one file and then get them in another file. I am trying to do this with global variables but I'm not sure how to do it. Here is my code:1.go2.go17int main() {18 Game game;19 game.loadTextures();20 game.update();21 game.draw();22 return 0;23}24class Game {25 void loadTextures();26 void update();27 void draw();28 sf::Texture texture;29};30void Game::loadTextures() {31}32void Game::update() {33}34void Game::draw() {35}
sharedArray
Using AI Code Generation
1import (2type data struct {3}4func (d *data) setArray() {5 rand.Seed(time.Now().UnixNano())6 for i := 0; i < 10; i++ {7 d.sharedArray = append(d.sharedArray, rand.Intn(100))8 }9}10func (d *data) getArray() []int {11}12func (d *data) printArray() {13 fmt.Println(d.sharedArray)14}15func main() {16 d := data{}17 d.setArray()18 d.printArray()19 fmt.Println(d.getArray())20}21import (22type data struct {23}24func (d *data) setArray() {25 rand.Seed(time.Now().UnixNano())26 for i := 0; i < 10; i++ {27 d.sharedArray = append(d.sharedArray, rand.Intn(100))28 }29}30func (d *data) getArray() []int {31}32func (d *data) printArray() {33 fmt.Println(d.sharedArray)34}35func main() {36 d := data{}37 d.setArray()38 d.printArray()39 fmt.Println(d.getArray())40}
sharedArray
Using AI Code Generation
1import (2func main() {3 d := data.NewData()4 d.Set(0, 1)5 fmt.Printf("sharedArray[0] is %d6", d.Get(0))7}8import (9func main() {10 d := data.NewData()11 d.Set(0, 1)12 fmt.Printf("sharedArray[0] is %d13", d.Get(0))14}15import (16func main() {17 d := data.NewData()18 d.Set(0, 1)19 fmt.Printf("sharedArray[0] is %d20", d.Get(0))21}22import (23func main() {24 d := data.NewData()25 d.Set(0, 1)26 fmt.Printf("sharedArray[0] is %d27", d.Get(0))28}29import (30func main() {31 d := data.NewData()32 d.Set(0, 1)33 fmt.Printf("sharedArray[0] is %d34", d.Get(0))35}36import (37func main() {38 d := data.NewData()
sharedArray
Using AI Code Generation
1import (2func main() {3 sharedArray := data.NewSharedArray(10)4 data := data.NewData(sharedArray)5 ch := make(chan int)6 go func() {7 for {8 fmt.Println("Reading from shared array: ", sharedArray.Read())9 time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)10 }11 }()12 go func() {13 for {14 data.Write(rand.Intn(100))15 time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)16 }17 }()18}19import (20func main() {21 sharedArray := data.NewSharedArray(10)22 data := data.NewData(sharedArray)23 ch := make(chan int)24 go func() {25 for {26 fmt.Println("Reading from shared array: ", sharedArray.Read())27 time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)28 }29 }()30 go func() {31 for {32 data.Write(rand.Intn(100))33 time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)34 }35 }()36}37import (
sharedArray
Using AI Code Generation
1import (2func main() {3 sa = sharedArray.NewSharedArray(5)4 sa.Write(0, 10)5 sa.Write(1, 11)6 sa.Write(2, 12)7 sa.Write(3, 13)8 sa.Write(4, 14)9 fmt.Println(sa.Read(0))10 fmt.Println(sa.Read(1))11 fmt.Println(sa.Read(2))12 fmt.Println(sa.Read(3))13 fmt.Println(sa.Read(4))14}15import (16func main() {17 sa = sharedArray.NewSharedArray(5)18 sa.Write(0, 10)19 sa.Write(1, 11)20 sa.Write(2, 12)21 sa.Write(3, 13)22 sa.Write(4, 14)23 fmt.Println(sa.Read(0))24 fmt.Println(sa.Read(1))25 fmt.Println(sa.Read(2))26 fmt.Println(sa.Read(3))27 fmt.Println(sa.Read(4))28}29import (30func main() {31 sa = sharedArray.NewSharedArray(5)32 sa.Write(0, 10)33 sa.Write(1, 11)34 sa.Write(2, 12)35 sa.Write(3, 13)36 sa.Write(4, 14)37 fmt.Println(sa.Read(0))38 fmt.Println(sa.Read(1))39 fmt.Println(sa.Read(2))40 fmt.Println(sa.Read(3))41 fmt.Println(sa.Read(4))42}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!