You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
stario/circle_test.go

87 lines
1.5 KiB
Go

package stario
import (
"fmt"
"sync/atomic"
"testing"
"time"
)
func Test_Circle(t *testing.T) {
buf := NewStarBuffer(2048)
go func() {
for {
//fmt.Println("write start")
buf.Write([]byte("中华人民共和国\n"))
//fmt.Println("write success")
time.Sleep(time.Millisecond * 50)
}
}()
cpp := ""
go func() {
time.Sleep(time.Second * 3)
for {
cache := make([]byte, 64)
ints, err := buf.Read(cache)
if err != nil {
fmt.Println("read error", err)
return
}
if ints != 0 {
cpp += string(cache[:ints])
}
}
}()
time.Sleep(time.Second * 13)
fmt.Println(cpp)
}
func Test_Circle_Speed(t *testing.T) {
buf := NewStarBuffer(1048976)
count := uint64(0)
for i := 1; i <= 10; i++ {
go func() {
for {
buf.putByte('a')
}
}()
}
for i := 1; i <= 10; i++ {
go func() {
for {
_, err := buf.getByte()
if err == nil {
atomic.AddUint64(&count, 1)
}
}
}()
}
time.Sleep(time.Second * 10)
fmt.Println(count)
}
func Test_Circle_Speed2(t *testing.T) {
buf := NewStarBuffer(8192)
count := uint64(0)
for i := 1; i <= 10; i++ {
go func() {
for {
buf.Write([]byte("hello world b612 hello world b612 b612 b612 b612 b612 b612"))
}
}()
}
for i := 1; i <= 10; i++ {
go func() {
for {
mybuf := make([]byte, 1024)
j, err := buf.Read(mybuf)
if err == nil {
atomic.AddUint64(&count, uint64(j))
}
}
}()
}
time.Sleep(time.Second * 10)
fmt.Println(float64(count) / 10 / 1024 / 1024)
}