aboutsummaryrefslogtreecommitdiff
path: root/weed/mount/access_pattern_test.go
blob: f3c05d2686e3b501280b6f29deddaab73b602677 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package mount

import (
	"testing"
	"time"
)

func TestAccessPatternDetector_Sequential(t *testing.T) {
	apd := NewAccessPatternDetector()

	inode := uint64(1)
	
	// Simulate sequential access pattern
	info1 := apd.RecordAccess(inode, 0, 1024)
	if info1.Pattern != RandomAccess {
		t.Error("First access should be detected as random")
	}

	info2 := apd.RecordAccess(inode, 1024, 1024)
	if info2.ConsecutiveSeq != 1 {
		t.Error("Second sequential access should increment counter")
	}

	info3 := apd.RecordAccess(inode, 2048, 1024)
	if info3.ConsecutiveSeq != 2 {
		t.Error("Third sequential access should increment counter")
	}

	info4 := apd.RecordAccess(inode, 3072, 1024)
	if info4.Pattern != SequentialAccess {
		t.Errorf("After %d sequential accesses, pattern should be Sequential, got: %v", 
			apd.sequentialThreshold+1, info4.Pattern)
	}
	
	if info4.PrefetchSize <= 0 {
		t.Error("Sequential access should set prefetch size")
	}
	
	shouldPrefetch, prefetchSize := apd.ShouldPrefetch(inode)
	if !shouldPrefetch {
		t.Error("Should recommend prefetch for sequential access")
	}
	
	if prefetchSize != info4.PrefetchSize {
		t.Errorf("Prefetch size mismatch: expected %d, got %d", info4.PrefetchSize, prefetchSize)
	}
}

func TestAccessPatternDetector_Random(t *testing.T) {
	apd := NewAccessPatternDetector()

	inode := uint64(2)
	
	// Simulate random access pattern
	offsets := []int64{0, 5000, 1000, 10000, 2000}
	
	for _, offset := range offsets {
		info := apd.RecordAccess(inode, offset, 1024)
		if info.ConsecutiveSeq > 0 && info != apd.fileInfo[inode] {
			// Reset should happen on non-sequential access
			t.Error("Sequential counter should reset on random access")
		}
	}
	
	finalInfo := apd.fileInfo[inode]
	if finalInfo.Pattern != RandomAccess {
		t.Errorf("Pattern should remain RandomAccess, got: %v", finalInfo.Pattern)
	}
	
	shouldPrefetch, _ := apd.ShouldPrefetch(inode)
	if shouldPrefetch {
		t.Error("Should not recommend prefetch for random access")
	}
}

func TestAccessPatternDetector_ModelAccess(t *testing.T) {
	apd := NewAccessPatternDetector()

	inode := uint64(3)
	
	// Simulate model file loading (large sequential reads)
	largeSize := 2 * 1024 * 1024 // 2MB
	
	apd.RecordAccess(inode, 0, largeSize)
	apd.RecordAccess(inode, int64(largeSize), largeSize)
	apd.RecordAccess(inode, int64(largeSize*2), largeSize)
	
	info := apd.RecordAccess(inode, int64(largeSize*3), largeSize)
	
	if info.Pattern != ModelAccess {
		t.Errorf("Large sequential reads should be detected as ModelAccess, got: %v", info.Pattern)
	}
	
	if info.Confidence < 0.9 {
		t.Errorf("Model access should have high confidence, got: %.2f", info.Confidence)
	}
	
	shouldPrefetch, prefetchSize := apd.ShouldPrefetch(inode)
	if !shouldPrefetch {
		t.Error("Should recommend prefetch for model access")
	}
	
	if prefetchSize < 4*1024*1024 { // Should be at least 4MB for models
		t.Errorf("Model access should have large prefetch size, got: %d", prefetchSize)
	}
}

