| 1 |
/* |
| 2 |
* hashing_test.go - tests for computing and benchmarking hashing costs |
| 3 |
* |
| 4 |
* Copyright 2017 Google Inc. |
| 5 |
* Author: Joe Richey ([email protected]) |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
* use this file except in compliance with the License. You may obtain a copy of |
| 9 |
* the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
* License for the specific language governing permissions and limitations under |
| 17 |
* the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package actions |
| 21 |
|
| 22 |
import ( |
| 23 |
"io" |
| 24 |
"log" |
| 25 |
"testing" |
| 26 |
"time" |
| 27 |
) |
| 28 |
|
| 29 |
// Tests that we can find valid hashing costs for various time targets and the |
| 30 |
// estimations are somewhat close to the targets. |
| 31 |
func TestCostsSearch(t *testing.T) { |
| 32 |
for _, target := range []time.Duration{ |
| 33 |
100 * time.Millisecond, |
| 34 |
200 * time.Millisecond, |
| 35 |
500 * time.Millisecond, |
| 36 |
} { |
| 37 |
costs, err := getHashingCosts(target) |
| 38 |
if err != nil { |
| 39 |
t.Error(err) |
| 40 |
} |
| 41 |
actual, err := timeHashingCosts(costs) |
| 42 |
if err != nil { |
| 43 |
t.Error(err) |
| 44 |
} |
| 45 |
|
| 46 |
if actual*3 < target { |
| 47 |
t.Errorf("actual=%v is too small (target=%v)", actual, target) |
| 48 |
} |
| 49 |
if target*3 < actual { |
| 50 |
t.Errorf("actual=%v is too big (target=%v)", actual, target) |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func benchmarkCostsSearch(b *testing.B, target time.Duration) { |
| 56 |
// Disable logging for benchmarks |
| 57 |
log.SetOutput(io.Discard) |
| 58 |
for i := 0; i < b.N; i++ { |
| 59 |
_, err := getHashingCosts(target) |
| 60 |
if err != nil { |
| 61 |
b.Fatal(err) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func BenchmarkCostsSearch10ms(b *testing.B) { |
| 67 |
benchmarkCostsSearch(b, 10*time.Millisecond) |
| 68 |
} |
| 69 |
|
| 70 |
func BenchmarkCostsSearch100ms(b *testing.B) { |
| 71 |
benchmarkCostsSearch(b, 100*time.Millisecond) |
| 72 |
} |
| 73 |
|
| 74 |
func BenchmarkCostsSearch1s(b *testing.B) { |
| 75 |
benchmarkCostsSearch(b, time.Second) |
| 76 |
} |