stud 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/stud/interval.rb +24 -13
- data/lib/stud/task.rb +8 -0
- data/lib/stud/temporary.rb +1 -1
- data/lib/stud/trap.rb +6 -0
- metadata +19 -62
- data/lib/stud/benchmark.rb +0 -132
- data/lib/stud/benchmark/rusage.rb +0 -54
- data/lib/stud/time.rb +0 -37
- data/lib/stud/time/format.rb +0 -1323
- data/lib/stud/time/format.tt +0 -236
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7d61ca01aaea7a1e6b562d7c03b50a2d0cf1379d
|
4
|
+
data.tar.gz: 55aeb2cdaa20574a2db50982c225129fb81b72ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e2ec3dd44726a5c12f42199c2887fc9e644424c58733355447d095b684488d0974cca7e6d55e115e6c8b120324873acd548c559e34171189284cb4bb094e78ee
|
7
|
+
data.tar.gz: 481b4e4b1e75dd949c67fd2dbb3b2e883fae13d292a351a1da26f0c572c2d10189ee6921119c77a3dae0108b6fd7eaae74f266ed8ef1497a5754ca5e89251fe4
|
data/lib/stud/interval.rb
CHANGED
@@ -1,27 +1,38 @@
|
|
1
|
+
require "stud/task"
|
1
2
|
module Stud
|
2
3
|
# This implementation tries to keep clock more accurately.
|
3
4
|
# Prior implementations still permitted skew, where as this one
|
4
5
|
# will attempt to correct for skew.
|
5
6
|
#
|
6
|
-
# The execution patterns of this method should be that
|
7
|
+
# The execution patterns of this method should be that
|
7
8
|
# the start time of 'block.call' should always be at time T*interval
|
8
|
-
def self.interval(time, &block)
|
9
|
+
def self.interval(time, opts = {}, &block)
|
9
10
|
start = Time.now
|
10
11
|
while true
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
sleep(time - duration)
|
16
|
-
start += time
|
12
|
+
break if Task.interrupted?
|
13
|
+
if opts[:sleep_then_run]
|
14
|
+
start = sleep_for_interval(time, start)
|
15
|
+
block.call
|
17
16
|
else
|
18
|
-
|
19
|
-
start =
|
17
|
+
block.call
|
18
|
+
start = sleep_for_interval(time, start)
|
20
19
|
end
|
21
20
|
end # loop forever
|
22
21
|
end # def interval
|
23
|
-
|
24
|
-
def
|
25
|
-
|
22
|
+
|
23
|
+
def self.sleep_for_interval(time, start)
|
24
|
+
duration = Time.now - start
|
25
|
+
# Sleep only if the duration was less than the time interval
|
26
|
+
if duration < time
|
27
|
+
sleep(time - duration)
|
28
|
+
start += time
|
29
|
+
else
|
30
|
+
# Duration exceeded interval time, reset the clock and do not sleep.
|
31
|
+
start = Time.now
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def interval(time, opts = {}, &block)
|
36
|
+
return Stud.interval(time, opts, &block)
|
26
37
|
end # def interval
|
27
38
|
end # module Stud
|
data/lib/stud/task.rb
CHANGED
data/lib/stud/temporary.rb
CHANGED
@@ -11,7 +11,7 @@ module Stud
|
|
11
11
|
def pathname(prefix=DEFAULT_PREFIX)
|
12
12
|
|
13
13
|
root = ENV["TMP"] || ENV["TMPDIR"] || ENV["TEMP"] || "/tmp"
|
14
|
-
return File.join(root, "#{prefix}-#{SecureRandom.
|
14
|
+
return File.join(root, "#{prefix}-#{SecureRandom.hex(30)}")
|
15
15
|
end
|
16
16
|
|
17
17
|
# Return a File handle to a randomly-generated path.
|
data/lib/stud/trap.rb
CHANGED
@@ -52,6 +52,12 @@ module Stud
|
|
52
52
|
# 'id' is the value returned by a previous Stud.trap() call
|
53
53
|
def self.untrap(signal, id)
|
54
54
|
@traps[signal].delete_if { |block| block.object_id == id }
|
55
|
+
|
56
|
+
# Restore the default handler if there are no custom traps anymore.
|
57
|
+
if @traps[signal].empty?
|
58
|
+
@traps.delete(signal)
|
59
|
+
Signal::trap(signal, "DEFAULT")
|
60
|
+
end
|
55
61
|
end # def self.untrap
|
56
62
|
end # module Stud
|
57
63
|
|
metadata
CHANGED
@@ -1,78 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.18
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Jordan Sissel
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-11-10 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: metriks
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: ffi
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
38
|
-
type: :runtime
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
46
13
|
- !ruby/object:Gem::Dependency
|
47
14
|
name: rspec
|
48
15
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
16
|
requirements:
|
51
|
-
- -
|
17
|
+
- - ">="
|
52
18
|
- !ruby/object:Gem::Version
|
53
19
|
version: '0'
|
54
20
|
type: :development
|
55
21
|
prerelease: false
|
56
22
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
23
|
requirements:
|
59
|
-
- -
|
24
|
+
- - ">="
|
60
25
|
- !ruby/object:Gem::Version
|
61
26
|
version: '0'
|
62
27
|
- !ruby/object:Gem::Dependency
|
63
28
|
name: insist
|
64
29
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
30
|
requirements:
|
67
|
-
- -
|
31
|
+
- - ">="
|
68
32
|
- !ruby/object:Gem::Version
|
69
33
|
version: '0'
|
70
34
|
type: :development
|
71
35
|
prerelease: false
|
72
36
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
37
|
requirements:
|
75
|
-
- -
|
38
|
+
- - ">="
|
76
39
|
- !ruby/object:Gem::Version
|
77
40
|
version: '0'
|
78
41
|
description: small reusable bits of code I'm tired of writing over and over. A library
|
@@ -82,46 +45,40 @@ executables: []
|
|
82
45
|
extensions: []
|
83
46
|
extra_rdoc_files: []
|
84
47
|
files:
|
48
|
+
- CHANGELIST
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- lib/stud/buffer.rb
|
52
|
+
- lib/stud/interval.rb
|
53
|
+
- lib/stud/pool.rb
|
85
54
|
- lib/stud/secret.rb
|
86
|
-
- lib/stud/with.rb
|
87
|
-
- lib/stud/benchmark/rusage.rb
|
88
55
|
- lib/stud/task.rb
|
89
|
-
- lib/stud/pool.rb
|
90
|
-
- lib/stud/buffer.rb
|
91
|
-
- lib/stud/try.rb
|
92
|
-
- lib/stud/time.rb
|
93
56
|
- lib/stud/temporary.rb
|
94
|
-
- lib/stud/benchmark.rb
|
95
57
|
- lib/stud/trap.rb
|
96
|
-
- lib/stud/
|
97
|
-
- lib/stud/
|
98
|
-
- lib/stud/time/format.tt
|
99
|
-
- LICENSE
|
100
|
-
- CHANGELIST
|
101
|
-
- README.md
|
58
|
+
- lib/stud/try.rb
|
59
|
+
- lib/stud/with.rb
|
102
60
|
homepage: https://github.com/jordansissel/ruby-stud
|
103
61
|
licenses: []
|
62
|
+
metadata: {}
|
104
63
|
post_install_message:
|
105
64
|
rdoc_options: []
|
106
65
|
require_paths:
|
107
66
|
- lib
|
108
67
|
- lib
|
109
68
|
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
-
none: false
|
111
69
|
requirements:
|
112
|
-
- -
|
70
|
+
- - ">="
|
113
71
|
- !ruby/object:Gem::Version
|
114
72
|
version: '0'
|
115
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
-
none: false
|
117
74
|
requirements:
|
118
|
-
- -
|
75
|
+
- - ">="
|
119
76
|
- !ruby/object:Gem::Version
|
120
77
|
version: '0'
|
121
78
|
requirements: []
|
122
79
|
rubyforge_project:
|
123
|
-
rubygems_version:
|
80
|
+
rubygems_version: 2.2.2
|
124
81
|
signing_key:
|
125
|
-
specification_version:
|
82
|
+
specification_version: 4
|
126
83
|
summary: stud - common code techniques
|
127
84
|
test_files: []
|
data/lib/stud/benchmark.rb
DELETED
@@ -1,132 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
# Benchmark Use Cases
|
3
|
-
# * Compare performance of different implementations.
|
4
|
-
# * run each implementation N times, compare runtimes (histogram, etc)
|
5
|
-
|
6
|
-
require "metriks"
|
7
|
-
require "stud/benchmark/rusage"
|
8
|
-
|
9
|
-
module Stud
|
10
|
-
module Benchmark
|
11
|
-
def self.run(iterations=1, &block)
|
12
|
-
timer = Metriks::Timer.new
|
13
|
-
start = Time.now
|
14
|
-
iterations.times { timer.time(&block) }
|
15
|
-
duration = Time.now - start
|
16
|
-
return Results.new(timer, duration)
|
17
|
-
end # def run
|
18
|
-
|
19
|
-
def self.runtimed(seconds=10, &block)
|
20
|
-
timer = Metriks::Timer.new
|
21
|
-
expiration = Time.now + seconds
|
22
|
-
|
23
|
-
start = Time.now
|
24
|
-
timer.time(&block) while Time.now < expiration
|
25
|
-
duration = Time.now - start
|
26
|
-
return Results.new(timer, duration)
|
27
|
-
end # def runtimed
|
28
|
-
|
29
|
-
def self.cputimed(seconds=10, &block)
|
30
|
-
timer = Metriks::Timer.new
|
31
|
-
expiration = Time.now + seconds
|
32
|
-
start_usage = Stud::Benchmark::RUsage.get
|
33
|
-
while Time.now < expiration
|
34
|
-
start = Stud::Benchmark::RUsage.get
|
35
|
-
block.call
|
36
|
-
finish = Stud::Benchmark::RUsage.get
|
37
|
-
cputime = (finish.user + finish.system) - (start.user + start.system)
|
38
|
-
timer.update(cputime)
|
39
|
-
end # while not expired
|
40
|
-
finish_usage = Stud::Benchmark::RUsage.get
|
41
|
-
duration = (finish_usage.user + finish_usage.system) \
|
42
|
-
- (start_usage.user + start_usage.system)
|
43
|
-
return Results.new(timer, duration)
|
44
|
-
end # self.cpu
|
45
|
-
|
46
|
-
class Results
|
47
|
-
include Enumerable
|
48
|
-
# Stolen from https://github.com/holman/spark/blob/master/spark
|
49
|
-
# TICKS = %w{▁ ▂ ▃ ▄ ▅ ▆ ▇ █}
|
50
|
-
|
51
|
-
TICKS = ["\x1b[38;5;#{232 + 8}m_\x1b[0m"] + %w{▁ ▂ ▃ ▄ ▅ ▆ ▇ █}
|
52
|
-
|
53
|
-
#.collect do |tick|
|
54
|
-
# 256 color support, use grayscale
|
55
|
-
#1.times.collect do |shade|
|
56
|
-
# '38' is foreground
|
57
|
-
# '48' is background
|
58
|
-
# Grey colors start at 232, but let's use the brighter half.
|
59
|
-
# escape [ 38 ; 5 ; <color>
|
60
|
-
#"\x1b[38;5;#{232 + 12 + 2 * shade}m#{tick}\x1b[0m"
|
61
|
-
#end
|
62
|
-
#tick
|
63
|
-
#end.flatten
|
64
|
-
|
65
|
-
def initialize(data, duration)
|
66
|
-
@data = data
|
67
|
-
@duration = duration
|
68
|
-
end # def initialize
|
69
|
-
|
70
|
-
def environment
|
71
|
-
# Older rubies don't have the RUBY_ENGINE defiend
|
72
|
-
engine = (RUBY_ENGINE rescue "ruby")
|
73
|
-
# Include jruby version in the engine
|
74
|
-
engine += (JRUBY_VERSION rescue "")
|
75
|
-
version = RUBY_VERSION
|
76
|
-
|
77
|
-
return "#{engine} #{version}"
|
78
|
-
end # def environment
|
79
|
-
|
80
|
-
def each(&block)
|
81
|
-
@data.snapshot.each(&block)
|
82
|
-
end # def each
|
83
|
-
|
84
|
-
def min
|
85
|
-
return @data.min
|
86
|
-
end
|
87
|
-
|
88
|
-
def max
|
89
|
-
return @data.max
|
90
|
-
end
|
91
|
-
|
92
|
-
def rate
|
93
|
-
return @data.count / @duration
|
94
|
-
end
|
95
|
-
|
96
|
-
def mean
|
97
|
-
return @data.mean
|
98
|
-
end # def mean
|
99
|
-
|
100
|
-
def stddev
|
101
|
-
# work around (Timer#stddev reports the variance)
|
102
|
-
# https://github.com/eric/metriks/pull/29
|
103
|
-
return @data.stddev ** 0.5
|
104
|
-
end # def stddev
|
105
|
-
|
106
|
-
def sum
|
107
|
-
return @data.instance_eval { @histogram.sum }
|
108
|
-
end # def sum
|
109
|
-
|
110
|
-
def pretty_print
|
111
|
-
puts self
|
112
|
-
end # def pretty_print
|
113
|
-
|
114
|
-
def to_s(scale=min .. max, ticks=10)
|
115
|
-
snapshot = @data.snapshot
|
116
|
-
values = snapshot.instance_eval { @values }
|
117
|
-
scale_distance = scale.end - scale.begin
|
118
|
-
tick = scale_distance / ticks
|
119
|
-
dist = ticks.to_i.times.collect do |i|
|
120
|
-
range = (scale.begin + tick * i) ... (scale.begin + tick * (i+1))
|
121
|
-
hits = values.select { |v| range.include?(v) }.count
|
122
|
-
percent = hits / values.size.to_f
|
123
|
-
next TICKS[(TICKS.count * percent).ceil] || TICKS.last
|
124
|
-
end
|
125
|
-
|
126
|
-
return sprintf("%20s %s (%.4f ... %.4f, mean: %0.4f, stddev: %0.4f)",
|
127
|
-
environment, dist.join(""), scale.begin, scale.end,
|
128
|
-
mean, stddev)
|
129
|
-
end # def to_s
|
130
|
-
end # class Stud::Benchmark::Result
|
131
|
-
end # module Benchmark
|
132
|
-
end # module Stud
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require "ffi"
|
2
|
-
|
3
|
-
module Stud
|
4
|
-
module Benchmark
|
5
|
-
module LibC
|
6
|
-
extend FFI::Library
|
7
|
-
ffi_lib "libc.so.6"
|
8
|
-
|
9
|
-
attach_function :getrusage, [:int, :pointer], :int
|
10
|
-
end
|
11
|
-
|
12
|
-
class TimeVal < FFI::Struct
|
13
|
-
layout :tv_sec, :long,
|
14
|
-
:tv_usec, :int32
|
15
|
-
|
16
|
-
def to_f
|
17
|
-
return self[:tv_sec] + (self[:tv_usec] / 1_000_000.0)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class RUsage < FFI::Struct
|
22
|
-
layout :utime, TimeVal,
|
23
|
-
:stime, TimeVal,
|
24
|
-
:maxrss, :long,
|
25
|
-
:ixrss, :long,
|
26
|
-
:idrss, :long,
|
27
|
-
:isrss, :long,
|
28
|
-
:minflt, :long,
|
29
|
-
:majflt, :long,
|
30
|
-
:nswap, :long,
|
31
|
-
:inblock, :long,
|
32
|
-
:oublock, :long,
|
33
|
-
:msgsnd, :long,
|
34
|
-
:msgrcv, :long,
|
35
|
-
:nsignals, :long,
|
36
|
-
:nvcsw, :long,
|
37
|
-
:nivcsw, :long
|
38
|
-
|
39
|
-
def self.get
|
40
|
-
usage = RUsage.new
|
41
|
-
LibC.getrusage(0, usage)
|
42
|
-
return usage
|
43
|
-
end
|
44
|
-
|
45
|
-
def user
|
46
|
-
return self[:utime].to_f
|
47
|
-
end
|
48
|
-
|
49
|
-
def system
|
50
|
-
return self[:stime].to_f
|
51
|
-
end
|
52
|
-
end # class RUsage
|
53
|
-
end # module Benchmark
|
54
|
-
end # module Stud
|
data/lib/stud/time.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
module Stud
|
2
|
-
module Time
|
3
|
-
# The following table is copied from joda-time's docs
|
4
|
-
# http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
|
5
|
-
# Symbol Meaning Presentation Examples
|
6
|
-
# ------ ------- ------------ -------
|
7
|
-
# G era text AD
|
8
|
-
# C century of era (>=0) number 20
|
9
|
-
# Y year of era (>=0) year 1996
|
10
|
-
|
11
|
-
# x weekyear year 1996
|
12
|
-
# w week of weekyear number 27
|
13
|
-
# e day of week number 2
|
14
|
-
# E day of week text Tuesday; Tue
|
15
|
-
|
16
|
-
# y year year 1996
|
17
|
-
# D day of year number 189
|
18
|
-
# M month of year month July; Jul; 07
|
19
|
-
# d day of month number 10
|
20
|
-
|
21
|
-
# a halfday of day text PM
|
22
|
-
# K hour of halfday (0~11) number 0
|
23
|
-
# h clockhour of halfday (1~12) number 12
|
24
|
-
|
25
|
-
# H hour of day (0~23) number 0
|
26
|
-
# k clockhour of day (1~24) number 24
|
27
|
-
# m minute of hour number 30
|
28
|
-
# s second of minute number 55
|
29
|
-
# S fraction of second number 978
|
30
|
-
|
31
|
-
# z time zone text Pacific Standard Time; PST
|
32
|
-
# Z time zone offset/id zone -0800; -08:00; America/Los_Angeles
|
33
|
-
|
34
|
-
# ' escape for text delimiter
|
35
|
-
# '' single quote literal '
|
36
|
-
end
|
37
|
-
end
|