use of com.facebook.buck.util.trace.BuildTraces.TraceAttributes in project buck by facebook.
the class TraceHandlerDelegate method getDataForRequest.
@Override
@Nullable
public SoyMapData getDataForRequest(Request baseRequest) throws IOException {
String path = baseRequest.getPathInfo();
Matcher matcher = TraceDataHandler.ID_PATTERN.matcher(path);
if (!matcher.matches()) {
return null;
}
SoyMapData templateData = new SoyMapData();
String id = matcher.group(1);
templateData.put("traceId", id);
// Read the args to `buck` out of the Chrome Trace.
TraceAttributes traceAttributes = buildTraces.getTraceAttributesFor(id);
templateData.put("dateTime", traceAttributes.getFormattedDateTime());
if (traceAttributes.getCommand().isPresent()) {
templateData.put("command", traceAttributes.getCommand().get());
}
return templateData;
}
use of com.facebook.buck.util.trace.BuildTraces.TraceAttributes in project buck by facebook.
the class TraceHandlerDelegateTest method testHandleGet.
@Test
public void testHandleGet() throws IOException, ServletException {
Request baseRequest = createMock(Request.class);
expect(baseRequest.getPathInfo()).andReturn("/abcdef");
baseRequest.setHandled(true);
HttpServletRequest request = createMock(HttpServletRequest.class);
HttpServletResponse response = createMock(HttpServletResponse.class);
response.setStatus(200);
response.setContentType("text/html; charset=utf-8");
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
expect(response.getWriter()).andReturn(printWriter);
response.flushBuffer();
BuildTraces buildTraces = createMock(BuildTraces.class);
expect(buildTraces.getTraceAttributesFor("abcdef")).andReturn(new TraceAttributes(Optional.of("buck build buck"), 2000L));
Handler traceHandler = new TemplateHandler(new TraceHandlerDelegate(buildTraces));
replayAll();
traceHandler.handle("/trace/abcdef", baseRequest, request, response);
verifyAll();
String expectedScriptTag = "<script src=\"/tracedata/abcdef?callback=onTracesLoaded\">";
String html = stringWriter.toString();
assertThat(html, containsString(expectedScriptTag));
assertThat(html, containsString("buck build buck"));
}
use of com.facebook.buck.util.trace.BuildTraces.TraceAttributes in project buck by facebook.
the class TracesHandlerTest method testHandleGet.
@Test
public void testHandleGet() throws IOException {
BuildTraces buildTraces = createMock(BuildTraces.class);
expect(buildTraces.getTraceAttributesFor(traceDir.resolve("build.a.trace"))).andReturn(new TraceAttributes(Optional.of("buck build buck"), 1000L));
expect(buildTraces.getTraceAttributesFor(traceDir.resolve("build.b.trace"))).andReturn(new TraceAttributes(Optional.of("buck test --all --code-coverage"), 4000L));
expect(buildTraces.getTraceAttributesFor(traceDir.resolve("build.c.trace"))).andReturn(new TraceAttributes(Optional.empty(), 2000L));
expect(buildTraces.getTraceAttributesFor(traceDir.resolve("build.d.trace"))).andReturn(new TraceAttributes(Optional.of("buck test //test/com/facebook/buck/cli:cli"), 3000L));
expect(buildTraces.listTraceFilesByLastModified()).andReturn(ImmutableList.of(traceDir.resolve("build.b.trace"), traceDir.resolve("build.d.trace"), traceDir.resolve("build.c.trace"), traceDir.resolve("build.a.trace")));
Request baseRequest = createMock(Request.class);
replayAll();
TracesHandlerDelegate delegate = new TracesHandlerDelegate(buildTraces);
TemplateHandler tracesHandler = new TemplateHandler(delegate);
String html = tracesHandler.createHtmlForResponse(baseRequest);
int indexB = html.indexOf("<a href=\"/trace/b\"><tt>build.b.trace</tt></a>");
assertTrue(indexB > 0);
int indexBCommand = html.indexOf("buck test --all --code-coverage");
assertTrue(indexBCommand > 0);
int indexD = html.indexOf("<a href=\"/trace/d\"><tt>build.d.trace</tt></a>");
assertTrue(indexD > indexB);
int indexDCommand = html.indexOf("buck test //test/com/facebook/buck/cli:cli");
assertTrue(indexDCommand > indexBCommand);
int indexC = html.indexOf("<a href=\"/trace/c\"><tt>build.c.trace</tt></a>");
assertTrue(indexC > indexD);
int indexA = html.indexOf("<a href=\"/trace/a\"><tt>build.a.trace</tt></a>");
assertTrue(indexA > indexC);
int indexACommand = html.indexOf("buck build buck");
assertTrue(indexACommand > indexDCommand);
verifyAll();
}
use of com.facebook.buck.util.trace.BuildTraces.TraceAttributes in project buck by facebook.
the class BuildTracesTest method testGetTraceAttributesForId.
@Test
public void testGetTraceAttributesForId() throws IOException {
FakeProjectFilesystem projectFilesystem = new FakeProjectFilesystem(new FakeClock(TimeUnit.MILLISECONDS.toNanos(1000L)));
projectFilesystem.writeContentsToPath("[\n" + " {\n" + " \"cat\": \"buck\",\n" + " \"pid\": 0,\n" + " \"ts\": 0,\n" + " \"ph\": \"M\",\n" + " \"args\": {\n" + " \"name\": \"buck\"\n" + " },\n" + " \"name\": \"process_name\",\n" + " \"tid\": 0\n" + " },\n" + " {\n" + " \"cat\": \"buck\",\n" + " \"name\": \"build\",\n" + " \"ph\": \"B\",\n" + " \"pid\": 0,\n" + " \"tid\": 1,\n" + " \"ts\": 5621911884918,\n" + " \"args\": {\n" + " \"command_args\": \"buck\"\n" + " }\n" + " }\n" + "]", projectFilesystem.getBuckPaths().getTraceDir().resolve("build.a.trace"));
BuildTraces helper = new BuildTraces(projectFilesystem);
TraceAttributes traceAttributes = helper.getTraceAttributesFor("a");
assertEquals("BuildTraces should be able to extract the command.", Optional.of("buck build buck"), traceAttributes.getCommand());
assertEquals(1000L, traceAttributes.getLastModifiedTime());
// We cannot verify the contents of getFormattedDateTime() because they may vary depending on
// timezone and locale.
assertNotNull(Strings.emptyToNull(traceAttributes.getFormattedDateTime()));
}
use of com.facebook.buck.util.trace.BuildTraces.TraceAttributes in project buck by facebook.
the class TracesHandlerDelegate method getTraces.
@VisibleForTesting
SoyListData getTraces() throws IOException {
List<Path> traceFiles = buildTraces.listTraceFilesByLastModified();
SoyListData traces = new SoyListData();
for (Path path : traceFiles) {
String name = path.getFileName().toString();
Matcher matcher = TRACE_FILE_NAME_PATTERN.matcher(name);
if (!matcher.matches()) {
// Could be build.trace or launch.xxx.trace.
continue;
}
SoyMapData trace = new SoyMapData();
trace.put("name", name);
trace.put("id", matcher.group(1));
TraceAttributes traceAttributes = buildTraces.getTraceAttributesFor(path);
trace.put("dateTime", traceAttributes.getFormattedDateTime());
if (traceAttributes.getCommand().isPresent()) {
trace.put("command", traceAttributes.getCommand().get());
} else {
trace.put("command", "");
}
traces.add(trace);
}
return traces;
}
Aggregations