Exemple #1
0
func BenchmarkReadDisk(b *testing.B) {
	m := NewByData(NewData([]byte(utils.RandString(256))))
	buf := bytes.NewBuffer(make([]byte, 0, m.Size()*b.N))
	for i := 0; i < b.N; i++ {
		m.WriteTo(buf)
	}
	path := utils.GetRoot("/test/message/tmp")
	os.MkdirAll(filepath.Dir(path), 0777)
	if err := ioutil.WriteFile(path, buf.Bytes(), 0666); err != nil {
		b.Fatal(err)
	}

	b.ResetTimer()
	f, err := os.Open(path)
	if err != nil {
		b.Fatal(err)
	}
	defer f.Close()
	r := bufio.NewReader(f)
	var header Header
	for i := 0; i < b.N; i++ {
		_, err := Read(&header, r, RF_RESEEK_ON_FAULT)
		if err != nil {
			b.Fatal(err)
		}
	}
}
Exemple #2
0
func TestFileOp(t *testing.T) {
	dirname := utils.GetRoot("/test/bitmap.file")

	f, err := NewFile(dirname)
	if err != nil {
		t.Fatal(err)
	}
	f.Delete()
	if _, err := os.Stat(dirname); os.IsExist(err) {
		t.Fatal("expect not exist")
	}

	f, err = NewFileEx(dirname, 11)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	buf2 := make([]byte, 1024)
	copy(buf2, []byte("hello"))
	buf := make([]byte, 1024)

	{
		n, err := f.WriteAt(buf2[:5], int64(f.chunkSize)-3)
		if err != nil {
			t.Fatal(err)
		}
		nRead, err := f.ReadAt(buf[:5], int64(f.chunkSize)-3)
		if err != nil && !logex.Equal(err, io.EOF) {
			t.Fatal(err)
		}
		if !bytes.Equal(buf[:nRead], buf2[:n]) {
			t.Fatal("result not expect")
		}
	}

	if f.Size() != int64(f.chunkSize)-3+5 {
		t.Fatal("size not expect")
	}

	for i := 0; i < 20; i++ {
		offset := int64(utils.RandInt(4096))
		n, err := f.WriteAt(buf2[:5], 1024+offset)
		if err != nil {
			t.Fatal(err)
		}
		nRead, err := f.ReadAt(buf[:5], 1024+offset)
		if err != nil && !logex.Equal(err, io.EOF) {
			logex.Error(err)
			t.Fatal(err)
		}
		if !bytes.Equal(buf[:nRead], buf2[:n]) {
			t.Fatal("result not expect")
		}
	}

	if _, err := f.ReadAt(buf, -1); err == nil {
		t.Fatal("expect error")
	}
}
Exemple #3
0
func main() {
	conf := &topic.Config{
		Root:     utils.GetRoot("/topics/"),
		ChunkBit: 22,
	}
	_, _, err := mq.Listen(":12345", conf, RunClient)
	if err != nil {
		logex.Fatal(err)
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGHUP)
	<-c
}
Exemple #4
0
func benchmarkFileSize(b *testing.B, size int) {
	fileName := utils.GetRoot("/test/bitmap/normalfile")
	os.RemoveAll(fileName)

	f, _ := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0600)
	b.SetBytes(int64(size))
	data := make([]byte, size)
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		f.Write(data)
	}
	f.Sync()
}
Exemple #5
0
func benchmarkSize(b *testing.B, size int) {
	fileName := utils.GetRoot("/test/bitmap/benchmark")
	os.RemoveAll(fileName)

	f, err := NewFileEx(fileName, 22)
	if err != nil {
		b.Fatal(err)
	}
	defer func() {
		f.Delete()
	}()

	b.ResetTimer()
	data := []byte(strings.Repeat(utils.RandString(1), size))
	w := utils.Writer{f, 0}
	for i := 0; i < b.N; i++ {
		w.Write(data)
		b.SetBytes(int64(len(data)))
	}
}
Exemple #6
0
func TestFileSize(t *testing.T) {
	dirname := utils.GetRoot("/test/bitmap.file.size")
	os.RemoveAll(dirname)
	f, err := NewFile(dirname)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	if f.Size() != 0 {
		t.Fatal(err, f.Size())
	}

	if _, err := f.WriteAt([]byte("hello"), 12); err != nil {
		t.Fatal(err)
	}

	if _, err := os.Create(dirname + "/.index"); err != nil {
		t.Fatal(err)
	}
	if f.Size() != 5+12 {
		t.Fatal("file size not expected", f.Size())
	}
}
Exemple #7
0
func TestFileCon(t *testing.T) {
	n := 100
	dirname := utils.GetRoot("/test/bitmap.file.con")
	os.RemoveAll(dirname)

	f, err := NewFileEx(dirname, 1)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	buf := []byte(utils.RandString(3))
	off := int64(0)
	for i := 0; i < n; i++ {
		n, err := f.WriteAt(buf, off)
		if err != nil {
			t.Fatal(err)
		}
		if n != len(buf) {
			t.Fatal("short write", n, len(buf))
		}
		off += int64(n)
	}
}
Exemple #8
0
	return nil
}

func safeWrite(w io.Writer, buf []byte) error {
	n, err := w.Write(buf)
	if err != nil {
		return logex.Trace(err)
	}
	if n != len(buf) {
		return logex.NewError("write length not match")
	}
	return nil
}

var cfg = &Config{
	BasePath: utils.GetRoot("/test/lfs"),
}

func genBlock(size int) []byte {
	return bytes.Repeat([]byte(utils.RandString(1)), size)
}

func newIns() (*Ins, error) {
	ins, err := New(cfg)
	if err != nil {
		return nil, logex.Trace(err)
	}
	ins.Pruge()
	return New(cfg)
}
Exemple #9
0
import (
	"net"
	"sync"
	"testing"

	"github.com/chzyer/fsmq/muxque"
	"github.com/chzyer/fsmq/muxque/topic"
	"github.com/chzyer/fsmq/rpc/message"
	"github.com/chzyer/fsmq/utils"

	"gopkg.in/logex.v1"
)

var (
	conf = &topic.Config{
		Root:     utils.GetRoot("/test/api"),
		ChunkBit: 22,
	}
	addr = ":12345"
)

func runClient(m *mq.Muxque, conn *net.TCPConn) {
	mq.NewClient(m, conn)
}

type Fataler interface {
	Fatal(...interface{})
}

func runServer(t Fataler) (*mq.Muxque, *net.TCPListener) {
	que, ln, err := mq.Listen(addr, conf, runClient)