func TestAccessPatternDetector_EpochAccess(t *testing.T) {
	apd := NewAccessPatternDetector()

	inode := uint64(4)
	
	// Simulate many accesses first
	for i := 0; i < 150; i++ {
		apd.RecordAccess(inode, int64(i*1024), 1024)
	}
	
	// Simulate gap (sleep not needed, just update last access time)
	info := apd.fileInfo[inode]
	info.LastAccessTime = time.Now().Add(-2 * time.Minute)
	
	// Access from beginning again (epoch restart)
	epochInfo := apd.RecordAccess(inode, 0, 1024)
	
	if epochInfo.Pattern != EpochAccess {
		t.Errorf("Restart from beginning should be detected as EpochAccess, got: %v", epochInfo.Pattern)
	}
	
	shouldPrefetch, prefetchSize := apd.ShouldPrefetch(inode)
	if !shouldPrefetch {
		t.Error("Should recommend prefetch for epoch access")
	}
	
	if prefetchSize < 256*1024 { // Should have reasonable prefetch size
		t.Errorf("Epoch access should have decent prefetch size, got: %d", prefetchSize)
	}
}

func TestAccessPatternDetector_StridedAccess(t *testing.T) {
	apd := NewAccessPatternDetector()

	inode := uint64(5)
	
	// Simulate strided access (e.g., reading every nth byte for image processing)
	stride := int64(4096)
	
	apd.RecordAccess(inode, 0, 1024)
	apd.RecordAccess(inode, 1024+stride, 1024) // Gap between reads
	apd.RecordAccess(inode, 2048+stride*2, 1024)
	info := apd.RecordAccess(inode, 3072+stride*3, 1024)
	
	// Note: Current simple implementation may not detect complex stride patterns
	// This test validates the structure is in place
	t.Logf("Strided access pattern: %v (confidence: %.2f)", info.Pattern, info.Confidence)
}

func TestAccessPatternDetector_PatternTransition(t *testing.T) {
	apd := NewAccessPatternDetector()

	inode := uint64(6)
	
	// Start with sequential
	apd.RecordAccess(inode, 0, 1024)
	apd.RecordAccess(inode, 1024, 1024)
	apd.RecordAccess(inode, 2048, 1024)
	info := apd.RecordAccess(inode, 3072, 1024)
	
	if info.Pattern != SequentialAccess {
		t.Error("Should detect sequential pattern")
	}
	
	// Break with random access
	randomInfo := apd.RecordAccess(inode, 10000, 1024)
	
	if randomInfo.Pattern != RandomAccess {
		t.Errorf("Pattern should transition to RandomAccess after break, got: %v", randomInfo.Pattern)
	}
	
	if randomInfo.PrefetchSize != 0 {
		t.Error("Prefetch size should be reset after pattern break")
	}
}

func TestAccessPatternDetector_MultipleFiles(t *testing.T) {
	apd := NewAccessPatternDetector()

	// Test tracking multiple files simultaneously
	file1 := uint64(10)
	file2 := uint64(20)
	
	// File 1: Sequential pattern
	apd.RecordAccess(file1, 0, 1024)
	apd.RecordAccess(file1, 1024, 1024)
	apd.RecordAccess(file1, 2048, 1024)
	seq_info := apd.RecordAccess(file1, 3072, 1024)
	
	// File 2: Random pattern
	apd.RecordAccess(file2, 5000, 1024)
	apd.RecordAccess(file2, 1000, 1024)
	random_info := apd.RecordAccess(file2, 8000, 1024)
	
	if seq_info.Pattern != SequentialAccess {
		t.Error("File 1 should maintain sequential pattern")
	}
	
	if random_info.Pattern != RandomAccess {
		t.Error("File 2 should maintain random pattern")
	}
	
	// Verify independent tracking
	pattern1 := apd.GetPattern(file1)
	pattern2 := apd.GetPattern(file2)
	
	if pattern1 != SequentialAccess || pattern2 != RandomAccess {
		t.Error("Files should maintain independent patterns")
	}
}

