package com.oracle.truffle.tools.chromeinspector.types;
import java.util.concurrent.TimeUnit;
import com.oracle.truffle.tools.utils.json.JSONArray;
import com.oracle.truffle.tools.utils.json.JSONObject;
public final class Profile {
private final ProfileNode[] nodes;
private final long startTime;
private final long endTime;
private final TimeLineItem[] timeLine;
public Profile(ProfileNode[] nodes, long startTime, long endTime, TimeLineItem[] timeLine) {
this.nodes = nodes;
this.startTime = startTime;
this.endTime = endTime;
this.timeLine = timeLine;
}
public ProfileNode[] getNodes() {
return nodes;
}
public long getStartTime() {
return startTime;
}
public long getEndTime() {
return endTime;
}
public TimeLineItem[] getTimeLine() {
return timeLine;
}
public JSONObject toJSON() {
JSONObject json = new JSONObject();
json.put("nodes", ProfileNode.toJSON(nodes));
json.put("startTime", TimeUnit.MILLISECONDS.toMicros(startTime));
json.put("endTime", TimeUnit.MILLISECONDS.toMicros(endTime));
JSONArray samples = new JSONArray();
JSONArray timeDeltas = new JSONArray();
long lastTimestamp = startTime;
for (TimeLineItem item : timeLine) {
timeDeltas.put(TimeUnit.MILLISECONDS.toMicros(item.timestamp - lastTimestamp));
samples.put(item.id);
lastTimestamp = item.timestamp;
}
json.put("samples", samples);
json.put("timeDeltas", timeDeltas);
return json;
}
public static final class TimeLineItem {
private final long timestamp;
private final int id;
public TimeLineItem(long timestamp, int id) {
this.timestamp = timestamp;
this.id = id;
}
public long getTimestamp() {
return timestamp;
}
public int getId() {
return id;
}
}
}