summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/util/timer.py
blob: 1b584576f077315a5fd472c0749db50a56e8620d (plain) (blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import json
from io import StringIO

import spack.util.timer as timer


class Tick:
    """Timer that increments the seconds passed by 1
    everytime tick is called."""

    def __init__(self):
        self.time = 0.0

    def tick(self):
        self.time += 1
        return self.time


def test_timer():
    # 0
    t = timer.Timer(now=Tick().tick)

    # 1 (restart)
    t.start()

    # 2
    t.start("wrapped")

    # 3
    t.start("first")

    # 4
    t.stop("first")
    assert t.duration("first") == 1.0

    # 5
    t.start("second")

    # 6
    t.stop("second")
    assert t.duration("second") == 1.0

    # 7-8
    with t.measure("third"):
        pass
    assert t.duration("third") == 1.0

    # 9
    t.stop("wrapped")
    assert t.duration("wrapped") == 7.0

    # tick 10-13
    t.start("not-stopped")
    assert t.duration("not-stopped") == 1.0
    assert t.duration("not-stopped") == 2.0
    assert t.duration("not-stopped") == 3.0

    # 14
    assert t.duration() == 13.0

    # 15
    t.stop()
    assert t.duration() == 14.0


def test_timer_stop_stops_all():
    # Ensure that timer.stop() effectively stops all timers.

    # 0
    t = timer.Timer(now=Tick().tick)

    # 1
    t.start("first")

    # 2
    t.start("second")

    # 3
    t.start("third")

    # 4
    t.stop()

    assert t.duration("first") == 3.0
    assert t.duration("second") == 2.0
    assert t.duration("third") == 1.0
    assert t.duration() == 4.0


def test_stopping_unstarted_timer_is_no_error():
    t = timer.Timer(now=Tick().tick)
    assert t.duration("hello") == 0.0
    t.stop("hello")
    assert t.duration("hello") == 0.0


def test_timer_write():
    text_buffer = StringIO()
    json_buffer = StringIO()

    # 0
    t = timer.Timer(now=Tick().tick)

    # 1
    t.start("timer")

    # 2
    t.stop("timer")

    # 3
    t.stop()

    t.write_tty(text_buffer)
    t.write_json(json_buffer)

    output = text_buffer.getvalue().splitlines()
    assert "timer" in output[0]
    assert "1.000s" in output[0]
    assert "total" in output[1]
    assert "3.000s" in output[1]

    deserialized = json.loads(json_buffer.getvalue())
    assert deserialized == {
        "phases": [{"name": "timer", "path": "timer", "seconds": 1.0, "count": 1}],
        "total": 3.0,
    }


def test_null_timer():
    # Just ensure that the interface of the noop-timer doesn't break at some point
    buffer = StringIO()
    t = timer.NullTimer()
    t.start()
    t.start("first")
    t.stop("first")
    with t.measure("second"):
        pass
    t.stop()
    assert t.duration("first") == 0.0
    assert t.duration() == 0.0
    assert not t.phases
    t.write_json(buffer)
    t.write_tty(buffer)
    assert not buffer.getvalue()