func TestAccessPatternDetector_Metrics(t *testing.T) {
	apd := NewAccessPatternDetector()

	// Generate some access patterns
	file1 := uint64(100)
	file2 := uint64(200)
	
	// Sequential accesses for file1
	for i := 0; i < 5; i++ {
		apd.RecordAccess(file1, int64(i*1024), 1024)
	}
	
	// Random accesses for file2
	offsets := []int64{0, 5000, 1000, 10000}
	for _, offset := range offsets {
		apd.RecordAccess(file2, offset, 1024)
	}
	
	metrics := apd.GetMetrics()
	
	if metrics.TotalAccesses != 9 {
		t.Errorf("Expected 9 total accesses, got: %d", metrics.TotalAccesses)
	}
	
	if metrics.TotalFiles != 2 {
		t.Errorf("Expected 2 files, got: %d", metrics.TotalFiles)
	}
	
	if metrics.PatternCounts[SequentialAccess] != 1 {
		t.Errorf("Expected 1 sequential file, got: %d", metrics.PatternCounts[SequentialAccess])
	}
	
	if metrics.PatternCounts[RandomAccess] != 1 {
		t.Errorf("Expected 1 random file, got: %d", metrics.PatternCounts[RandomAccess])
	}
}

func TestAccessPatternDetector_Cleanup(t *testing.T) {
	apd := NewAccessPatternDetector()

	inode := uint64(999)
	
	// Create an access record
	apd.RecordAccess(inode, 0, 1024)
	
	// Verify it exists
	if len(apd.fileInfo) != 1 {
		t.Error("Should have one file info entry")
	}
	
	// Set old timestamp
	info := apd.fileInfo[inode]
	info.LastAccessTime = time.Now().Add(-2 * time.Hour)
	
	// Cleanup old entries
	apd.CleanupOldEntries(1 * time.Hour)
	
	if len(apd.fileInfo) != 0 {
		t.Error("Old entry should have been cleaned up")
	}
}

func TestAccessPatternDetector_Confidence(t *testing.T) {
	apd := NewAccessPatternDetector()
	apd.confidenceThreshold = 0.8 // High threshold for testing

	inode := uint64(888)
	
	// Start sequential access but don't reach high confidence
	apd.RecordAccess(inode, 0, 1024)
	apd.RecordAccess(inode, 1024, 1024)
	apd.RecordAccess(inode, 2048, 1024)
	info := apd.RecordAccess(inode, 3072, 1024)
	
	// Should be sequential but low confidence
	if info.Pattern != SequentialAccess {
		t.Error("Should detect sequential pattern")
	}
	
	if info.Confidence >= 0.8 {
		t.Errorf("Early sequential detection should have low confidence, got: %.2f", info.Confidence)
	}
	
	// Should not recommend prefetch due to low confidence
	shouldPrefetch, _ := apd.ShouldPrefetch(inode)
	if shouldPrefetch {
		t.Error("Should not prefetch with low confidence")
	}
	
	// Continue sequential access to build confidence
	for i := 4; i < 8; i++ {
		apd.RecordAccess(inode, int64(i*1024), 1024)
	}
	
	// Now should have high confidence
	highConfInfo := apd.fileInfo[inode]
	if highConfInfo.Confidence < 0.8 {
		t.Errorf("Extended sequential access should have high confidence, got: %.2f", highConfInfo.Confidence)
	}
	
	shouldPrefetch, _ = apd.ShouldPrefetch(inode)
	if !shouldPrefetch {
		t.Error("Should prefetch with high confidence")
	}
}

// Benchmark tests

func BenchmarkAccessPatternDetector_RecordAccess(b *testing.B) {
	apd := NewAccessPatternDetector()

	b.ResetTimer()
	
	for i := 0; i < b.N; i++ {
		inode := uint64(i % 100) // Cycle through 100 different files
		offset := int64(i * 1024)
		apd.RecordAccess(inode, offset, 1024)
	}
}

func BenchmarkAccessPatternDetector_ShouldPrefetch(b *testing.B) {
	apd := NewAccessPatternDetector()

	// Setup some files with different patterns
	for i := 0; i < 100; i++ {
		inode := uint64(i)
		// Create sequential pattern
		for j := 0; j < 5; j++ {
			apd.RecordAccess(inode, int64(j*1024), 1024)
		}
	}

	b.ResetTimer()
	
	for i := 0; i < b.N; i++ {
		inode := uint64(i % 100)
		apd.ShouldPrefetch(inode)
	}
}