Skip to content

Commit 12f858a

Browse files
authored
Merge branch 'master' into fix-specs
2 parents 480a157 + aa663a7 commit 12f858a

11 files changed

+95
-53
lines changed

BENCHMARKS.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ On average, Litecable is quite faster than the Redis based version and offers be
125125

126126
> ![litesearch](https://github.com/oldmoe/litestack/blob/master/assets/litesearch_logo_teal.png?raw=true)
127127
128-
Litesearch was benchmarked against Mielsearch, both using their respective ActiveRecord integrations. Mielsearch was running on the same machine as the benchmark script and was using the default configuration options. The dataset used for testing was the infamous Enron email corpus. Redisearch was not benchmarked due to the clients being not Rails 7.1 compatible (yet), will probably bench Redisearch when they are.
128+
Litesearch was benchmarked against Meilisearch, both using their respective ActiveRecord integrations. Meilisearch was running on the same machine as the benchmark script and was using the default configuration options. The dataset used for testing was the infamous Enron email corpus. Redisearch was not benchmarked due to the clients being not Rails 7.1 compatible (yet), will probably bench Redisearch when they are.
129129

130130
### Building the index
131131

132-
||MieliSearch|Litesearch|
132+
||Meilisearch|Litesearch|
133133
|-:|-:|-:|
134134
|Time to insert 10K docs|265.42 seconds|29.06 seconds|
135135
|Inserted docs/second|38|344|
136136
|Search latency (3 terms)|7.51 ms| 0.051ms|
137137
|Searches/second|133|19608|
138138
|Index rebuild|0.822|0.626|
139139

140-
We only limited the test to 10K documents becuause MieliSearch was taking a long time to index, so we decided to stop at a reasonable sample size. The search numbers for litesearch were double checked, event against a 100K document set and they remained virtually the same. It is clear that litesearch is much faster than MieliSearch in both indexing and searching, this could be partially attributed to litesearch being a simpler text search engine, but still, the difference is huge! For rebuilding the index though, Litesearch is not that much faster than Mielisearch.
140+
We only limited the test to 10K documents becuause Meilisearch was taking a long time to index, so we decided to stop at a reasonable sample size. The search numbers for litesearch were double checked, event against a 100K document set and they remained virtually the same. It is clear that litesearch is much faster than Meilisearch in both indexing and searching, this could be partially attributed to litesearch being a simpler text search engine, but still, the difference is huge! For rebuilding the index though, Litesearch is not that much faster than Meilisearch.

bin/liteboard

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# frozen_string_literal: true
33

44
require 'optparse'
5+
require 'erb'
6+
require 'yaml'
7+
require 'rackup'
58
require_relative '../lib/litestack/liteboard/liteboard'
69
DEFAULTS = {
710
config_path: Litemetric::DEFAULT_OPTIONS[:config_path],
@@ -15,7 +18,7 @@ options = {
1518
Port: 9292,
1619
Host: 'localhost',
1720
environment: 'production',
18-
pid: 'tmp/pids/liteboard.pid',
21+
pid: './liteboard.pid',
1922
quiet: false
2023
}
2124

@@ -49,14 +52,14 @@ check_database(options[:path]) if options[:path]
4952
config = nil
5053
if options[:config_path]
5154
begin
52-
config = Yaml.load_file(options[:config_path])
55+
config = YAML.load(ERB.new(File.read(options[:config_path])).result)
5356
rescue
5457
puts "liteboard: missing or bad config file, please ensure the config file path is correct"
5558
puts "liteboard: exiting"
5659
exit
5760
end
5861
else # no config path! use the default
59-
config = Yaml.load_file(DEFAULTS[:config_path]) rescue nil
62+
config = YAML.load(ERB.new(File.read(DEFAULTS[:config_path])).result) rescue nil
6063
end
6164

6265
if config
@@ -78,4 +81,4 @@ options[:app] = Liteboard.app
7881
require_relative '../lib/litestack'
7982
puts "Starting Liteboard version #{Litestack::VERSION}"
8083

81-
Rack::Server.start(options)
84+
Rackup::Server.start(options)

lib/litestack/liteboard/liteboard.rb

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "hanami/router"
3+
require "rack"
44
require "tilt"
55
require "erubi"
66

@@ -11,37 +11,32 @@ class Liteboard
1111
@@resolutions = {"minute" => [300, 12], "hour" => [3600, 24], "day" => [3600 * 24, 7], "week" => [3600 * 24 * 7, 53], "year" => [3600 * 24 * 365, 100]}
1212
@@res_mapping = {"hour" => "minute", "day" => "hour", "week" => "day", "year" => "week"}
1313
@@templates = {}
14-
@@app = Hanami::Router.new do
15-
get "/", to: ->(env) do
14+
@@app = proc do |env|
15+
case path = env["PATH_INFO"]
16+
when "/"
1617
Liteboard.new(env).call(:index)
17-
end
18-
19-
get "/topics/Litejob", to: ->(env) do
18+
when "/topics/Litejob"
2019
Liteboard.new(env).call(:litejob)
21-
end
22-
23-
get "/topics/Litecache", to: ->(env) do
20+
when "/topics/Litecache"
2421
Liteboard.new(env).call(:litecache)
25-
end
26-
27-
get "/topics/Litedb", to: ->(env) do
22+
when "/topics/Litedb"
2823
Liteboard.new(env).call(:litedb)
29-
end
30-
31-
get "/topics/Litecable", to: ->(env) do
24+
when "/topics/Litecable"
3225
Liteboard.new(env).call(:litecable)
3326
end
27+
3428
end
3529

3630
def initialize(env)
3731
@env = env
38-
@params = @env["router.params"]
32+
@req = Rack::Request.new(@env)
33+
@params = @req.params
3934
@running = true
4035
@lm = Litemetric.instance
4136
end
4237

4338
def params(key)
44-
URI.decode_uri_component(@params[key].to_s)
39+
URI.decode_uri_component(@params[key.to_s].to_s)
4540
end
4641

4742
def call(method)
@@ -227,9 +222,9 @@ def litejob
227222
@topic = "Litejob"
228223
@events = @lm.events_summaries(@topic, @resolution, @order, @dir, @search, @step * @count)
229224
@events.each do |event|
230-
data_points = @lm.event_data_points(@step, @count, @resolution, @topic, event["name"])
225+
data_points = @lm.event_data_points(@step, @count, @resolution, @topic, event[:name])
231226
event["counts"] = data_points.collect { |r| [r["rtime"], r["rcount"] || 0] }
232-
event["values"] = data_points.collect { |r| [r["rtime"], r["rtotal"] || 0] }
227+
event["values"] = data_points.collect { |r| [r["rtime"], (r["rtotal"] || 0.0)] }
233228
end
234229
@snapshot = read_snapshot(@topic)
235230
@size = begin
@@ -391,6 +386,7 @@ def round(float)
391386
end
392387

393388
def format(float)
389+
float = float.round(3)
394390
string = float.to_s
395391
whole, decimal = string.split(".")
396392
whole = whole.chars.reverse.each_slice(3).map(&:join).join(",").reverse

lib/litestack/litejobqueue.rb

+10-8
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,16 @@ def stop
141141

142142
def exit_callback
143143
@running = false # stop all workers
144-
return unless @jobs_in_flight > 0
145-
puts "--- Litejob detected an exit, cleaning up"
146-
index = 0
147-
while @jobs_in_flight > 0 && index < 30 # 3 seconds grace period for jobs to finish
148-
puts "--- Waiting for #{@jobs_in_flight} jobs to finish"
149-
sleep 0.1
150-
index += 1
144+
if @jobs_in_flight > 0
145+
puts "--- Litejob detected an exit, cleaning up"
146+
index = 0
147+
while @jobs_in_flight > 0 && index < 30 # 3 seconds grace period for jobs to finish
148+
puts "--- Waiting for #{@jobs_in_flight} jobs to finish"
149+
sleep 0.1
150+
index += 1
151+
end
152+
puts " --- Exiting with #{@jobs_in_flight} jobs in flight"
151153
end
152-
puts " --- Exiting with #{@jobs_in_flight} jobs in flight"
153154
end
154155

155156
def setup
@@ -179,6 +180,7 @@ def schedule(spawn = false, &block)
179180

180181
# create a worker according to environment
181182
def create_worker
183+
return if defined?(Rails) && !defined?(Rails::Server)
182184
Litescheduler.spawn do
183185
worker_sleep_index = 0
184186
while @running

lib/litestack/litemetric.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@ def run_stmt_hash(stmt, *args)
148148
end
149149

150150
def exit_callback
151-
return unless @collector.count > 0
152-
warn "--- Litemetric detected an exit, flushing metrics"
153151
@running = false
154-
@collector.flush
152+
if @collector.count > 0
153+
warn "--- Litemetric detected an exit, flushing metrics"
154+
@collector.flush
155+
end
155156
end
156157

157158
def setup
@@ -203,8 +204,8 @@ def collect_metrics
203204
def create_snapshotter
204205
Litescheduler.spawn do
205206
while @running
206-
sleep @litemetric.options[:snapshot_interval]
207207
capture_snapshot
208+
sleep @litemetric.options[:snapshot_interval]
208209
end
209210
end
210211
end

lib/litestack/litesupport.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "yaml"
77
require "pathname"
88
require "fileutils"
9+
require "erb"
910

1011
require_relative "litescheduler"
1112

@@ -14,7 +15,7 @@ class Error < StandardError; end
1415

1516
# Detect the Rack or Rails environment.
1617
def self.detect_environment
17-
if defined? Rails
18+
if defined?(Rails) && Rails.respond_to?(:env)
1819
Rails.env
1920
elsif ENV["RACK_ENV"]
2021
ENV["RACK_ENV"]
@@ -182,7 +183,7 @@ def configure(options = {})
182183
end
183184
@options = defaults.merge(options)
184185
config = begin
185-
YAML.load_file(@options[:config_path])
186+
YAML.load(ERB.new(File.read(@options[:config_path])).result)
186187
rescue
187188
{}
188189
end # an empty hash won't hurt

lib/litestack/railtie.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
module Litestack
44
class Railtie < ::Rails::Railtie
55
initializer :disable_production_sqlite_warning do |app|
6-
# The whole point of this gem is to use sqlite3 in production.
7-
app.config.active_record.sqlite3_production_warning = false
6+
if config.active_record.key?(:sqlite3_production_warning)
7+
# The whole point of this gem is to use sqlite3 in production.
8+
app.config.active_record.sqlite3_production_warning = false
9+
end
810
end
911
end
1012
end

lib/litestack/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Litestack
4-
VERSION = "0.4.3"
4+
VERSION = "0.4.4"
55
end

litestack.gemspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ Gem::Specification.new do |spec|
3333
spec.add_dependency "sqlite3"
3434
spec.add_dependency "oj"
3535
spec.add_dependency "rack"
36-
spec.add_dependency "hanami-router", "~> 2.0"
36+
spec.add_dependency "rackup"
3737
spec.add_dependency "tilt"
3838
spec.add_dependency "erubi"
39-
39+
spec.add_development_dependency "activerecord"
4040
spec.add_development_dependency "rake"
4141
spec.add_development_dependency "activerecord"
4242
spec.add_development_dependency "railties"

test/test_configuration.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "minitest/autorun"
2+
require_relative "../lib/litestack/litesupport"
3+
4+
class SampleLiteComponent
5+
include Litesupport::Liteconnection
6+
7+
def initialize(options = {})
8+
init(options)
9+
end
10+
end
11+
12+
class TestConfiguration < Minitest::Test
13+
def test_yaml_with_no_erb
14+
config_file = Tempfile.new(['litecomponent', '.yml'])
15+
config_file.write('path: ":memory:"')
16+
17+
config_file.read
18+
19+
sample_component = SampleLiteComponent.new({config_path: config_file.path})
20+
21+
assert_equal sample_component.options[:path], ":memory:"
22+
23+
config_file.close!
24+
end
25+
26+
def test_yaml_with_erb
27+
config_file = Tempfile.new(['litecomponent', '.yml'])
28+
config_file.write('path: "<%= ":memory:" %>"')
29+
30+
config_file.read
31+
32+
sample_component = SampleLiteComponent.new({config_path: config_file.path})
33+
34+
assert_equal sample_component.options[:path], ":memory:"
35+
36+
config_file.close!
37+
end
38+
end

test/test_jobqueue.rb

+7-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def teardown
2828
def test_push
2929
@jobqueue.push(MyJob.name, [Time.now.to_i], 0, "test")
3030
assert @jobqueue.count != 0
31-
sleep 1.2
32-
assert @jobqueue.count == 0
31+
assert 0..2, @jobqueue.count == 0
32+
@jobqueue.clear
3333
end
3434

3535
def test_delete
@@ -47,8 +47,8 @@ def test_push_with_delay
4747
assert @jobqueue.count != 0
4848
sleep 0.1
4949
assert @jobqueue.count != 0
50-
sleep 2
51-
assert @jobqueue.count == 0
50+
assert 0..2, @jobqueue.count == 0
51+
@jobqueue.clear
5252
end
5353

5454
def test_retry
@@ -57,14 +57,13 @@ def test_retry
5757
assert @jobqueue.count != 0
5858
sleep 0.1
5959
assert @jobqueue.count != 0
60-
sleep 2.5
61-
assert @jobqueue.count("test") == 0
60+
assert 0..3, @jobqueue.count("test") == 0
6261
# should fail forever
6362
@jobqueue.push(MyJob.name, [Time.now.to_i + 3], 0, "test")
6463
assert @jobqueue.count != 0
6564
sleep 0.1
6665
assert @jobqueue.count != 0
67-
sleep 2.1
68-
assert @jobqueue.count("test") == 0
66+
assert 0..3, @jobqueue.count("test") == 0
67+
@jobqueue.clear
6968
end
7069
end

0 commit comments

Comments
 (0)