Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
misc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
misc
Commits
48ea9ea7
Commit
48ea9ea7
authored
Mar 17, 2017
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
dcdba0e3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
10 deletions
+50
-10
syspread_test.go
syspread_test.go
+50
-10
No files found.
syspread_test.go
View file @
48ea9ea7
...
...
@@ -4,13 +4,14 @@ import (
"crypto/rand"
"fmt"
"os"
"syscall"
"testing"
)
// xpread is pread(2) but aborts on an error or when fewer bytes was read
func
xpread
(
f
*
os
.
File
,
buf
[]
byte
,
offset
int64
)
{
n
,
err
:=
f
.
ReadAt
(
buf
,
offset
)
func
xpread
(
f
d
int
,
buf
[]
byte
,
offset
int64
)
{
n
,
err
:=
syscall
.
Pread
(
fd
,
buf
,
offset
)
if
err
!=
nil
{
panic
(
err
)
}
...
...
@@ -19,38 +20,77 @@ func xpread(f *os.File, buf []byte, offset int64) {
}
}
// BenchmarkPread1 benchmarks pread syscall when reading 1 byte from a file on tmpfs
func
BenchmarkPread1
(
b
*
testing
.
B
)
{
// xreadat is ReadAt but aborts on an error or when fewer bytes was read
func
xreadat
(
f
*
os
.
File
,
buf
[]
byte
,
offset
int64
)
{
n
,
err
:=
f
.
ReadAt
(
buf
,
offset
)
if
err
!=
nil
{
panic
(
err
)
}
if
n
!=
len
(
buf
)
{
panic
(
fmt
.
Errorf
(
"readat(%v) -> %v"
,
len
(
buf
),
n
))
}
}
// setupXdat4K setups /dev/shm/x.dat with 4K content
func
setupXdat4K
(
t
testing
.
TB
)
*
os
.
File
{
// setup 4K file on a tmpfs
f
,
err
:=
os
.
Create
(
"/dev/shm/x.dat"
)
if
err
!=
nil
{
b
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
buf4K
:=
make
([]
byte
,
4096
)
_
,
err
=
rand
.
Read
(
buf4K
)
if
err
!=
nil
{
// rand promises n = len(buf) <=> err == nil
b
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
_
,
err
=
f
.
Write
(
buf4K
)
if
err
!=
nil
{
b
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
return
f
}
// BenchmarkReadAt benchmarks os.File.ReadAt when reading 1 byte from a file on tmpfs
func
BenchmarkReadAt1
(
b
*
testing
.
B
)
{
f
:=
setupXdat4K
(
b
)
buf1B
:=
make
([]
byte
,
1
)
// warm up
xreadat
(
f
,
buf1B
,
0
)
b
.
ResetTimer
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
xreadat
(
f
,
buf1B
,
0
)
}
b
.
StopTimer
()
err
:=
f
.
Close
()
if
err
!=
nil
{
b
.
Fatal
(
f
)
}
}
// BenchmarkPread1 benchmarks pread syscall when reading 1 byte from a file on tmpfs
func
BenchmarkPread1
(
b
*
testing
.
B
)
{
f
:=
setupXdat4K
(
b
)
fd
:=
int
(
f
.
Fd
())
// syscall wants fd as int, not uintptr
buf1B
:=
make
([]
byte
,
1
)
// warm up
xpread
(
f
,
buf1B
,
0
)
xpread
(
f
d
,
buf1B
,
0
)
b
.
ResetTimer
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
xpread
(
f
,
buf1B
,
0
)
xpread
(
f
d
,
buf1B
,
0
)
}
b
.
StopTimer
()
err
=
f
.
Close
()
err
:
=
f
.
Close
()
if
err
!=
nil
{
b
.
Fatal
(
f
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment