github.com/wencode/hack@v0.2.9/mmap/mmap_test.go (about)

     1  package mmap
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestMap(t *testing.T) {
    10  	mf, err := Open("foo.data", WithWrite())
    11  	if err != nil {
    12  		t.Fatalf("open error %v", err)
    13  	}
    14  	defer mf.Close()
    15  
    16  	buf := mf.Buffer()
    17  	if len(buf) != os.Getpagesize() {
    18  		t.Fatal("file default size error")
    19  	}
    20  
    21  	for i := 0; i < len(buf); i++ {
    22  		buf[i] = '0' + byte(i%10)
    23  	}
    24  	buf.Sync()
    25  }
    26  
    27  func TestWrite(t *testing.T) {
    28  	mf, err := Open("foo.data", WithWrite(), WithTruncate(), WithLength(4096))
    29  	if err != nil {
    30  		t.Fatalf("open error %v", err)
    31  	}
    32  	defer mf.Close()
    33  
    34  	val := []byte{'a', 'b', 'c', 'd'}
    35  	n, err := mf.Write(val)
    36  	if err != nil || n != len(val) {
    37  		t.Fatalf("write %d, err %v", n, err)
    38  	}
    39  	mf.Flush(mf.Buffer()[:4])
    40  }
    41  
    42  func TestRead(t *testing.T) {
    43  	mf, err := Open("foo.data")
    44  	if err != nil {
    45  		t.Fatalf("open error %v", err)
    46  	}
    47  	defer mf.Close()
    48  
    49  	val := make([]byte, 4)
    50  	n, err := mf.Read(val)
    51  	if err != nil || n != len(val) {
    52  		t.Fatalf("write %d, err %v", n, err)
    53  	}
    54  }
    55  
    56  func TestResize(t *testing.T) {
    57  	mf, err := Open("foo.data", WithWrite(), WithTruncate(), WithLength(4096))
    58  	if err != nil {
    59  		t.Fatalf("open error %v", err)
    60  	}
    61  	defer mf.Close()
    62  
    63  	if err := mf.Resize(8192, WithLength(8192)); err != nil {
    64  		t.Fatalf("resize error %v", err)
    65  	}
    66  	data := []byte("hello")
    67  	n := copy(mf.master[8185:], data)
    68  	if n != len(data) {
    69  		t.Errorf("write data error after resize")
    70  	}
    71  }
    72  
    73  func TestExtend(t *testing.T) {
    74  	mf, err := Open("foo.data", WithWrite(), WithTruncate(), WithLength(4096))
    75  	if err != nil {
    76  		t.Fatalf("open error %v", err)
    77  	}
    78  	defer mf.Close()
    79  
    80  	buf, err := mf.ExtendMap(4096, 4096)
    81  	if err != nil {
    82  		t.Fatalf("extend error %v", err)
    83  	}
    84  	data := []byte("hello")
    85  	n := copy(buf, data)
    86  	if n != len(data) {
    87  		t.Errorf("write data error after extend")
    88  	}
    89  }
    90  
    91  func BenchmarkCopyBuffer(b *testing.B) {
    92  	for i := 0; i < 1000; i++ {
    93  		fmt.Fprintf(os.Stderr, "benchmark %d-%d\n", b.N, i)
    94  		doCopyBuffer(b)
    95  	}
    96  }
    97  
    98  func doCopyBuffer(b *testing.B) {
    99  	filename := fmt.Sprintf("mem_%d.data", b.N)
   100  	siz := roundTo4096(b.N)
   101  	if siz > 1024*1024*512 {
   102  		siz = 1024 * 1024 * 512
   103  	}
   104  	mf, err := Open(
   105  		filename,
   106  		WithWrite(),
   107  		WithLength(siz),
   108  	)
   109  	if err != nil {
   110  		b.Fatalf("open %s error:%v", filename, err)
   111  	}
   112  	defer mf.Close()
   113  
   114  	b.StartTimer()
   115  
   116  	buf := mf.Buffer()
   117  	if buf == nil {
   118  		b.Fatalf("get %s buffer failed", filename)
   119  	}
   120  
   121  	pos := len(buf) / 2
   122  	copy(buf[:pos], buf[pos:])
   123  
   124  	b.StopTimer()
   125  }
   126  
   127  func roundTo4096(n int) int {
   128  	return (n + 4095) & ^4095
   129  }