diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 0000000..18eed2f --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +node-ruby-img diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..951d42f --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-2.4.2 diff --git a/Gemfile b/Gemfile index 925db3f..ae6f57b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source "https://rubygems.org" gem 'ruby-vips' +gem 'mini_magick' diff --git a/Rakefile b/Rakefile index d66c076..0e84f32 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,9 @@ namespace :run do + desc "Run Ruby Minimagick benchmark" + task "ruby:mini" do + sh "ruby ruby_io_speed_mm.rb" + end + desc "Run Ruby benchmark" task :ruby do sh "ruby ruby_io_speed.rb" @@ -15,7 +20,7 @@ namespace :run do end desc "Run all benchmarks" - task :all => [:ruby, :node, "node:streams"] + task :all => ["ruby:mini", :ruby, :node, "node:streams"] end namespace :install do diff --git a/mem.rb b/mem.rb new file mode 100644 index 0000000..be0dc6f --- /dev/null +++ b/mem.rb @@ -0,0 +1,29 @@ +require 'thread' + +# Credits: http://zogovic.com/post/59091704817/memory-usage-monitor-in-ruby + +class MemoryUsageMonitor + attr_reader :peak_memory, :log + + def initialize(frequency: 0.25) + @frequency = frequency + @peak_memory = 0 + @log = [] + end + + def start + @thread = Thread.new do + while true do + puts "#{Process::pid}" + memory = `ps -o rss -p #{Process::pid}`.chomp.split("\n").last.strip.to_i + @log.push memory + @peak_memory = [memory, @peak_memory].max + sleep @frequency + end + end + end + + def stop + Thread.kill(@thread) + end +end diff --git a/node_io_speed.js b/node_io_speed.js index 238edcd..4ee6ca9 100644 --- a/node_io_speed.js +++ b/node_io_speed.js @@ -12,7 +12,7 @@ var SIZES = { var performUpload = function(size, next){ var name = size + ".jpg" sharp('cakes.jpg') - .resize(SIZES[size][0], SIZES[size][1]) + .resize(SIZES[size][0]) .toFile(name, next); } diff --git a/node_io_speed_streams.js b/node_io_speed_streams.js index f9e3fe0..bcbb929 100644 --- a/node_io_speed_streams.js +++ b/node_io_speed_streams.js @@ -15,7 +15,7 @@ var performUpload = function(size, next){ var resizer = function(){ var client = sharp() return client.resize. - apply(client, SIZES[size]) + apply(client, [SIZES[size][0]]) } var stream = image.pipe(resizer(size)).pipe(fs.createWriteStream(name)) diff --git a/package-lock.json b/package-lock.json index 8e9bcb6..5bb0b45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,63 +9,274 @@ "resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz", "integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=" }, - "bluebird": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.0.tgz", - "integrity": "sha1-eRQg1/VR7qKJdFOop3ZT+WYG1nw=" - }, - "color": { - "version": "0.10.1", - "resolved": "https://registry.npmjs.org/color/-/color-0.10.1.tgz", - "integrity": "sha1-wEGI34KiCd3rzOzazT7DIPGTc58=", + "caw": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/caw/-/caw-2.0.1.tgz", + "integrity": "sha512-Cg8/ZSBEa8ZVY9HspcGUYaK63d/bN7rqS3CYCzEGUxuYv6UlmcjzDUz2fCFFHyTvUW5Pk0I+3hkA3iXlIj6guA==", "requires": { - "color-convert": "0.5.3", - "color-string": "0.3.0" + "get-proxy": "2.1.0", + "isurl": "1.0.0", + "tunnel-agent": "0.6.0", + "url-to-options": "1.0.1" } }, - "color-convert": { - "version": "0.5.3", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-0.5.3.tgz", - "integrity": "sha1-vbbGnOZg+t/+CwAHzER+G59ygr0=" + "chownr": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.0.1.tgz", + "integrity": "sha1-4qdQQqlVGQi+vSW4Uj1fl2nXkYE=" }, "color-name": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, - "color-string": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/color-string/-/color-string-0.3.0.tgz", - "integrity": "sha1-J9RvtnAlxcL6JZk7+/V55HhBuZE=", + "config-chain": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/config-chain/-/config-chain-1.1.11.tgz", + "integrity": "sha1-q6CXR9++TD5w52am5BWG4YWfxvI=", + "requires": { + "ini": "1.3.4", + "proto-list": "1.2.4" + } + }, + "decompress-response": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-3.3.0.tgz", + "integrity": "sha1-gKTdMjdIOEv6JICDYirt7Jgq3/M=", "requires": { - "color-name": "1.1.3" + "mimic-response": "1.0.0" } }, + "detect-libc": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-0.2.0.tgz", + "integrity": "sha1-R/31ZzSKF+wl/L8LnkRjSKdvn7U=" + }, "dotenv": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-1.2.0.tgz", "integrity": "sha1-fNc+FuB/BXyAchR6W8OoZ38KtcY=" }, + "get-proxy": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/get-proxy/-/get-proxy-2.1.0.tgz", + "integrity": "sha512-zmZIaQTWnNQb4R4fJUEp/FC51eZsc6EkErspy3xtIYStaq8EB/hDIWipxsal+E8rz0qD7f2sL/NA9Xee4RInJw==", + "requires": { + "npm-conf": "1.1.2" + } + }, + "has-symbol-support-x": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/has-symbol-support-x/-/has-symbol-support-x-1.4.1.tgz", + "integrity": "sha512-JkaetveU7hFbqnAC1EV1sF4rlojU2D4Usc5CmS69l6NfmPDnpnFUegzFg33eDkkpNCxZ0mQp65HwUDrNFS/8MA==" + }, + "has-to-string-tag-x": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/has-to-string-tag-x/-/has-to-string-tag-x-1.4.1.tgz", + "integrity": "sha512-vdbKfmw+3LoOYVr+mtxHaX5a96+0f3DljYd8JOqvOLsf5mw2Otda2qCDT9qRqLAhrjyQ0h7ual5nOiASpsGNFw==", + "requires": { + "has-symbol-support-x": "1.4.1" + } + }, + "ini": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.4.tgz", + "integrity": "sha1-BTfLedr1m1mhpRff9wbIbsA5Fi4=" + }, + "is-arrayish": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.1.tgz", + "integrity": "sha1-wt/DhquqDD4zxI2z/ocFnmkGXv0=" + }, + "is-object": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-object/-/is-object-1.0.1.tgz", + "integrity": "sha1-iVJojF7C/9awPsyF52ngKQMINHA=" + }, + "isurl": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isurl/-/isurl-1.0.0.tgz", + "integrity": "sha512-1P/yWsxPlDtn7QeRD+ULKQPaIaN6yF368GZ2vDfv0AL0NwpStafjWCDDdn0k8wgFMWpVAqG7oJhxHnlud42i9w==", + "requires": { + "has-to-string-tag-x": "1.4.1", + "is-object": "1.0.1" + } + }, + "mimic-response": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.0.tgz", + "integrity": "sha1-3z02Uqc/3ta5sLJBRub9BSNTRY4=" + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + }, + "minipass": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-2.2.1.tgz", + "integrity": "sha512-u1aUllxPJUI07cOqzR7reGmQxmCqlH88uIIsf6XZFEWgw7gXKpJdR+5R9Y3KEDmWYkdIz9wXZs3C0jOPxejk/Q==", + "requires": { + "yallist": "3.0.2" + } + }, + "minizlib": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/minizlib/-/minizlib-1.0.3.tgz", + "integrity": "sha1-1cGr93vhVGGZUuJTM27Mq5sqMvU=", + "requires": { + "minipass": "2.2.1" + } + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "requires": { + "minimist": "0.0.8" + } + }, "nan": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/nan/-/nan-2.7.0.tgz", "integrity": "sha1-2Vv3IeyHfgjbJ27T/G63j5CDrUY=" }, + "npm-conf": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/npm-conf/-/npm-conf-1.1.2.tgz", + "integrity": "sha512-dotwbpwVzfNB/2EF3A2wjK5tEMLggKfuA/8TG6WvBB1Zrv+JsvF7E8ei9B/HGq211st/GwXFbREcNJvJ1eySUQ==", + "requires": { + "config-chain": "1.1.11", + "pify": "3.0.0" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1.0.2" + } + }, + "pify": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=" + }, + "proto-list": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz", + "integrity": "sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=" + }, + "safe-buffer": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz", + "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==" + }, "semver": { "version": "5.4.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.4.1.tgz", "integrity": "sha512-WfG/X9+oATh81XtllIo/I8gOiY9EXRdv1cQdyykeXK17YcUW3EXUAi2To4pcH6nZtJPr7ZOpM5OMyWJZm+8Rsg==" }, "sharp": { - "version": "0.11.4", - "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.11.4.tgz", - "integrity": "sha1-90oencsS7Db/vPXePitioF/Zljw=", + "version": "0.18.4", + "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.18.4.tgz", + "integrity": "sha1-/jKcDwaJbCiqJDdt8f/wKuV/LTQ=", "requires": { - "bluebird": "3.5.0", - "color": "0.10.1", + "caw": "2.0.1", + "color": "2.0.0", + "detect-libc": "0.2.0", "nan": "2.7.0", - "semver": "5.4.1" + "semver": "5.4.1", + "simple-get": "2.7.0", + "tar": "3.2.1" + }, + "dependencies": { + "color": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/color/-/color-2.0.0.tgz", + "integrity": "sha1-4MmXLR6WmFcASxAeqlXOq1lh1n0=", + "requires": { + "color-convert": "1.9.0", + "color-string": "1.5.2" + } + }, + "color-convert": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.0.tgz", + "integrity": "sha1-Gsz5fdc5uYO/mU1W/sj5WFNkG3o=", + "requires": { + "color-name": "1.1.3" + } + }, + "color-string": { + "version": "1.5.2", + "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.5.2.tgz", + "integrity": "sha1-JuRYFLw8mny9Z1FkikFDRRSnc6k=", + "requires": { + "color-name": "1.1.3", + "simple-swizzle": "0.2.2" + } + } } + }, + "simple-concat": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.0.tgz", + "integrity": "sha1-c0TLuLbib7J9ZrL8hvn21Zl1IcY=" + }, + "simple-get": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-2.7.0.tgz", + "integrity": "sha512-RkE9rGPHcxYZ/baYmgJtOSM63vH0Vyq+ma5TijBcLla41SWlh8t6XYIGMR/oeZcmr+/G8k+zrClkkVrtnQ0esg==", + "requires": { + "decompress-response": "3.3.0", + "once": "1.4.0", + "simple-concat": "1.0.0" + } + }, + "simple-swizzle": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", + "integrity": "sha1-pNprY1/8zMoz9w0Xy5JZLeleVXo=", + "requires": { + "is-arrayish": "0.3.1" + } + }, + "tar": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/tar/-/tar-3.2.1.tgz", + "integrity": "sha512-ZSzds1E0IqutvMU8HxjMaU8eB7urw2fGwTq88ukDOVuUIh0656l7/P7LiVPxhO5kS4flcRJQk8USG+cghQbTUQ==", + "requires": { + "chownr": "1.0.1", + "minipass": "2.2.1", + "minizlib": "1.0.3", + "mkdirp": "0.5.1", + "yallist": "3.0.2" + } + }, + "tunnel-agent": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", + "integrity": "sha1-J6XeoGs2sEoKmWZ3SykIaPD8QP0=", + "requires": { + "safe-buffer": "5.1.1" + } + }, + "url-to-options": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/url-to-options/-/url-to-options-1.0.1.tgz", + "integrity": "sha1-FQWgOiiaSMvXpDTvuu7FBV9WM6k=" + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + }, + "yallist": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.2.tgz", + "integrity": "sha1-hFK0u36Dx8GI2AQcGoN8dz1ti7k=" } } } diff --git a/ruby_io_speed.rb b/ruby_io_speed.rb index 8d5e56c..696ed8f 100644 --- a/ruby_io_speed.rb +++ b/ruby_io_speed.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require "vips" ORIGINAL = "cakes.jpg" @@ -17,4 +18,6 @@ end end.each(&:join) -puts "Took: #{((Time.now - start) * 1000).to_i}ms" +stop = Time.now + +puts "Took: #{((stop - start) * 1000).to_i}ms" diff --git a/ruby_io_speed_mm.rb b/ruby_io_speed_mm.rb new file mode 100644 index 0000000..a6413f2 --- /dev/null +++ b/ruby_io_speed_mm.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true +require "mini_magick" + +ORIGINAL = "cakes.jpg" + +SIZES = { + large: [800, 800], + medium: [500, 500], + small: [300, 300], +} + +start = Time.now + +SIZES.map do |size, (width, height)| + Thread.new do + name = size.to_s + ".jpg" + MiniMagick::Image.open(ORIGINAL).resize("#{width}x#{height}").write(name) + end +end.each(&:join) + +stop = Time.now + +puts "Took: #{((stop - start) * 1000).to_i}ms"