|
| 1 | +defmodule ServerSentEvents.Bench do |
| 2 | + @target_sizes [ |
| 3 | + {"1 MB", 1_048_576}, |
| 4 | + {"4 MB", 4_194_304} |
| 5 | + ] |
| 6 | + |
| 7 | + @data_lines_per_event 8 |
| 8 | + @data_chunk String.duplicate("abcdefghijklmnopqrstuvwxyz0123456789", 16) |
| 9 | + @incomplete_trim_bytes 97 |
| 10 | + |
| 11 | + def run do |
| 12 | + inputs = inputs() |
| 13 | + |
| 14 | + print_inputs(inputs) |
| 15 | + |
| 16 | + Benchee.run( |
| 17 | + %{ |
| 18 | + "complete large payload" => &parse_complete!/1, |
| 19 | + "incomplete large payload" => &parse_incomplete!/1 |
| 20 | + }, |
| 21 | + inputs: inputs, |
| 22 | + memory_time: 2, |
| 23 | + reduction_time: 0, |
| 24 | + time: 5, |
| 25 | + warmup: 2 |
| 26 | + ) |
| 27 | + end |
| 28 | + |
| 29 | + defp inputs do |
| 30 | + Map.new(@target_sizes, fn {label, target_bytes} -> |
| 31 | + {label, build_input(target_bytes)} |
| 32 | + end) |
| 33 | + end |
| 34 | + |
| 35 | + defp build_input(target_bytes) do |
| 36 | + events = build_events(target_bytes) |
| 37 | + complete_payload = IO.iodata_to_binary(events) |
| 38 | + complete_event_count = length(events) |
| 39 | + |
| 40 | + if complete_event_count < 2 do |
| 41 | + raise "expected at least two events for benchmark input generation" |
| 42 | + end |
| 43 | + |
| 44 | + last_event = List.last(events) |
| 45 | + incomplete_last_event_size = byte_size(last_event) - @incomplete_trim_bytes |
| 46 | + |
| 47 | + if incomplete_last_event_size <= 0 do |
| 48 | + raise "trailing event trim removed the entire event" |
| 49 | + end |
| 50 | + |
| 51 | + prefix_size = byte_size(complete_payload) - byte_size(last_event) |
| 52 | + |
| 53 | + incomplete_payload = |
| 54 | + binary_part(complete_payload, 0, prefix_size) <> |
| 55 | + binary_part(last_event, 0, incomplete_last_event_size) |
| 56 | + |
| 57 | + %{ |
| 58 | + complete_event_count: complete_event_count, |
| 59 | + complete_payload: complete_payload, |
| 60 | + incomplete_complete_event_count: complete_event_count - 1, |
| 61 | + incomplete_payload: incomplete_payload, |
| 62 | + incomplete_rest_bytes: incomplete_last_event_size |
| 63 | + } |
| 64 | + end |
| 65 | + |
| 66 | + defp build_events(target_bytes) do |
| 67 | + 1 |
| 68 | + |> Stream.iterate(&(&1 + 1)) |
| 69 | + |> Enum.reduce_while({[], 0}, fn id, {events, total_bytes} -> |
| 70 | + event = build_event(id) |
| 71 | + next_total_bytes = total_bytes + byte_size(event) |
| 72 | + |
| 73 | + cond do |
| 74 | + next_total_bytes < target_bytes -> |
| 75 | + {:cont, {[event | events], next_total_bytes}} |
| 76 | + |
| 77 | + events == [] -> |
| 78 | + {:cont, {[event | events], next_total_bytes}} |
| 79 | + |
| 80 | + true -> |
| 81 | + {:halt, Enum.reverse([event | events])} |
| 82 | + end |
| 83 | + end) |
| 84 | + end |
| 85 | + |
| 86 | + defp build_event(id) do |
| 87 | + [ |
| 88 | + "id: ", |
| 89 | + Integer.to_string(id), |
| 90 | + "\n", |
| 91 | + "event: completion.delta\n", |
| 92 | + build_data_lines(id), |
| 93 | + "\n" |
| 94 | + ] |
| 95 | + |> IO.iodata_to_binary() |
| 96 | + end |
| 97 | + |
| 98 | + defp build_data_lines(id) do |
| 99 | + for part <- 1..@data_lines_per_event do |
| 100 | + [ |
| 101 | + "data: {\"id\":", |
| 102 | + Integer.to_string(id), |
| 103 | + ",\"part\":", |
| 104 | + Integer.to_string(part), |
| 105 | + ",\"content\":\"", |
| 106 | + @data_chunk, |
| 107 | + "\"}\n" |
| 108 | + ] |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + defp parse_complete!(input) do |
| 113 | + case ServerSentEvents.parse(input.complete_payload) do |
| 114 | + {events, rest} -> |
| 115 | + if rest == "" and length(events) == input.complete_event_count do |
| 116 | + events |
| 117 | + else |
| 118 | + raise """ |
| 119 | + unexpected result for complete payload: |
| 120 | + events=#{length(events)} expected=#{input.complete_event_count} |
| 121 | + rest_bytes=#{byte_size(rest)} |
| 122 | + """ |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + defp parse_incomplete!(input) do |
| 128 | + case ServerSentEvents.parse(input.incomplete_payload) do |
| 129 | + {events, rest} -> |
| 130 | + if length(events) == input.incomplete_complete_event_count and |
| 131 | + byte_size(rest) == input.incomplete_rest_bytes do |
| 132 | + {events, rest} |
| 133 | + else |
| 134 | + raise """ |
| 135 | + unexpected result for incomplete payload: |
| 136 | + events=#{length(events)} expected=#{input.incomplete_complete_event_count} |
| 137 | + rest_bytes=#{byte_size(rest)} expected_rest_bytes=#{input.incomplete_rest_bytes} |
| 138 | + """ |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + defp print_inputs(inputs) do |
| 144 | + IO.puts("Benchmarking ServerSentEvents.parse/1") |
| 145 | + |
| 146 | + Enum.each(inputs, fn {label, input} -> |
| 147 | + IO.puts([ |
| 148 | + " ", |
| 149 | + label, |
| 150 | + ": complete=", |
| 151 | + format_bytes(byte_size(input.complete_payload)), |
| 152 | + " (", |
| 153 | + Integer.to_string(input.complete_event_count), |
| 154 | + " events), incomplete=", |
| 155 | + format_bytes(byte_size(input.incomplete_payload)), |
| 156 | + " (", |
| 157 | + Integer.to_string(input.incomplete_complete_event_count), |
| 158 | + " complete + ", |
| 159 | + format_bytes(input.incomplete_rest_bytes), |
| 160 | + " buffered)" |
| 161 | + ]) |
| 162 | + end) |
| 163 | + |
| 164 | + IO.puts("") |
| 165 | + end |
| 166 | + |
| 167 | + defp format_bytes(bytes) when bytes >= 1_048_576 do |
| 168 | + :io_lib.format("~.2f MB", [bytes / 1_048_576]) |> IO.iodata_to_binary() |
| 169 | + end |
| 170 | + |
| 171 | + defp format_bytes(bytes) when bytes >= 1024 do |
| 172 | + :io_lib.format("~.2f KB", [bytes / 1024]) |> IO.iodata_to_binary() |
| 173 | + end |
| 174 | + |
| 175 | + defp format_bytes(bytes) do |
| 176 | + "#{bytes} B" |
| 177 | + end |
| 178 | +end |
| 179 | + |
| 180 | +ServerSentEvents.Bench.run() |
0 commit comments