-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
99 lines (91 loc) · 2.64 KB
/
bench.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
package main
import (
"fmt"
"time"
"github.com/dgurney/unikey/generator"
"github.com/dgurney/unikey/validator"
)
/*
Copyright (C) 2021 Daniel Gurney
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// generationBenchmark generates amount * 3 keys and times it
func generationBenchmark(amount uint) {
oem := generator.Mod7OEM{}
cd := generator.Mod7CD{}
ecd := generator.Mod7ElevenCD{}
a := int(amount)
started := time.Now()
for i := 0; i < a; i++ {
oem.Generate()
cd.Generate()
ecd.Generate()
}
var ended time.Duration
switch {
case time.Since(started).Round(time.Second) > 1:
ended = time.Since(started).Round(time.Millisecond)
default:
ended = time.Since(started).Round(time.Microsecond)
}
fmt.Printf("Took %s to generate %d keys.\n", ended, amount*3)
}
// validationBenchmark validates amount * 3 keys and times it
func validationBenchmark(amount uint) {
oem := generator.Mod7OEM{}
cd := generator.Mod7CD{}
ecd := generator.Mod7ElevenCD{}
keys := make([]string, 0)
a := int(amount)
fmt.Printf("Generating %d keys for the test...\n", amount*3)
for i := 0; i < a; i++ {
oem.Generate()
cd.Generate()
ecd.Generate()
keys = append(keys, cd.String())
keys = append(keys, ecd.String())
keys = append(keys, oem.String())
}
var ki validator.KeyValidator
started := time.Now()
for _, k := range keys {
switch {
case len(k) == 12 && k[4:5] == "-":
ki = validator.Mod7ElevenCD{
First: k[0:4],
Second: k[5:12],
}
case len(k) == 11 && k[3:4] == "-":
ki = validator.Mod7CD{
First: k[0:3],
Second: k[4:11],
}
case len(k) == 23 && k[5:6] == "-" && k[9:10] == "-" && k[17:18] == "-" && len(k[18:]) == 5:
ki = validator.Mod7OEM{
First: k[0:5],
// nice
Second: k[6:9],
Third: k[10:17],
Fourth: k[18:],
}
}
ki.Validate()
}
var ended time.Duration
switch {
case time.Since(started).Round(time.Second) > 1:
ended = time.Since(started).Round(time.Millisecond)
default:
ended = time.Since(started).Round(time.Microsecond)
}
fmt.Printf("Took %s to validate %d keys.\n", ended, len(keys))
}