---
title: "Analysis for Logos Storage vs. Deluge Benchmarks - Static Network Dissemination Experiment"
output:
  bookdown::html_notebook2:
    number_sections: TRUE
    toc: TRUE
date: "2025-01-15"
---

# Introduction

This document contains the analysis for the Deluge vs. Logos Storage benchmarks. All data is obtained from our [benchmark suite](https://github.com/logos-storage/bittorrent-benchmarks/).
Each node runs in its own virtual machine, a [CPX31](https://www.hetzner.com/cloud) standard Hetzner virtual machine with $4$ shared vCPUs and $8\text{GB}$ of RAM. [iperf3](https://iperf.fr/) measurements conducted across nodes puts inter-node networking bandwidth at about $4.3\text{Gbps}$.

The benchmark consists in running a series of _static dissemination experiments_, where a file of size $b$ is disseminated across a swarm (set of nodes) of size $n$. Each swarm is split into a seeder set of size $s$ and a leecher (or downloader) set of size $l = n - s$. Seeders have the complete file at the start of the experiment, whereas leechers have nothing. The experiment consists in starting the leechers and then measuring the time it takes for each to download the file.

Leechers are started as closely as possible to each other so that they start downloading the file roughly at the same time. This stresses the network and, under these conditions,
 should provide us with a reasonable idea of what the lower bound on performance should be.

For a given network configuration $(n, s, l = n - s)$, we define it's seeder ratio as $r = s / n$. A higher seeder ratio should lead to faster dissemination, but if the swarms are homogeneous and scalable, the impact should not be large. We also expect close-to-constant performance for a given seeder ratio after for large enough swarms. Deviations from such behavior are likely issues.

We are then interested in asserting how system performance degrades under increasing file or swarm sizes. We expect larger files to take roughly linearly longer to download. We expect system performance to increase with swarm size up to a maximum. Deviations from this behavior likely reflect issues with the protocol.

Each experiment is ran $10$ times. We rotate seeders and leechers at random at every $5$ repetitions (so twice in total). This should allow us to account for performance differences that might arise from lack of overlay homogeneity or other factors.

```{r message=FALSE, echo = FALSE}
library(tidyverse)
library(bit64)

devtools::load_all()
```

```{r message = FALSE, include = !knitr::is_html_output()}
experiments <- read_all_experiments('./data/do/g1761924045/', label = 'deluge') |>
  merge_experiments(read_all_experiments('./data/do/g1762505060/', label = 'storage-baseline')) |>
  merge_experiments(read_all_experiments('./data/do/g1761729711/', label = 'storage-optimized')) |>
  merge_experiments(read_all_experiments('./data/do/g1775565300/', label = 'new-protocol'))
```

```{r include = !knitr::is_html_output()}
COUNT_DISTINCT = list(
  'codex_experiment_config_log_entry' = FALSE,
  'deluge_experiment_config_log_entry' = TRUE
)
```

```{r message = FALSE, include = !knitr::is_html_output()}
benchmarks <- lapply(experiments, function(experiment) {
  print(glue::glue('Process {experiment$experiment_id} - {experiment$label}'))
  download_time_stats <- tryCatch({
    meta <- experiment$meta
    completion <- experiment |>
      download_times(
        piece_count_distinct = COUNT_DISTINCT[[meta$experiment_type]]) |>
      completion_time_stats(meta)
    
    if (is.null(completion)) {
      NULL
    } else {
      completion |> mutate(
        experiment_type = meta$experiment_type,
        label = experiment$label,
        network_size = meta$nodes$network_size,
        seeders = meta$seeders,
        leechers = network_size - meta$seeders,
        file_size = meta$file_size
      )
    }
  }, error = function(e) { print(e); NULL })
}) |> 
  drop_nulls() |>
  bind_rows() |>
  arrange(file_size, network_size, seeders, leechers) |>
  mutate(
    file_size_bytes = file_size,
    # This factor conversion is horrible but needed so things are sorted properly in the plot.
    file_size = factor(rlang::parse_bytes(as.character(file_size)),
                        levels = rlang::parse_bytes(as.character(
                          unique(file_size[order(file_size, decreasing = TRUE)])))),
    seeder_ratio = seeders / network_size,
    completion_median_speed = file_size_bytes / completion_median,
    completion_p25_speed = file_size_bytes / completion_p25,
    completion_p75_speed = file_size_bytes / completion_p75,
    transfer_median_speed = file_size_bytes / transfer_median,
    transfer_p25_speed = file_size_bytes / transfer_p25,
    transfer_p75_speed = file_size_bytes / transfer_p75
  ) |>
  relocate(file_size, network_size, seeders, leechers, file_size_bytes)
```

# Results

```{r echo = FALSE}
benchmarks <- benchmarks |>
  group_by(experiment_type, label, network_size, seeders, leechers, file_size) |>
  slice_min(missing, n = 1, with_ties = FALSE) |>
  ungroup()
```

## Benchmark Data - Raw

Raw data in tabular format:

```{r echo = FALSE}
DT::datatable(
  benchmarks |> arrange(network_size, seeders),
  extensions = 'Buttons',
  options = list(
    dom = 'tBplr',
    searching = FALSE,
    buttons = c('copy', 'csv', 'excel'),
    scrollX = TRUE
  )
)
```

```{r echo = FALSE}
relative_performance <- compute_speedups(
  benchmarks = benchmarks,
  base = 'deluge',
  compare = c('storage-baseline', 'storage-optimized', 'new-protocol')
)
```

## Median Download Speed

```{r fig.cap='Median download speed for Deluge and Logos Storage', fig.width = 11, message = FALSE, echo = FALSE}
comparison_plot(
  benchmarks,
  completion_p25_speed,
  completion_p75_speed,
  completion_median_speed,
  ylab = 'median download speed (bytes/second)',
  free_y = TRUE
) + Y_BPS
```

## Median Transfer Speed

```{r fig.width = 11, message = FALSE, echo = FALSE}
comparison_plot(
  benchmarks,
  transfer_p25_speed,
  transfer_p75_speed,
  transfer_median_speed,
  ylab = 'median transfer speed (bytes/second)',
  free_y = TRUE
) + Y_BPS
```

## Median Download Time

```{r fig.cap='Median time to download a whole file for Deluge and Logos Storage', fig.width = 11, message = FALSE, echo = FALSE}
comparison_plot(
  benchmarks,
  completion_p25,
  completion_p75,
  completion_median,
  ylab = 'median download time',
  free_y = TRUE
) + Y_TIMESPAN
```

## Median Time to First Byte

The time elapsed from the moment in which we ask a node to download a file to the time in which it logs having downloaded the first $x\%$ of the file -- whatever the logging granularity is -- marks our time to first byte. This is actually an approximation which factors in _i)_ DHT lookup latency; _ii)_ swarm bootstrap latency; _iii)_ a fraction, typically $1/100^{th}$, of the download time. This should impact smaller files more than it impacts larger files.

```{r fig.cap='Median time-to-first-byte for Deluge and Logos Storage', fig.width = 11, message = FALSE, echo = FALSE}
comparison_plot(
  benchmarks,
  first_byte_p25,
  first_byte_p75,
  first_byte_median,
  ylab = 'median download time',
  free_y = TRUE
) + Y_TIMESPAN
```

## Median Download Time Ratio

Let $t_d$ and $t_c$ be the median times that Deluge and Logos Storage, respectively, take to download some file of a given size. The median download time ratio is defined as $m = t_c / t_d$.
When $m < 1$, Logos Storage is faster than Deluge. It is otherwise $m$ times slower to download the same file.

```{r fig.cap='Median downlaod time ratio for Logos Storage and Deluge', fig.width = 11, message = FALSE, echo = FALSE}
ggplot(relative_performance, aes(col = label, group = label)) +
  geom_line(aes(x = network_size, y = relative_median, col = label), lwd=1) +
  geom_hline(yintercept = 1, linetype = 'dashed', col = 'darkgray') +
  geom_point(aes(x = network_size, y = relative_median, col = label)) +
  ylab('median speedup/slowdown over Deluge') +
  annotate('text', label = 'faster', x = 29, y = 0, col = 'darkgreen') +
  annotate('text', label = 'slower', x = 28.5, y = 2, col = 'darkred') +
  theme_minimal(base_size=15) +
  scale_color_discrete(name = '') +
  facet_grid(
    file_size ~ seeder_ratio,
    labeller = labeller(
      file_size = as_labeller(function(x) x),
      seeder_ratio = as_labeller(function(x) {
        paste0("seeder ratio: ", scales::percent(as.numeric(x)))
      }))
  )
```
