Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add socket tree and schemas caching #7

Merged
merged 3 commits into from
May 13, 2024
Merged

Conversation

doorgan
Copy link
Contributor

@doorgan doorgan commented May 9, 2024

Closes #2

Adds a socket tree and shcemas cache module ChannelSpec.Cache with PersistentTermCache and NoneCache implementations.

This improves performance of code that needs to access the socket tree and the tree schemas several times(ie a socket Plug), while also providing a way to rebuild the schemas during development

The approximate performance improvements are:

  • 798.25%increase in Iterations per second
  • 99.35% decrease in memory usage(though I suspect this to be a bit questionable, 32B sounds like it might not have been measured correctly?)

Benchmarks

Without cache

Name                  ips        average  deviation         median         99th %
get schemas      787.08 K        1.27 μs  ±1931.28%        1.04 μs        2.17 μs

Memory usage statistics:

Name           Memory usage
get schemas         4.94 KB

Reduction count statistics:

Name        Reduction count
get schemas             343

With PersistentTermCache

Name                  ips        average  deviation         median         99th %
get schemas        7.07 M      141.40 ns ±20706.66%         125 ns         208 ns

Memory usage statistics:

Name           Memory usage
get schemas            32 B

Reduction count statistics:

Name        Reduction count
get schemas              19

With NoneCache

Name                  ips        average  deviation         median         99th %
get schemas      649.44 K        1.54 μs  ±1612.13%        1.21 μs        2.38 μs

Memory usage statistics:

Name           Memory usage
get schemas         5.02 KB

Reduction count statistics:

Name        Reduction count
get schemas             369

Benchmark code

defmodule BenchmarkTests do
  defmodule Socket do
    use ChannelSpec.Socket

    channel "foo:*", BenchmarkTests.Channel
  end

  defmodule Channel do
    use Phoenix.Channel
    use ChannelHandler.Router
    use ChannelSpec.Operations

    operation "baraaaaaaaa",
      payload: %{type: :object},
      replies: %{
        ok: %{type: :string},
        error: %{stype: :string}
      }

    handle "bar", fn _payload, _context, socket ->
      {:noreply, socket}
    end
  end

  def get_schemas do
    tree = Socket.__socket_tree__()
    
    unless tree.channels["foo:{string}"].messages["bar"].function == :handle_in do
      raise "invalid result"
    end
  end
end

Benchee.run(
  %{
    "get schemas" => &BenchmarkTests.get_schemas/0,
  },
  time: 10,
  memory_time: 4,
  reduction_time: 1
)

Test plan

Run the tests and verify they still work

Copy link
Contributor Author

doorgan commented May 9, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @doorgan and the rest of your teammates on Graphite Graphite

@doorgan doorgan marked this pull request as ready for review May 9, 2024 04:19
@doorgan doorgan marked this pull request as draft May 9, 2024 04:19
@doorgan doorgan marked this pull request as ready for review May 9, 2024 04:54
@doorgan doorgan merged commit e212c05 into main May 13, 2024
7 checks passed
@doorgan doorgan deleted the doorgan/cache_socket_tree branch May 13, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Resolve socket tree at runtime
2 participants