Skip to content

Commit

Permalink
Introduce RuboCop (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan authored Jan 10, 2025
1 parent ac2112c commit 02673a4
Show file tree
Hide file tree
Showing 68 changed files with 2,057 additions and 2,245 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ name: Lint Ruby

on:
workflow_dispatch:
# TODO: Autocorrect first
# push:
# branches:
# - main
# pull_request:
push:
branches:
- main
pull_request:

jobs:
rubocop:
Expand Down
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ AllCops:
- 'lib/.rbnext/**/*'
- 'lib/generators/**/templates/*.rb'
- '.github/**/*'
- 'examples/**/*'
- 'benchmark/**/*'
DisplayCopNames: true
SuggestExtensions: false
NewCops: disable
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Gem Version](https://badge.fury.io/rb/nats-pure.svg)](https://rubygems.org/gems/nats-pure)
[![License Apache 2.0](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Build](https://github.com/nats-io/nats-pure/workflows/Build/badge.svg)](https://github.com/nats-io/nats-pure/actions)
[![Build](https://github.com/nats-io/nats-pure.rb/workflows/Build/badge.svg)](https://github.com/nats-io/nats-pure.rb/actions)
[![Documentation](https://img.shields.io/badge/docs-link-brightgreen.svg)](https://docs.nats.io/)

# NATS: Pure Ruby Client
Expand Down
3 changes: 1 addition & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ rescue LoadError
task(:rubocop) {}
end

# TODO: Add rubocop as soon as we fix the style issues
task default: %w[spec]
task default: %w[rubocop spec]
30 changes: 16 additions & 14 deletions benchmark/latency_perf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Copyright 2016-2018 The NATS Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,14 +14,14 @@
# limitations under the License.
#

require 'optparse'
require "optparse"

$LOAD_PATH << File.expand_path('../../lib', __FILE__)
require 'nats/io/client'
$LOAD_PATH << File.expand_path("../../lib", __FILE__)
require "nats/io/client"

$loop = 10000
$hash = 250
$sub = 'test'
$sub = "test"

$subscriptions = 1
$concurrency = 1
Expand All @@ -33,7 +35,7 @@
opts.separator ""
opts.separator "options:"

opts.on("-s SUBJECT", "Send subject (default: #{$sub})") { |sub| $sub = sub }
opts.on("-s SUBJECT", "Send subject (default: #{$sub})") { |sub| $sub = sub }
opts.on("-n ITERATIONS", "iterations to expect (default: #{$loop})") { |iter| $loop = iter.to_i }
opts.on("-c SUBSCRIPTIONS", "Subscription number (default: (#{$subscriptions})") { |subscriptions| $subscriptions = subscriptions.to_i }
opts.on("-t CONCURRENCY", "Subscription processing concurrency (default: (#{$concurrency})") { |concurrency| $concurrency = concurrency.to_i }
Expand All @@ -44,7 +46,7 @@
$drain = $loop

trap("TERM") { exit! }
trap("INT") { exit! }
trap("INT") { exit! }

nats = NATS::IO::Client.new

Expand All @@ -61,19 +63,19 @@
puts "nats: reconnected!"
end

nats.connect(:max_reconnect => 10)
nats.connect(max_reconnect: 10)

if $queue
$subscriptions.times do
sub = nats.subscribe($sub, queue: $queue) do |msg|
msg.respond("OK:"+msg.data)
msg.respond("OK:" + msg.data)
end
sub.processing_concurrency = $concurrency
end
else
$subscriptions.times do
sub = nats.subscribe($sub) do |msg|
msg.respond("OKOK:"+msg.data)
msg.respond("OKOK:" + msg.data)
end
sub.processing_concurrency = $concurrency
end
Expand All @@ -87,17 +89,17 @@
loop do
begin
nats.request($sub, "AAA-#{$drain}", timeout: 2)
rescue NATS::IO::Timeout => e
rescue NATS::IO::Timeout
timeouts += 1
end

$drain-=1
$drain -= 1
if $drain == 0
ms = "%.2f" % (((Time.now-$start)/$loop)*1000.0)
ms = "%.2f" % (((Time.now - $start) / $loop) * 1000.0)
puts "\nTest completed : #{ms} ms avg request/response latency\n"
puts "Timeouts: #{timeouts}" if timeouts > 0
exit!
else
printf('#') if $drain.modulo($hash) == 0
elsif $drain.modulo($hash) == 0
printf("#")
end
end
10 changes: 6 additions & 4 deletions benchmark/nuid_perf.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'securerandom'
require 'nats/nuid'
require 'benchmark/ips'
# frozen_string_literal: true

require "securerandom"
require "nats/nuid"
require "benchmark/ips"

Benchmark.ips do |x|
x.report "NUID based inboxes with locked instance" do |t|
Expand All @@ -15,6 +17,6 @@
x.report "SecureRandom based inboxes" do |t|
t.times { "_INBOX.#{::SecureRandom.hex(11)}" }
end

x.compare!
end
42 changes: 21 additions & 21 deletions benchmark/pub_perf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Copyright 2016-2018 The NATS Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,24 +14,24 @@
# limitations under the License.
#

require 'optparse'
require "optparse"

$:.unshift File.expand_path('../../lib', __FILE__)
require 'nats/io/client'
$:.unshift File.expand_path("../../lib", __FILE__)
require "nats/io/client"

$count = 100000
$batch = 100

$delay = 0.00001
$dmin = 0.00001
$dmin = 0.00001

TRIP = (2*1024*1024)
TSIZE = 4*1024
TRIP = (2 * 1024 * 1024)
TSIZE = 4 * 1024

$sub = 'test'
$sub = "test"
$data_size = 16

$hash = 2500
$hash = 2500

$stdout.sync = true

Expand All @@ -39,43 +41,41 @@
opts.separator ""
opts.separator "options:"

opts.on("-n COUNT", "Messages to send (default: #{$count}}") { |count| $count = count.to_i }
opts.on("-s SIZE", "Message size (default: #{$data_size})") { |size| $data_size = size.to_i }
opts.on("-S SUBJECT", "Send subject (default: (#{$sub})") { |sub| $sub = sub }
opts.on("-b BATCH", "Batch size (default: (#{$batch})") { |batch| $batch = batch.to_i }
opts.on("-n COUNT", "Messages to send (default: #{$count}}") { |count| $count = count.to_i }
opts.on("-s SIZE", "Message size (default: #{$data_size})") { |size| $data_size = size.to_i }
opts.on("-S SUBJECT", "Send subject (default: (#{$sub})") { |sub| $sub = sub }
opts.on("-b BATCH", "Batch size (default: (#{$batch})") { |batch| $batch = batch.to_i }
end

parser.parse(ARGV)

trap("TERM") { exit! }
trap("INT") { exit! }
trap("INT") { exit! }

$data = Array.new($data_size) { "%01x" % rand(16) }.join('').freeze
$data = Array.new($data_size) { "%01x" % rand(16) }.join("").freeze

nats = NATS::IO::Client.new
nats.connect

$batch = 10 if $data_size >= TSIZE
$start = Time.now
$start = Time.now
$to_send = $count

puts "Sending #{$count} messages of size #{$data.size} bytes on [#{$sub}]"

loop do
(0..$batch).each do
$to_send -= 1
nats.publish($sub, $data)
if $to_send == 0
nats.publish($sub, $data)
nats.flush

elapsed = Time.now - $start
mbytes = sprintf("%.1f", (($data_size*$count)/elapsed)/(1024*1024))
puts "\nTest completed : #{($count/elapsed).ceil} sent/received msgs/sec (#{mbytes} MB/sec)\n"
mbytes = sprintf("%.1f", (($data_size * $count) / elapsed) / (1024 * 1024))
puts "\nTest completed : #{($count / elapsed).ceil} sent/received msgs/sec (#{mbytes} MB/sec)\n"
exit
else
nats.publish($sub, $data)
end
sleep $delay if $to_send.modulo(1000) == 0
printf('#') if $to_send.modulo($hash) == 0
printf("#") if $to_send.modulo($hash) == 0
end
end
51 changes: 31 additions & 20 deletions benchmark/pub_sub_perf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Copyright 2016-2018 The NATS Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,12 +14,12 @@
# limitations under the License.
#

require 'optparse'
require 'concurrent'
require 'ruby-progressbar'
require "optparse"
require "concurrent"
require "ruby-progressbar"

$:.unshift File.expand_path('../../lib', __FILE__)
require 'nats/io/client'
$:.unshift File.expand_path("../../lib", __FILE__)
require "nats/io/client"

$count = 100000
$batch = 100
Expand All @@ -27,9 +29,9 @@

$delay = 0.00001

TSIZE = 4*1024
TSIZE = 4 * 1024

$sub = 'test'
$sub = "test"
$data_size = 16

$stdout.sync = true
Expand All @@ -40,34 +42,40 @@
opts.separator ""
opts.separator "options:"

opts.on("-n COUNT", "Messages to send (default: #{$count}}") { |count| $count = count.to_i }
opts.on("-s SIZE", "Message size (default: #{$data_size})") { |size| $data_size = size.to_i }
opts.on("-S SUBJECT", "Send subject (default: (#{$sub})") { |sub| $sub = sub }
opts.on("-b BATCH", "Batch size (default: (#{$batch})") { |batch| $batch = batch.to_i }
opts.on("-n COUNT", "Messages to send (default: #{$count}}") { |count| $count = count.to_i }
opts.on("-s SIZE", "Message size (default: #{$data_size})") { |size| $data_size = size.to_i }
opts.on("-S SUBJECT", "Send subject (default: (#{$sub})") { |sub| $sub = sub }
opts.on("-b BATCH", "Batch size (default: (#{$batch})") { |batch| $batch = batch.to_i }
opts.on("-c SUBSCRIPTIONS", "Subscription number (default: (#{$subscriptions})") { |subscriptions| $subscriptions = subscriptions.to_i }
opts.on("-t CONCURRENCY", "Subscription processing concurrency (default: (#{$concurrency})") { |concurrency| $concurrency = concurrency.to_i }
end

parser.parse(ARGV)

$data = Array.new($data_size) { "%01x" % rand(16) }.join('').freeze
$data = Array.new($data_size) { "%01x" % rand(16) }.join("").freeze

puts "Sending #{$count} messages of size #{$data.size} bytes on [#{$sub}], receiving each in #{$subscriptions} subscriptions"

$progressbar = ProgressBar.create(title: "Received", total: $count*$subscriptions, format: '%t: |%B| %p%% %a %e', autofinish: false, throttle_rate: 0.1)
$progressbar = ProgressBar.create(title: "Received", total: $count * $subscriptions, format: "%t: |%B| %p%% %a %e", autofinish: false, throttle_rate: 0.1)

def results
elapsed = Time.now - $start
mbytes = sprintf("%.1f", (($data_size*$received)/elapsed)/(1024*1024))
elapsed = Time.now - $start
mbytes = sprintf("%.1f", (($data_size * $received) / elapsed) / (1024 * 1024))
<<~MSG
Test completed: #{($received/elapsed).ceil} received msgs/sec (#{mbytes} MB/sec)
Test completed: #{($received / elapsed).ceil} received msgs/sec (#{mbytes} MB/sec)
Received #{$received} messages in #{elapsed} seconds
MSG
end

trap("TERM") { puts results; exit! }
trap("INT") { puts results; exit! }
trap("TERM") {
puts results
exit!
}
trap("INT") {
puts results
exit!
}

nats = NATS::IO::Client.new
nats.connect
Expand All @@ -77,11 +85,14 @@ def results
$received = 0

$subscriptions.times do
subscription = nats.subscribe($sub) { $received += 1; $progressbar.progress = $received }
subscription = nats.subscribe($sub) {
$received += 1
$progressbar.progress = $received
}
subscription.processing_concurrency = $concurrency
end

$start = Time.now
$start = Time.now
$to_send = $count

loop do
Expand Down
Loading

0 comments on commit 02673a4

Please sign in to comment.