forked from tenta-browser/goleveldb-encrypted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencypted_benchmark_test.go
161 lines (132 loc) · 3.64 KB
/
encypted_benchmark_test.go
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
/*
* JLevelDB Encrypted Storage
*
* Copyright (C) 2021 Jeffrey H. Johnson <[email protected]>
* Copyright (C) 2019 Tenta, LLC
*
* 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 jleveldbencrypted_test
import (
"io/ioutil"
"math/rand"
"os"
"testing"
ejdb "github.com/johnsonjh/jleveldb-encrypted"
"github.com/johnsonjh/jleveldb/leveldb"
"github.com/johnsonjh/jleveldb/leveldb/util"
)
var keys [][]byte
var (
shortKey = []byte{
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
}
longKey = []byte{
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
}
)
func initStrings(num, len int) {
keys = make([][]byte, num)
rand.Seed(int64(num + len))
for i := 0; i < num; i++ {
k := make([]byte, len)
rand.Read(k)
keys[i] = k
}
}
func rangeEnd(len int) []byte {
r := make([]byte, len+1)
for i := 0; i <= len; i++ {
r[i] = 0xff
}
return r
}
type bType int
var (
fileStorage bType = 1
encryptedStorage bType = 2
)
func loadBench(db *leveldb.DB) {
for i := 0; i < len(keys); i++ {
db.Put(keys[i], []byte{'a'}, nil)
}
db.CompactRange(util.Range{Start: nil, Limit: nil})
}
func doBenchMark(b *testing.B, tp bType, num, len int, key []byte) {
initStrings(num, len)
for n := 0; n < b.N; n++ {
dir, err := ioutil.TempDir("", "encrypted-jleveldb-bench")
if err != nil {
b.Fatal(err.Error())
}
if tp == fileStorage {
var db *leveldb.DB
db, err = leveldb.OpenFile(dir, nil)
if err != nil {
b.Fatal(err.Error())
}
loadBench(db)
db.Close()
} else if tp == encryptedStorage {
var edb *ejdb.EncryptedDB
edb, err = ejdb.OpenAESEncryptedFile(dir, key, nil)
if err != nil {
b.Fatal(err.Error())
}
loadBench(edb.DB)
edb.Close()
}
os.RemoveAll(dir)
}
}
func Benchmark_Normal_100keys_8bytes(b *testing.B) {
doBenchMark(b, fileStorage, 100, 8, shortKey)
}
func Benchmark_AES128_100keys_8bytes(b *testing.B) {
doBenchMark(b, encryptedStorage, 100, 8, shortKey)
}
func Benchmark_AES256_100keys_8bytes(b *testing.B) {
doBenchMark(b, encryptedStorage, 100, 8, longKey)
}
func Benchmark_Normal_10000keys_8bytes(b *testing.B) {
doBenchMark(b, fileStorage, 10000, 8, shortKey)
}
func Benchmark_AES128_10000keys_8bytes(b *testing.B) {
doBenchMark(b, encryptedStorage, 10000, 8, shortKey)
}
func Benchmark_AES256_10000keys_8bytes(b *testing.B) {
doBenchMark(b, encryptedStorage, 1000, 8, longKey)
}
func Benchmark_Normal_100keys_32bytes(b *testing.B) {
doBenchMark(b, fileStorage, 100, 32, shortKey)
}
func Benchmark_AES128_100keys_32bytes(b *testing.B) {
doBenchMark(b, encryptedStorage, 100, 32, shortKey)
}
func Benchmark_AES256_100keys_32bytes(b *testing.B) {
doBenchMark(b, encryptedStorage, 100, 32, longKey)
}
func Benchmark_Normal_10000keys_32bytes(b *testing.B) {
doBenchMark(b, fileStorage, 10000, 32, shortKey)
}
func Benchmark_AES128_10000keys_32bytes(b *testing.B) {
doBenchMark(b, encryptedStorage, 10000, 32, shortKey)
}
func Benchmark_AES256_10000keys_32bytes(b *testing.B) {
doBenchMark(b, encryptedStorage, 10000, 32, longKey)
}