Search in sources :

Example 1 with TraceAttributes

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;
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) Matcher(java.util.regex.Matcher) TraceAttributes(com.facebook.buck.util.trace.BuildTraces.TraceAttributes) Nullable(javax.annotation.Nullable)

Example 2 with TraceAttributes

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"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) TraceAttributes(com.facebook.buck.util.trace.BuildTraces.TraceAttributes) Handler(org.eclipse.jetty.server.Handler) Matchers.containsString(org.hamcrest.Matchers.containsString) BuildTraces(com.facebook.buck.util.trace.BuildTraces) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 3 with TraceAttributes

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();
}
Also used : Request(org.eclipse.jetty.server.Request) TraceAttributes(com.facebook.buck.util.trace.BuildTraces.TraceAttributes) BuildTraces(com.facebook.buck.util.trace.BuildTraces) Test(org.junit.Test)

Example 4 with TraceAttributes

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()));
}
Also used : FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeClock(com.facebook.buck.timing.FakeClock) SettableFakeClock(com.facebook.buck.timing.SettableFakeClock) TraceAttributes(com.facebook.buck.util.trace.BuildTraces.TraceAttributes) Test(org.junit.Test)

Example 5 with TraceAttributes

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;
}
Also used : Path(java.nio.file.Path) SoyMapData(com.google.template.soy.data.SoyMapData) Matcher(java.util.regex.Matcher) TraceAttributes(com.facebook.buck.util.trace.BuildTraces.TraceAttributes) SoyListData(com.google.template.soy.data.SoyListData) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

TraceAttributes (com.facebook.buck.util.trace.BuildTraces.TraceAttributes)7 Test (org.junit.Test)5 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)3 FakeClock (com.facebook.buck.timing.FakeClock)3 SettableFakeClock (com.facebook.buck.timing.SettableFakeClock)3 BuildTraces (com.facebook.buck.util.trace.BuildTraces)2 SoyMapData (com.google.template.soy.data.SoyMapData)2 Matcher (java.util.regex.Matcher)2 Request (org.eclipse.jetty.server.Request)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SoyListData (com.google.template.soy.data.SoyListData)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Path (java.nio.file.Path)1 Nullable (javax.annotation.Nullable)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Handler (org.eclipse.jetty.server.Handler)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1