Skip to content

Commit 9a746ed

Browse files
committed
test/openssl/test_ossl.rb: use clock_gettime for measuring time
The benchmark library is planned to become a bundled gem in Ruby 3.5. While we can add it in our Gemfile, it is only used in test_memcmp_timing and the usage can be easily replaced with a few Process.clock_gettime calls.
1 parent fee4c4f commit 9a746ed

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

test/openssl/test_ossl.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ def test_secure_compare
4242
end
4343

4444
def test_memcmp_timing
45-
begin
46-
require "benchmark"
47-
rescue LoadError
48-
pend "Benchmark is not available in this environment. Please install it with `gem install benchmark`."
49-
end
50-
5145
# Ensure using fixed_length_secure_compare takes almost exactly the same amount of time to compare two different strings.
5246
# Regular string comparison will short-circuit on the first non-matching character, failing this test.
5347
# NOTE: this test may be susceptible to noise if the system running the tests is otherwise under load.
@@ -58,8 +52,14 @@ def test_memcmp_timing
5852

5953
a_b_time = a_c_time = 0
6054
100.times do
61-
a_b_time += Benchmark.measure { 100.times { OpenSSL.fixed_length_secure_compare(a, b) } }.real
62-
a_c_time += Benchmark.measure { 100.times { OpenSSL.fixed_length_secure_compare(a, c) } }.real
55+
t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
56+
100.times { OpenSSL.fixed_length_secure_compare(a, b) }
57+
t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
58+
100.times { OpenSSL.fixed_length_secure_compare(a, c) }
59+
t3 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
60+
61+
a_b_time += t2 - t1
62+
a_c_time += t3 - t2
6363
end
6464
assert_operator(a_b_time, :<, a_c_time * 10, "fixed_length_secure_compare timing test failed")
6565
assert_operator(a_c_time, :<, a_b_time * 10, "fixed_length_secure_compare timing test failed")

0 commit comments

Comments
 (0)