FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
benchmarks/src/main/java/org/lmdbjava/bench/Common.java at master · lmdbjava/benchmarks · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
lmdbjava
/
benchmarks
Public
Notifications
You must be signed in to change notification settings
Fork
22
Star
144
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
benchmarks
/
src
/
main
/
java
/
org
/
lmdbjava
/
bench
/
Common.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
203 lines (179 loc) · 5.66 KB
Breadcrumbs
benchmarks
/
src
/
main
/
java
/
org
/
lmdbjava
/
bench
/
Common.java
Copy path
File metadata and controls
203 lines (179 loc) · 5.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright © 2016-2025 The LmdbJava Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
lmdbjava
.
bench
;
import
static
java
.
lang
.
Integer
.
BYTES
;
import
static
java
.
lang
.
System
.
getProperty
;
import
static
java
.
lang
.
System
.
out
;
import
static
jnr
.
posix
.
POSIXFactory
.
getPOSIX
;
import
static
org
.
openjdk
.
jmh
.
annotations
.
Scope
.
Benchmark
;
import
java
.
io
.
File
;
import
java
.
io
.
IOException
;
import
java
.
util
.
zip
.
CRC32
;
import
jnr
.
posix
.
FileStat
;
import
jnr
.
posix
.
POSIX
;
import
org
.
agrona
.
collections
.
IntHashSet
;
import
org
.
apache
.
commons
.
math3
.
random
.
BitsStreamGenerator
;
import
org
.
apache
.
commons
.
math3
.
random
.
MersenneTwister
;
import
org
.
openjdk
.
jmh
.
annotations
.
Param
;
import
org
.
openjdk
.
jmh
.
annotations
.
State
;
import
org
.
openjdk
.
jmh
.
infra
.
BenchmarkParams
;
/**
* Common JMH {@link State} superclass for all DB benchmark states.
*
* <p>
* Members do not reflect the typical code standards of the LmdbJava project due
* to compliance requirements with JMH {@link Param} and {@link State}.
*/
@
State
(
Benchmark
)
public
class
Common
{
static
final
byte
[]
RND_MB
=
new
byte
[
1_048_576
];
static
final
int
STRING_KEY_LENGTH
=
16
;
private
static
final
POSIX
POSIX
=
getPOSIX
();
private
static
final
BitsStreamGenerator
RND
=
new
MersenneTwister
();
private
static
final
int
S_BLKSIZE
=
512
;
// from sys/stat.h
private
static
final
File
TMP_BENCH
;
File
compact
;
CRC32
crc
;
/**
* Keys are always an integer, however they are actually stored as integers
* (taking 4 bytes) or as zero-padded 16 byte strings. Storing keys as
* integers offers a major performance gain.
*/
@
Param
(
"true"
)
boolean
intKey
;
/**
* Determined during {@link #setup()} based on {@link #intKey} value.
*/
int
keySize
;
/**
* Keys in designated (random/sequential) order.
*/
int
[]
keys
;
/**
* Number of entries to read/write to the database.
*/
@
Param
(
"1000000"
)
int
num
;
/**
* Whether the keys are to be inserted into the database in sequential order
* (and in the "readKeys" case, read back in that order). For LMDB, sequential
* inserts use {@link org.lmdbjava.PutFlags#MDB_APPEND} and offer a major
* performance gain. If this field is false, the append flag will not be used
* and the keys will instead be inserted (and read back via "readKeys") in a
* random order.
*/
@
Param
(
"true"
)
boolean
sequential
;
File
tmp
;
/**
* Whether the values contain random bytes or are simply the same as the key.
* If true, the random bytes are obtained sequentially from a 1 MB random byte
* buffer.
*/
@
Param
(
"false"
)
boolean
valRandom
;
/**
* Number of bytes in each value.
*/
@
Param
(
"100"
)
int
valSize
;
static
{
RND
.
nextBytes
(
RND_MB
);
final
String
tmpParent
=
getProperty
(
"java.io.tmpdir"
);
TMP_BENCH
=
new
File
(
tmpParent
,
"lmdbjava-benchmark-scratch"
);
}
public
void
setup
(
final
BenchmarkParams
b
)
throws
IOException
{
keySize
=
intKey
?
BYTES
:
STRING_KEY_LENGTH
;
crc
=
new
CRC32
();
final
IntHashSet
set
=
new
IntHashSet
(
num
);
keys
=
new
int
[
num
];
for
(
int
i
=
0
;
i
<
num
;
i
++) {
if
(
sequential
) {
keys
[
i
] =
i
;
}
else
{
while
(
true
) {
int
candidateKey
=
RND
.
nextInt
();
if
(
candidateKey
<
0
) {
candidateKey
*= -
1
;
}
if
(!
set
.
contains
(
candidateKey
)) {
set
.
add
(
candidateKey
);
keys
[
i
] =
candidateKey
;
break
;
}
}
}
}
rmdir
(
TMP_BENCH
);
tmp
=
create
(
b
,
""
);
compact
=
create
(
b
,
"-compacted"
);
}
public
void
reportSpaceBeforeClose
() {
if
(
tmp
.
getName
().
contains
(
".readKey-"
)) {
reportSpaceUsed
(
tmp
,
"before-close"
);
}
}
public
void
teardown
()
throws
IOException
{
// we only output for key, as all impls offer it and it should be fixed
if
(
tmp
.
getName
().
contains
(
".readKey-"
)) {
reportSpaceUsed
(
tmp
,
"after-close"
);
}
rmdir
(
TMP_BENCH
);
}
protected
void
reportSpaceUsed
(
final
File
dir
,
final
String
desc
) {
final
File
[]
files
=
dir
.
listFiles
();
if
(
files
==
null
) {
return
;
}
long
bytes
=
0
;
for
(
final
File
f
:
files
) {
if
(
f
.
isDirectory
()) {
throw
new
UnsupportedOperationException
(
"impl created directory"
);
}
final
FileStat
stat
=
POSIX
.
stat
(
f
.
getAbsolutePath
());
bytes
+=
stat
.
blocks
() *
S_BLKSIZE
;
}
out
.
println
(
"
\n
Bytes
\t
"
+
desc
+
"
\t
"
+
bytes
+
"
\t
"
+
dir
.
getName
());
}
final
String
padKey
(
final
int
key
) {
final
String
skey
=
Integer
.
toString
(
key
);
return
"0000000000000000"
.
substring
(
0
,
16
-
skey
.
length
()) +
skey
;
}
private
File
create
(
final
BenchmarkParams
b
,
final
String
suffix
) {
final
File
f
=
new
File
(
TMP_BENCH
,
b
.
id
() +
suffix
);
if
(!
f
.
mkdirs
()) {
throw
new
IllegalStateException
(
"Cannot mkdir "
+
f
);
}
return
f
;
}
private
void
rmdir
(
final
File
file
) {
if
(!
file
.
exists
()) {
return
;
}
if
(
file
.
isDirectory
()) {
final
File
[]
files
=
file
.
listFiles
();
if
(
files
==
null
) {
return
;
}
for
(
final
File
f
:
files
) {
rmdir
(
f
);
}
}
if
(!
file
.
delete
()) {
throw
new
IllegalStateException
(
"Cannot delete "
+
file
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL