Last active
September 11, 2019 19:08
-
-
Save krisselden/54a0d6122382cce2622ad17008caa67e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env Rscript | |
app.name = "My App" | |
# file you gave source-map-explorer | |
# source-map-explorer 1.0.1/bundle.js --tsv > bundle-1.0.1.tsv | |
# source-map-explorer 1.5.0/bundle.js --tsv > bundle-1.5.0.tsv | |
asset.file = "bundle.js" | |
before.version = "1.0.1" | |
before.file = "bundle-1.0.1.tsv" | |
before.description = "1.0.1 (Jun 01)" | |
after.version = "1.5.0" | |
after.description = "1.5.0 (Sep 01)" | |
after.file = "bundle-1.5.0.tsv" | |
before.tsv <- read.table(file=before.file, header=TRUE, stringsAsFactors=FALSE) | |
after.tsv <- read.table(file=after.file, header=TRUE, stringsAsFactors=FALSE) | |
versions <- list(before.tsv, after.tsv) | |
names(versions) <- c(before.version, after.version) | |
library(tidyverse) | |
# bind before and after to one dataset, adding a new column Version | |
sizes <- bind_rows(versions, .id = "Version") | |
sizes$Version = factor( | |
sizes$Version, levels=c(before.version, after.version)) | |
# truncate the Source paths to depth | |
truncate_path <- function (paths, depth=2) { | |
unname(sapply(paths, function (path) { | |
# split path by / and take the first 2 | |
components <- strsplit(path, "/")[[1]][seq(depth)] | |
# filter out NA if the path < depth | |
components <- components[!is.na(components)] | |
paste0(components, collapse="/") | |
})) | |
} | |
summed_sizes <- sizes %>% | |
mutate(Source = truncate_path(Source)) %>% | |
# .drop=FALSE if a Source exists in one Version but | |
# not the other Version it will still add a row for it with NA | |
group_by(Source, Version, .drop=FALSE) %>% | |
# Since we truncated the Source paths we need to sum | |
# the truncated Source by Version so we have only have | |
# 2 per version, we remove NA from the Sum so Size is 0 | |
# if a file does not exist | |
summarise(Size = sum(Size, na.rm=T)) %>% | |
# Group by Source and add a Change columns | |
group_by(Source, Change=diff(Size), AbsChange=abs(diff(Size))) %>% | |
# Arrange by absolute change for top 40 | |
arrange(desc(AbsChange)) | |
# take the top 40 absolute changes by Version | |
# Should be 80 rows now 1 Source per Version and 2 Versions | |
top_40 <- summed_sizes %>% | |
group_by(Version) %>% | |
top_n(40, AbsChange) | |
# order factor this affects how ggplot orders | |
# the discrete x axis (currently it is alphabetical by source) | |
top_40$Source = factor( | |
top_40$Source, | |
levels=unique(top_40$Source[order(top_40$AbsChange)])) | |
# file size labels | |
library(sitools) | |
label_size <- function (x) { | |
x[is.na(x)] <- 0 | |
f2si(x, unit="b") | |
} | |
abbreviate_label <- function (x) { | |
abbreviate(x, method="both", minlength=50) | |
} | |
# ggplot(top_40, aes(x = Source, y = Size, color = Version)) + | |
# geom_col(position="dodge") + | |
# scale_x_discrete(labels = abbreviate_label) + | |
# scale_y_continuous(labels = label_size) + | |
# coord_flip() + theme_classic() | |
# collaspe out Version | |
# the Change and AbsChange should be the same for each Version | |
changes <- top_40 %>% | |
group_by(Source) %>% | |
summarise(Change = max(Change), AbsChange = max(AbsChange)) %>% | |
# add a T/F column `Change >= 0` for maybe coloring | |
mutate(Change >= 0) %>% | |
arrange(desc(Change)) | |
# calculate hi lo breaks for scale_fill_gradientn aesthetic legend | |
hi <- max(changes$AbsChange); | |
# round up to 50kb | |
hi <- ceiling(hi/50000) * 50000 | |
lo <- -hi; | |
lims <- c(lo, hi); | |
breaks <- c(lo, 0, hi) # labels | |
# find 0 in a range of 0 to 1 | |
mid <- 1 - hi / (hi - lo) | |
# values 0-1 for color points on gradient | |
colors <- c("orchid1", "orchid3", "purple3", "purple1") | |
values <- c(0, mid, mid+0.001, 1) | |
ggplot(changes, aes(x = Source, y = Change)) + | |
geom_col(position="dodge", aes(fill = Change)) + | |
scale_x_discrete(labels = abbreviate_label) + | |
scale_y_continuous(labels = label_size) + | |
scale_fill_gradientn(colors = colors, | |
limits = lims, | |
breaks = breaks, | |
values = values, | |
labels = label_size) + | |
ggtitle(paste0(app.name, " Top 40 ", asset.file," Changes")) + | |
ylab(paste0("Change from ", before.description, " to ", after.description)) + | |
xlab(paste0("Source in ", asset.file)) + | |
coord_flip() + | |
theme_classic() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment