forked from OpenHFT/Java-Thread-Affinity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJNIClockTest.java
More file actions
78 lines (70 loc) · 2.62 KB
/
Copy pathJNIClockTest.java
File metadata and controls
78 lines (70 loc) · 2.62 KB
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
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.ticker.impl;
import net.openhft.affinity.Affinity;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by peter on 13/07/15.
*/
public class JNIClockTest {
@Test
public void testNanoTime() throws Exception {
for (int i = 0; i < 20000; i++)
System.nanoTime();
Affinity.setAffinity(2);
JNIClock instance = JNIClock.INSTANCE;
for (int i = 0; i < 50; i++) {
long start0 = System.nanoTime();
long start1 = instance.ticks();
Thread.sleep(10);
long time0 = System.nanoTime();
long time1 = instance.ticks();
if (i > 1) {
assertEquals(10_100_000, time0 - start0, 100_000);
assertEquals(10_100_000, instance.toNanos(time1 - start1), 100_000);
assertEquals(instance.toNanos(time1 - start1) / 1e3, instance.toMicros(time1 - start1), 0.6);
}
}
}
@Test
@Ignore("Long running")
public void testJitter() {
Affinity.setAffinity(2);
int samples = 100000, count = 0;
long[] time = new long[samples];
long[] length = new long[samples];
JNIClock clock = JNIClock.INSTANCE;
long start = clock.ticks(), prev = start, prevJump = start;
for (int i = 0; i < 1000_000_000; i++) {
long now = clock.ticks();
long delta = now - prev;
if (delta > 4_000) {
time[count] = now - prevJump;
prevJump = now;
length[count] = delta;
count++;
if (count >= samples)
break;
}
prev = now;
}
for (int i = 0; i < count; i++) {
System.out.println(((long) (clock.toMicros(time[i]) * 10)) / 10.0 + ", " + ((long) (clock.toMicros(length[i]) * 10) / 10.0));
}
}
}