-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_precise_sleep.py
56 lines (45 loc) · 1.34 KB
/
test_precise_sleep.py
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
import sys
import os
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(ROOT_DIR)
os.chdir(ROOT_DIR)
import time
import numpy as np
from diffusion_policy.common.precise_sleep import precise_sleep, precise_wait
def test_sleep():
dt = 0.1
tol = 1e-3
time_samples = list()
for i in range(100):
precise_sleep(dt)
# time.sleep(dt)
time_samples.append(time.monotonic())
time_deltas = np.diff(time_samples)
from matplotlib import pyplot as plt
plt.plot(time_deltas)
plt.ylim((dt-tol,dt+tol))
def test_wait():
dt = 0.1
tol = 1e-3
errors = list()
t_start = time.monotonic()
for i in range(1,100):
t_end_desired = t_start + i * dt
time.sleep(t_end_desired - time.monotonic())
t_end = time.monotonic()
errors.append(t_end - t_end_desired)
new_errors = list()
t_start = time.monotonic()
for i in range(1,100):
t_end_desired = t_start + i * dt
precise_wait(t_end_desired)
t_end = time.monotonic()
new_errors.append(t_end - t_end_desired)
from matplotlib import pyplot as plt
plt.plot(errors, label='time.sleep')
plt.plot(new_errors, label='sleep/spin hybrid')
plt.ylim((-tol,+tol))
plt.title('0.1 sec sleep error')
plt.legend()
if __name__ == '__main__':
test_sleep()