aboutsummaryrefslogtreecommitdiff
path: root/internal/inomap/inomap_test.go
blob: 0349fd6c532f2cb7f1696bb8fd9c5028c7b90226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package inomap

import (
	"sync"
	"testing"
)

func TestTranslate(t *testing.T) {
	const baseDev = 12345
	m := New(baseDev)

	q := QIno{Dev: baseDev, Ino: 1}
	out := m.Translate(q)
	if out != 1 {
		t.Errorf("expected 1, got %d", out)
	}
	q.Ino = inumTranslateBase
	out = m.Translate(q)
	if out < inumTranslateBase {
		t.Errorf("got %d", out)
	}
	out2 := m.Translate(q)
	if out2 != out {
		t.Errorf("unstable mapping: %d %d", out2, out)
	}
}

func TestTranslateStress(t *testing.T) {
	const baseDev = 12345
	m := New(baseDev)
	var wg sync.WaitGroup
	wg.Add(4)
	go func() {
		q := QIno{Dev: baseDev}
		for i := uint64(1); i <= 10000; i++ {
			q.Ino = i
			out := m.Translate(q)
			if out != i {
				t.Fail()
			}
		}
		wg.Done()
	}()
	go func() {
		q := QIno{Dev: baseDev}
		for i := uint64(1); i <= 10000; i++ {
			q.Ino = inumTranslateBase + i
			out := m.Translate(q)
			if out < inumTranslateBase {
				t.Fail()
			}
		}
		wg.Done()
	}()
	go func() {
		q := QIno{Dev: 9999999}
		for i := uint64(1); i <= 10000; i++ {
			q.Ino = i
			out := m.Translate(q)
			if out < inumTranslateBase {
				t.Fail()
			}
		}
		wg.Done()
	}()
	go func() {
		q := QIno{Dev: 4444444}
		for i := uint64(1); i <= 10000; i++ {
			q.Ino = i
			out := m.Translate(q)
			if out < inumTranslateBase {
				t.Fail()
			}
		}
		wg.Done()
	}()
	wg.Wait()
	if m.Count() != 30000 {
		t.Fail()
	}
}

func BenchmarkTranslateSingleDev(b *testing.B) {
	m := New(0)
	var q QIno
	for n := 0; n < b.N; n++ {
		q.Ino = uint64(n % 1000)
		m.Translate(q)
	}
}

func BenchmarkTranslateManyDevs(b *testing.B) {
	m := New(0)
	var q QIno
	for n := 0; n < b.N; n++ {
		q.Dev = uint64(n % 10)
		q.Ino = uint64(n % 1000)
		m.Translate(q)
	}
}