1#!/usr/bin/env python
2#  Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#      https://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16import time
17import rsa
18
19poolsize = 8
20accurate = True
21
22
23def run_speed_test(bitsize):
24    iterations = 0
25    start = end = time.time()
26
27    # At least a number of iterations, and at least 2 seconds
28    while iterations < 10 or end - start < 2:
29        iterations += 1
30        rsa.newkeys(bitsize, accurate=accurate, poolsize=poolsize)
31        end = time.time()
32
33    duration = end - start
34    dur_per_call = duration / iterations
35
36    print('%5i bit: %9.3f sec. (%i iterations over %.1f seconds)' %
37          (bitsize, dur_per_call, iterations, duration))
38
39
40if __name__ == '__main__':
41    for bitsize in (128, 256, 384, 512, 1024, 2048, 3072, 4096):
42        run_speed_test(bitsize)
43