From ec247f5bce4200d573a4be828c01bc1cdb85e1a6 Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Sat, 30 Jan 2021 20:38:52 +0530 Subject: [PATCH] perf: add server timings --- src/main/java/me/kavin/piped/Main.java | 77 +++++++++++++++----------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index d86c933..1cb0545 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -30,172 +30,185 @@ public class Main { routes.get("/webhooks/pubsub", (req, res) -> { + long start = System.nanoTime(); QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { - return writeResponse(res, query.parameters().get("hub.challenge").get(0), 200, "private"); + return writeResponse(res, query.parameters().get("hub.challenge").get(0), 200, "private", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.post("/webhooks/pubsub", (req, res) -> { + long start = System.nanoTime(); + try { req.receive().asInputStream().subscribe(in -> { try { SyndFeed feed = new SyndFeedInput().build(new XmlReader(in)); - feed.getEntries().forEach(entry -> { - System.out.println(entry.getLinks().get(0).getHref()); - System.out.println(entry.getAuthors().get(0).getUri()); - }); + feed.getEntries().forEach(entry -> { + System.out.println(entry.getLinks().get(0).getHref()); + System.out.println(entry.getAuthors().get(0).getUri()); + }); } catch (Exception e) { e.printStackTrace(); } }); - return writeResponse(res, "ok", 200, "private"); + return writeResponse(res, "ok", 200, "private", start); } catch (Exception e) { - e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + e.printStackTrace(); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/sponsors/{videoId}", (req, res) -> { + long start = System.nanoTime(); QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { return writeResponse(res, SponsorBlockUtils.getSponsors(req.param("videoId"), - query.parameters().get("category").get(0)), 200, "public, max-age=3600"); + query.parameters().get("category").get(0)), 200, "public, max-age=3600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/streams/{videoId}", (req, res) -> { + long start = System.nanoTime(); try { // The stream links are valid for 6 hours. return writeResponse(res, ResponseHelper.streamsResponse(req.param("videoId")), 200, - "public, s-maxage=21540"); + "public, s-maxage=21540", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/channels/{channelId}", (req, res) -> { + long start = System.nanoTime(); try { return writeResponse(res, ResponseHelper.channelResponse(req.param("channelId")), 200, - "public, max-age=600"); + "public, max-age=600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/nextpage/channels/{channelId}", (req, res) -> { + long start = System.nanoTime(); QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { return writeResponse(res, ResponseHelper.channelPageResponse(req.param("channelId"), - query.parameters().get("url").get(0)), 200, "public, max-age=3600"); + query.parameters().get("url").get(0)), 200, "public, max-age=3600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/playlists/{playlistId}", (req, res) -> { + long start = System.nanoTime(); try { return writeResponse(res, ResponseHelper.playlistResponse(req.param("playlistId")), 200, - "public, max-age=600"); + "public, max-age=600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/nextpage/playlists/{playlistId}", (req, res) -> { + long start = System.nanoTime(); QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { return writeResponse(res, ResponseHelper.playlistPageResponse(req.param("playlistId"), - query.parameters().get("url").get(0)), 200, "public, max-age=3600"); + query.parameters().get("url").get(0)), 200, "public, max-age=3600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/suggestions", (req, res) -> { + long start = System.nanoTime(); QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { return writeResponse(res, ResponseHelper.suggestionsResponse(query.parameters().get("query").get(0)), 200, - "public, max-age=600"); + "public, max-age=600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/search", (req, res) -> { + long start = System.nanoTime(); QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { return writeResponse(res, ResponseHelper.searchResponse(query.parameters().get("q").get(0)), 200, - "public, max-age=600"); + "public, max-age=600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/nextpage/search", (req, res) -> { + long start = System.nanoTime(); QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { return writeResponse(res, ResponseHelper.searchPageResponse(query.parameters().get("q").get(0), query.parameters().get("url").get(0), query.parameters().get("id").get(0)), - 200, "public, max-age=3600"); + 200, "public, max-age=3600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); routes.get("/trending", (req, res) -> { + long start = System.nanoTime(); try { - return writeResponse(res, ResponseHelper.trendingResponse(), 200, "public, max-age=3600"); + return writeResponse(res, ResponseHelper.trendingResponse(), 200, "public, max-age=3600", start); } catch (Exception e) { e.printStackTrace(); - return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); + return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); } }); @@ -205,14 +218,16 @@ public class Main { Thread.sleep(Long.MAX_VALUE); } - public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache) { + public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache, long time) { return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache) + .addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0) .send(ByteBufFlux.fromString(Flux.just(resp), java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT)); } - public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache) { + public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache, long time) { return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache) + .addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0) .sendByteArray(Flux.just(resp)); }