1# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import re
6from autotest_lib.client.bin import test, utils
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros import pkcs11
9
10class platform_Pkcs11LoadPerf(test.test):
11    """This tests the performance of loading a PKCS #11 token."""
12
13    version = 1
14
15    def run_once(self):
16        pkcs11.setup_p11_test_token(True)
17        pkcs11.load_p11_test_token()
18        # Prepare the token with a key.
19        utils.system('p11_replay --inject')
20        pkcs11.unload_p11_test_token()
21        pkcs11.load_p11_test_token()
22        # List the objects and gather timing data.
23        output = utils.system_output('p11_replay --list_objects')
24        # The output will have multiple lines like 'Elapsed: 25ms'. We are
25        # expecting at least three:
26        # 1) How long it took to open a session.
27        # 2) How long it took to list public objects.
28        # 3) How long it took to list private objects.
29        # The following extracts the numeric value from each timing statement.
30        time_list = [int(match.group(1)) for match in
31            re.finditer(r'Elapsed: (\d+)ms', output, flags=re.MULTILINE)]
32        if len(time_list) < 3:
33            error.TestFail('Expected output not found.')
34        self.output_perf_value(description='Key_Ready',
35                               value=(time_list[0] + time_list[1] + time_list[2]),
36                               units='ms', higher_is_better=False)
37        self.write_perf_keyval(
38            {'cert_ready_ms': time_list[0] + time_list[1],
39             'key_ready_ms': time_list[0] + time_list[1] + time_list[2]})
40        pkcs11.cleanup_p11_test_token()
41