From 484269dba29d3fa0b35d85b5ad9a302059ea0ef9 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Wed, 12 Apr 2023 15:34:36 +0200 Subject: [PATCH] Add block vs ensure benchmark --- .gitignore | 2 ++ block-ensure/README.md | 5 +++++ block-ensure/example.rb | 29 +++++++++++++++++++++++++++ shell.nix | 44 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 .gitignore create mode 100644 block-ensure/README.md create mode 100755 block-ensure/example.rb create mode 100644 shell.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e5cd9b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/bundle +/.envrc diff --git a/block-ensure/README.md b/block-ensure/README.md new file mode 100644 index 0000000..20c2ab0 --- /dev/null +++ b/block-ensure/README.md @@ -0,0 +1,5 @@ +``` + user system total real +block 0.961390 0.008933 0.970323 ( 0.978850) +ensure 0.752044 0.006407 0.758451 ( 0.763439) +``` diff --git a/block-ensure/example.rb b/block-ensure/example.rb new file mode 100755 index 0000000..eb903a2 --- /dev/null +++ b/block-ensure/example.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +require 'benchmark' + +class Example + def initialize + @mutex = Mutex.new + end + + def using_block + @mutex.synchronize do + + end + end + + def using_ensure + @mutex.lock + ensure + @mutex.unlock + end +end + +Benchmark.bm(12) do |x| + ex = Example.new + n = 10_000_000 + + x.report('block') { n.times { ex.using_block } } + x.report('ensure') { n.times { ex.using_ensure } } +end diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..bd3f656 --- /dev/null +++ b/shell.nix @@ -0,0 +1,44 @@ +{ + # use the environment channel + pkgs ? import {}, + + # use a pinned package state + pinned ? import(fetchTarball("https://github.com/NixOS/nixpkgs/archive/14d9b465c71.tar.gz")) {}, +}: +let + # specify ruby version to use + ruby = pinned.ruby_3_1; + + # control llvm/clang version (e.g for packages built from source) + llvm = pinned.llvmPackages_12; + + # control gcc version (e.g for packages built from source) + gcc = pinned.gcc12; +in llvm.stdenv.mkDerivation { + # unique project name for this environment derivation + name = "dd-trace-rb.devshell"; + + buildInputs = [ + ruby + + # TODO: some gems insist on using `gcc` on Linux, satisfy them for now: + # - json + # - protobuf + # - ruby-prof + gcc + ]; + + shellHook = '' + # get major.minor.0 ruby version + export RUBY_VERSION="$(ruby -e 'puts RUBY_VERSION.gsub(/\d+$/, "0")')" + + # make gem install work in-project, compatibly with bundler + export GEM_HOME="$(pwd)/vendor/bundle/ruby/$RUBY_VERSION" + + # make bundle work in-project + export BUNDLE_PATH="$(pwd)/vendor/bundle" + + # enable calling gem scripts without bundle exec + export PATH="$GEM_HOME/bin:$PATH" + ''; +}