func TestBarrierWriterReaderPartial(t *testing.T) { buf := NewBuffer() defer buf.Close() b, err := rand.Bytes(1e6, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } n, err := buf.Write(b) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if n != len(b) { t.Fatal("length invalid") } b2 := make([]byte, len(b)) count := 0 l := 100 for count < len(b) { n, err = buf.Read(b2[count : count+l]) if err == io.EOF { break } else if err != nil { t.Fatal(e.Trace(e.Forward(err))) } count += n } if count != len(b) { t.Fatal("length invalid", count, len(b)) } if !bytes.Equal(b, b2) { t.Fatal("not equal") } }
func TestFullWriteRead(t *testing.T) { const num = 1000 const maxmax = 1e6 const size = 1e6 bufs := make([][]byte, num) numGoStart := runtime.NumGoroutine() buf := NewBuffer() defer func() { buf.Close() numGoStop := runtime.NumGoroutine() t.Log("NumGoroutine:", numGoStart, numGoStop, numGoStop-numGoStart) }() for i := 0; i < num; i++ { n, err := rand.Int63n(size, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } in, err := rand.Bytes(int(n)+1, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } bufs[i] = in _, err = buf.Write(in) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } } for i := 0; i < num; i++ { n, err := rand.Int63n(maxmax, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } max := int(n) + 1 in := bufs[i] size := len(in) if size > max { size = max } b := make([]byte, len(in)) for count := 0; count < len(in); { end := len(b) if count+size < end { end = count + size } n, err := buf.Read(b[count:end]) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } count += n } if !bytes.Equal(in, b) { t.Fatal("data not equal") } } }
func TestProtoWriteReadPartial(t *testing.T) { const num = 10 const maxmax = 1e2 const size = 1e6 bufs := make([][]byte, num) fifo := NewNotAFifo(100) defer fifo.Close() for i := 0; i < num; i++ { n, err := rand.Int63n(size, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } in, err := rand.Bytes(int(n)+1, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } bufs[i] = in err = fifo.Write(in) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } } for i := 0; i < num; i++ { n, err := rand.Int63n(maxmax, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } max := int(n) + 1 in := bufs[i] size := len(in) if size > max { size = max } buf := make([]byte, len(in)) for count := 0; count < len(in); { end := len(buf) if count+size < end { end = count + size } n, err := fifo.Read(buf[count:end]) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } count += n } if !bytes.Equal(in, buf) { t.Fatal("data not equal") } } }
func BenchmarkLoad(b *testing.B) { instances := core.NewInstances() owners := auth.NewPlainTextCredentials() id = &auth.PlainText{Ident: "id", Pass: "******"} owners.Add(id) constructor := core.NewConstructor() server = &core.Server{ Proto: "tcp", Addr: "localhost:0", ConnTimeout: 30 * time.Second, Insts: instances, Cons: constructor, FifoLength: 1, Ttl: 24 * time.Hour, Cleanup: 5 * time.Minute, } server.Start() err := instances.New("1", "3", reflect.ValueOf(&Load{}), owners) if err != nil { b.Error(e.Trace(e.Forward(err))) } l := &LoadClient{ Net: "tcp", Addrs: server.Address().String(), Auth: id, Session: "1", Instance: "3", PointerRecv: true, } err = l.Init(true) if err != nil { b.Error(e.Trace(e.Forward(err))) } in, err := rand.Bytes(1e6, "go") if err != nil { b.Error(e.Trace(e.Forward(err))) } b.ResetTimer() for i := 0; i < b.N; i++ { l.Load(in) b.SetBytes(int64(len(in))) } }
func TestBarrierReaderWriter(t *testing.T) { buf := NewBuffer() defer buf.Close() var wg sync.WaitGroup wg.Add(1) b, err := rand.Bytes(1e6, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } signal := make(chan struct{}) go func() { defer wg.Done() b2 := make([]byte, len(b)) count := 0 l := 100 signal <- struct{}{} for count < len(b) { n, err := buf.Read(b2[count : count+l]) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } count += n } if count != len(b) { t.Fatal("length invalid") } if !bytes.Equal(b, b2) { t.Fatal("not equal") } }() <-signal n, err := buf.Write(b) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if n != len(b) { t.Fatal("length invalid") } wg.Wait() }
func TestProtoWriteRead(t *testing.T) { const num = 10 const size = 1e7 bufs := make([][]byte, num) fifo := NewNotAFifo(100) defer fifo.Close() for i := 0; i < num; i++ { n, err := rand.Int63n(size, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } in, err := rand.Bytes(int(n)+1, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } bufs[i] = in err = fifo.Write(in) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } } for i := 0; i < num; i++ { in := bufs[i] out := make([]byte, len(in)) n, err := fifo.Read(out) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if n != len(in) { t.Fatal("don't read all data") } if !bytes.Equal(in, out) { t.Fatal("data not equal") } } }
func TestBarrierReaderParcialWriter(t *testing.T) { const size int = 1e6 const wparcial int = 1e3 const rparcial int = 1e3 buf := NewBuffer() defer buf.Close() var wg sync.WaitGroup wg.Add(1) b, err := rand.Bytes(size, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } signal := make(chan struct{}) go func() { defer wg.Done() b2 := make([]byte, len(b)) count := 0 signal <- struct{}{} for count < len(b) { l := wparcial + count if l > len(b2) { l = len(b2) } n, err := buf.Read(b2[count:l]) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } count += n } if count != len(b) { t.Fatal("length invalid") } if !bytes.Equal(b, b2) { t.Fatal("not equal") } }() <-signal bb := make([]byte, len(b)) copy(bb, b) for len(bb) > 0 { d, err := rand.Int63n(10, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } time.Sleep(time.Duration(d) * time.Millisecond) l := rparcial if rparcial > len(bb) { l = len(bb) } n, err := buf.Write(bb[:l]) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if n != l { t.Fatal("length invalid") } bb = bb[l:] } wg.Wait() }
func TestProtoSortIncoming(t *testing.T) { const size = 1e6 + 100 const num = 100 log.SetLevel("all", log.ProtoPrio) defer log.SetLevel("all", log.DebugPrio) buf := bytes.NewBuffer([]byte{}) reader := bufio.NewReader(buf) buffers := newBuffers(10) ins := make([][]byte, num+1) for i := 2; i <= num; i++ { err := buffers.New(uint32(i)) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } b := make([]byte, 14) err = header(b, uint32(i), size) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } ins[i], err = rand.Bytes(size, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } n, err := buf.Write(b) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if n != 14 { t.Fatal("write fail") } n, err = buf.Write(ins[i]) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if n != size { t.Fatal("write fail") } } err := sortIncoming(reader, buffers, "test") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } out := make([]byte, size) for i := 2; i <= num; i++ { n, err := buffers.Read(uint32(i), out) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if n != size { t.Fatal("read fail", i) } //t.Log(i, ins[i][:30]) //t.Log(i, out[:30]) if !bytes.Equal(ins[i], out) { // t.Log(ins[i][:30]) t.Fatal("not equal", i) } } }
func TestProtoWriteReadRandom(t *testing.T) { const num = 500 const bufsize = 1e6 var wg sync.WaitGroup wg.Add(num * 2) bufs := make([][]byte, num) fifo := NewNotAFifo(100) defer fifo.Close() for i := 0; i < num; i++ { // n, err := rand.Int63n(bufsize, "go") // if err != nil { // t.Fatal(e.Trace(e.Forward(err))) // } in, err := rand.Bytes(bufsize, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } bufs[i] = append(make([]byte, binary.MaxVarintLen32), in...) binary.PutVarint(bufs[i][0:binary.MaxVarintLen32], int64(i)) } go func() { for i := 0; i < num; i++ { go func(i int) { defer wg.Done() n, err := rand.Int63n(100, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } time.Sleep(time.Duration(n) * time.Millisecond) //println("Start writer", i) err = fifo.Write(bufs[i]) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } //println("End writer", i) }(i) } }() go func() { for i := 0; i < num; i++ { go func(i int) { defer wg.Done() dur, err := rand.Int63n(100, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } time.Sleep(time.Duration(dur) * time.Millisecond) out := make([]byte, bufsize+binary.MaxVarintLen32) //println("Start reader", i) n, err := fifo.Read(out) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } //println("End reader", i) n -= binary.MaxVarintLen32 idx, b := binary.Varint(out[:binary.MaxVarintLen32]) if b <= 0 { t.Fatal("can't decode the index") } if idx < 0 || idx > int64(len(bufs)) { t.Fatal("wrong idx", idx) } //println("Reader idx", idx) if n != bufsize { t.Fatal("don't read all data", n) } if !bytes.Equal(bufs[idx], out) { t.Fatal("data not equal") } //println("Stop reader", i) }(i) } }() wg.Wait() }
func TestLoad(t *testing.T) { //defer leaktest.Check(t)() //ulimit -n 1000 var wg sync.WaitGroup const num = 5 t.Log("Num:", num) wg.Add(num) err := instances.New("1", "3", reflect.ValueOf(&Load{}), owners) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } l := &LoadClient{ Net: "tcp", Addrs: server.Address().String(), Auth: id, Session: "1", Instance: "3", PointerRecv: true, NumClones: 5, MaxNumClones: 10, } err = l.Init(true) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } for i := 0; i < num; i++ { go func(i int) { defer wg.Done() println("Starting", i) n, err := rand.Int63n(100, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } time.Sleep(time.Duration(n) * time.Millisecond) // n, err = rand.Int63n(1e6, "go") // if err != nil { // t.Fatal(e.Trace(e.Forward(err))) // } n = 1e6 in, err := rand.Bytes(int(n), "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } out := l.Load(in) err = l.Error() if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if !bytes.Equal(in, out) { t.Fatal("byte array not equal") } println("end", i) }(i) } wg.Wait() err = l.CloseClient() if err != nil { t.Fatal(e.Trace(e.Forward(err))) } }