| 1 |
import time |
| 2 |
import platform |
| 3 |
from rpython.rlib import rtimer |
| 4 |
from rpython.rtyper.test.test_llinterp import interpret |
| 5 |
from rpython.translator.c.test.test_genc import compile |
| 6 |
|
| 7 |
class TestTimer(object): |
| 8 |
|
| 9 |
@staticmethod |
| 10 |
def timer(): |
| 11 |
t1 = rtimer.read_timestamp() |
| 12 |
start = time.time() |
| 13 |
while time.time() - start < 0.1: |
| 14 |
|
| 15 |
pass |
| 16 |
t2 = rtimer.read_timestamp() |
| 17 |
return t2 - t1 |
| 18 |
|
| 19 |
def test_direct(self): |
| 20 |
diff = self.timer() |
| 21 |
|
| 22 |
assert diff > 1000 |
| 23 |
|
| 24 |
def test_annotation(self): |
| 25 |
diff = interpret(self.timer, []) |
| 26 |
assert diff > 1000 |
| 27 |
|
| 28 |
def test_compile_c(self): |
| 29 |
function = compile(self.timer, []) |
| 30 |
diff = function() |
| 31 |
assert diff > 1000 |
| 32 |
|
| 33 |
|
| 34 |
class TestGetUnit(object): |
| 35 |
|
| 36 |
@staticmethod |
| 37 |
def get_unit(): |
| 38 |
return rtimer.get_timestamp_unit() |
| 39 |
|
| 40 |
def test_direct(self): |
| 41 |
unit = self.get_unit() |
| 42 |
assert unit == rtimer.UNIT_NS |
| 43 |
|
| 44 |
def test_annotation(self): |
| 45 |
unit = interpret(self.get_unit, []) |
| 46 |
assert unit == rtimer.UNIT_NS |
| 47 |
|
| 48 |
def test_compile_c(self): |
| 49 |
function = compile(self.get_unit, []) |
| 50 |
unit = function() |
| 51 |
if platform.processor() in ('x86', 'x86_64'): |
| 52 |
assert unit == rtimer.UNIT_TSC |
| 53 |
else: |
| 54 |
assert unit in (rtimer.UNIT_TSC, |
| 55 |
rtimer.UNIT_NS, |
| 56 |
rtimer.UNIT_QUERY_PERFORMANCE_COUNTER) |