1+ /*
2+ * Copyright 2014 Higher Frequency Trading
3+ * <p/>
4+ * http://www.higherfrequencytrading.com
5+ * <p/>
6+ * Licensed under the Apache License, Version 2.0 (the "License");
7+ * you may not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ * <p/>
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ * <p/>
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+
19+ package net .openhft .affinity ;
20+
21+ import java .io .PrintStream ;
22+ /* e.g.
23+ After 2430 seconds, the average per hour was
24+ 2us 78400
25+ 3us 122703
26+ 4us 345238
27+ 6us 216098
28+ 8us 78694
29+ 10us 3977528
30+ 14us 114495
31+ 20us 4931
32+ 30us 203
33+ 40us 35
34+ 60us 18
35+ 80us 11
36+ 100us 9
37+ 140us 132
38+ 200us 85
39+ 300us 473
40+ 400us 5
41+ 1ms 24
42+ */
43+
44+ /**
45+ * User: peter.lawrey Date: 30/06/13 Time: 13:13
46+ */
47+ class MicroJitterSampler {
48+
49+ private static final long [] DELAY = {
50+ 2 * 1000 , 3 * 1000 , 4 * 1000 , 6 * 1000 , 8 * 1000 , 10 * 1000 , 14 * 1000 ,
51+ 20 * 1000 , 30 * 1000 , 40 * 1000 , 60 * 1000 , 80 * 1000 , 100 * 1000 , 140 * 1000 ,
52+ 200 * 1000 , 300 * 1000 , 400 * 1000 , 600 * 1000 , 800 * 1000 , 1000 * 1000 ,
53+ 2 * 1000 * 1000 , 5 * 1000 * 1000 , 10 * 1000 * 1000 ,
54+ 20 * 1000 * 1000 , 50 * 1000 * 1000 , 100 * 1000 * 1000
55+ };
56+ private static final double UTIL = Double .parseDouble (System .getProperty ("util" , "50" ));
57+ // static final int CPU = Integer.getInteger("cpu", 0);
58+ private final int [] count = new int [DELAY .length ];
59+ private long totalTime = 0 ;
60+
61+ public static void main (String ... ignored ) throws InterruptedException {
62+ // AffinityLock al = AffinityLock.acquireLock();
63+
64+ // warmup.
65+ new MicroJitterSampler ().sample (1000 * 1000 * 1000 );
66+
67+ MicroJitterSampler microJitterSampler = new MicroJitterSampler ();
68+ while (!Thread .currentThread ().isInterrupted ()) {
69+ if (UTIL >= 100 ) {
70+ microJitterSampler .sample (30L * 1000 * 1000 * 1000 );
71+ } else {
72+ long sampleLength = (long ) ((1 / (1 - UTIL / 100 ) - 1 ) * 1000 * 1000 );
73+ for (int i = 0 ; i < 30 * 1000 ; i += 2 ) {
74+ microJitterSampler .sample (sampleLength );
75+ //noinspection BusyWait
76+ Thread .sleep (1 );
77+ }
78+ }
79+
80+ microJitterSampler .print (System .out );
81+ }
82+ }
83+
84+ private static String asString (long timeNS ) {
85+ return timeNS < 1000 ? timeNS + "ns" :
86+ timeNS < 1000000 ? timeNS / 1000 + "us" :
87+ timeNS < 1000000000 ? timeNS / 1000000 + "ms" :
88+ timeNS / 1000000000 + "sec" ;
89+ }
90+
91+ void sample (long intervalNS ) {
92+ long prev = System .nanoTime ();
93+ long end = prev + intervalNS ;
94+ long now ;
95+ do {
96+ now = System .nanoTime ();
97+ long time = now - prev ;
98+ if (time >= DELAY [0 ]) {
99+ int i ;
100+ for (i = 1 ; i < DELAY .length ; i ++)
101+ if (time < DELAY [i ])
102+ break ;
103+ count [i - 1 ]++;
104+ }
105+ prev = now ;
106+ } while (now < end );
107+ totalTime += intervalNS ;
108+ }
109+
110+ void print (PrintStream ps ) {
111+ ps .println ("After " + totalTime / 1000000000 + " seconds, the average per hour was" );
112+ for (int i = 0 ; i < DELAY .length ; i ++) {
113+ if (count [i ] < 1 ) continue ;
114+ long countPerHour = (long ) Math .ceil (count [i ] * 3600e9 / totalTime );
115+ ps .println (asString (DELAY [i ]) + '\t' + countPerHour );
116+ }
117+ ps .println ();
118+ }
119+ }
0 commit comments