-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
engine: page defragmentation #202
Changes from 4 commits
aa188d7
6d17cb2
15ee4ec
2b1e267
1598dd1
0d50af1
da03cc3
edd5399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,63 @@ | ||||||||||||||||||||||||
package page | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||
"math" | ||||||||||||||||||||||||
"math/rand" | ||||||||||||||||||||||||
"testing" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const ( | ||||||||||||||||||||||||
maxKeySize = 100 | ||||||||||||||||||||||||
maxRecordSize = 1_000 | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
var ( | ||||||||||||||||||||||||
pageSize = int(math.Round(2 * float64(Size) / 3)) // two-thirds of the max size | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func generateRecordCell(b *testing.B) RecordCell { | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ordering: exported before unexported |
||||||||||||||||||||||||
keySize := 1 + rand.Intn(maxKeySize-1) | ||||||||||||||||||||||||
recordSize := 1 + rand.Intn(maxRecordSize-1) | ||||||||||||||||||||||||
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use an explicitely seeded random source, to be sure that the test is always reproducible, regardless of someone else seeding the whole rand package or not. |
||||||||||||||||||||||||
key, record := make([]byte, keySize), make([]byte, recordSize) | ||||||||||||||||||||||||
_, err := rand.Read(key) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
b.Fatal(err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+23
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We tend to use
And you would get rid of a lot of err checks like this. |
||||||||||||||||||||||||
_, err = rand.Read(record) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
b.Fatal(err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return RecordCell{ | ||||||||||||||||||||||||
Key: key, | ||||||||||||||||||||||||
Record: record, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func generatePage(b *testing.B) *Page { | ||||||||||||||||||||||||
b.Helper() | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this a helper, but |
||||||||||||||||||||||||
p, err := load(make([]byte, pageSize)) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why only use a page with 2 thirds of the total size? Is that benchmark still representative if the page is smaller than 16K? |
||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
b.Fatal(err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
record := generateRecordCell(b) | ||||||||||||||||||||||||
for err = p.StoreRecordCell(record); err != ErrPageFull; err = p.StoreRecordCell(record) { | ||||||||||||||||||||||||
record = generateRecordCell(b) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+43
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
return p | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func BenchmarkPage_Defragment(b *testing.B) { | ||||||||||||||||||||||||
data := generatePage(b).data | ||||||||||||||||||||||||
bytes := make([]byte, len(data)) | ||||||||||||||||||||||||
b.ResetTimer() | ||||||||||||||||||||||||
b.ReportAllocs() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
for i := 0; i < b.N; i++ { | ||||||||||||||||||||||||
copy(bytes, data) | ||||||||||||||||||||||||
Comment on lines
+51
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||
page := &Page{ // have to recreate a page with initial data | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||||||||||||||||||||||||
data: bytes, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+57
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably not be part of the benchmark, exclude it with |
||||||||||||||||||||||||
page.Defragment() | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please read the contribution and coding guidelines.