Minor code-style changes
Timings dump format change Pre-tests (not functional right now)
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
package mc.core.timings;
|
||||
|
||||
public interface MeasurableThread {
|
||||
ThreadTimings getTimings();
|
||||
}
|
||||
@@ -19,9 +19,9 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@Slf4j
|
||||
public class TimingsManager {
|
||||
private final Map<Thread, ThreadTimings> threadTimings = new ConcurrentHashMap<>();
|
||||
// These variables are essential in Timings thread synchronization
|
||||
private final AtomicBoolean waitForFile = new AtomicBoolean(false);
|
||||
private Map<Thread, ThreadTimings> threadTimings = new ConcurrentHashMap<>();
|
||||
private TimingsWriter writer;
|
||||
private Thread timingsIoThread;
|
||||
private CountDownLatch ioThreadStopMutex;
|
||||
@@ -139,8 +139,23 @@ public class TimingsManager {
|
||||
}
|
||||
|
||||
public ThreadTimings getCurrentThreadTimings() {
|
||||
if (Thread.currentThread() instanceof MeasurableThread) {
|
||||
return ((MeasurableThread) Thread.currentThread()).getTimings();
|
||||
} else return this.threadTimings.computeIfAbsent(Thread.currentThread(), s -> new ThreadTimings());
|
||||
|
||||
synchronized (this.threadTimings) {
|
||||
if (this.threadTimings.containsKey(Thread.currentThread())) {
|
||||
return this.threadTimings.get(Thread.currentThread());
|
||||
} else {
|
||||
ThreadTimings timings = new ThreadTimings();
|
||||
this.threadTimings.put(Thread.currentThread(), timings);
|
||||
if (queue != null) {
|
||||
try {
|
||||
writer.writeEvent(timings.getThreadId(), 0, System.nanoTime(), TimingsEventType.TIMINGS_CHANGE_THREAD_OPTIONS, "name: " + Thread.currentThread().getName());
|
||||
} catch (NullPointerException ignored) {
|
||||
// It means that there the file recording was stopped
|
||||
// we don't actually care about it
|
||||
}
|
||||
}
|
||||
return timings;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,10 +26,11 @@ public class TimingsFileWriter implements TimingsWriter {
|
||||
public void writeEvent(int threadId, int stackId, long time, TimingsEventType type) {
|
||||
lock.lock();
|
||||
try {
|
||||
writer.write(threadId);
|
||||
writer.write(stackId);
|
||||
writer.writeInt(threadId);
|
||||
writer.writeInt(stackId);
|
||||
writer.writeLong(time);
|
||||
writer.writeShort(type.getId());
|
||||
writer.writeBoolean(false);
|
||||
} catch (IOException e) {
|
||||
log.error("Unable to write timings record", e);
|
||||
} finally {
|
||||
@@ -41,10 +42,11 @@ public class TimingsFileWriter implements TimingsWriter {
|
||||
public void writeEvent(int threadId, int stackId, long time, TimingsEventType type, String data) {
|
||||
lock.lock();
|
||||
try {
|
||||
writer.write(threadId);
|
||||
writer.write(stackId);
|
||||
writer.writeInt(threadId);
|
||||
writer.writeInt(stackId);
|
||||
writer.writeLong(time);
|
||||
writer.writeShort(type.getId());
|
||||
writer.writeBoolean(true);
|
||||
writer.writeUTF(data);
|
||||
} catch (IOException e) {
|
||||
log.error("Unable to write timings record", e);
|
||||
|
||||
51
event-loop/src/test/java/mc/core/timings/TimingsTest.java
Normal file
51
event-loop/src/test/java/mc/core/timings/TimingsTest.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package mc.core.timings;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class TimingsTest {
|
||||
@Test
|
||||
public void basicTest() {
|
||||
try (Timings timings = Timings.start()) {
|
||||
System.out.println("Test code");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void brokenTimingTest() {
|
||||
try (Timings timings = Timings.start()) {
|
||||
Timings t1 = Timings.start();
|
||||
Timings.start();
|
||||
System.out.println("Pre Close t1");
|
||||
t1.close();
|
||||
System.out.println("Finished");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fileRecording() throws IOException {
|
||||
Timings.getTimingsManager().startRecording(new File("test.timings"));
|
||||
|
||||
try (Timings t1 = Timings.start()) {
|
||||
try {
|
||||
Thread.sleep(20);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try (Timings t2 = Timings.start()) {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Thread.sleep(5);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
Timings.getTimingsManager().stopRecording();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user