censive 0.19 → 0.20

Sign up to get free protection for your applications and to get access to all the features.
data/lib/flay.rb ADDED
@@ -0,0 +1,227 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # ============================================================================
4
+ # flay - A quick and lightweight benchmarking tool for Ruby
5
+ #
6
+ # Author: Steve Shreeve (steve.shreeve@gmail.com)
7
+ # Date: Feb 9, 2023
8
+ # ============================================================================
9
+ # GOALS:
10
+ # 1. Provide a simple way to benchmark various code
11
+ # 2. Easy to configure and start comparing results
12
+ #
13
+ # TODO:
14
+ # 1. Everything
15
+ # ============================================================================
16
+
17
+ class Hash
18
+ alias_method :default_lookup, :[]
19
+
20
+ def [](key, miss=nil)
21
+ key?(sym = key.to_sym) and return default_lookup(sym) || miss
22
+ ary = key.to_s.split(/(?:[.\/\[]|\][.\/]?)/)
23
+ val = ary.inject(self) do |obj, sub|
24
+ if obj == self then default_lookup(sub.to_sym)
25
+ elsif obj == nil then break
26
+ elsif sub =~ /\A-?\d*\z/ then obj[sub.to_i]
27
+ else obj[sub.to_sym]
28
+ end
29
+ end or miss
30
+ end
31
+
32
+ def method_missing(name, *args)
33
+ name !~ /=$/ ? self[name, *args] : self[$`.to_sym] = args.first
34
+ end
35
+ end
36
+
37
+ config = {
38
+ environments: [
39
+ {
40
+ name: "Environment 1",
41
+ before: <<~"|",
42
+ # Environment 1 before
43
+ |
44
+ after: <<~"|",
45
+ # Environment 1 after
46
+ |
47
+ },
48
+ {
49
+ name: "Environment 2",
50
+ before: <<~"|",
51
+ # Environment 1 before
52
+ |
53
+ after: <<~"|",
54
+ # Environment 1 after
55
+ |
56
+ },
57
+ ],
58
+
59
+ contexts: [
60
+ {
61
+ name: "Context 1",
62
+ before: <<~"|",
63
+ # context 1 before
64
+ |
65
+ script: <<~"|",
66
+ a = [*1..1e5]
67
+ a.sum
68
+ |
69
+ after: <<~"|",
70
+ # context 1 after
71
+ |
72
+ },
73
+ {
74
+ name: "Context 2",
75
+ before: <<~"|",
76
+ # context 2 before
77
+ |
78
+ after: <<~"|",
79
+ # context 2 after
80
+ |
81
+ },
82
+ ],
83
+
84
+ tasks: [
85
+ {
86
+ name: "Task 1",
87
+ runs: 35,
88
+ before: <<~"|",
89
+ # Task 1 before
90
+ |
91
+ after: <<~"|",
92
+ # Task 1 after
93
+ |
94
+ },
95
+ {
96
+ name: "Task 2",
97
+ secs: 30,
98
+ before: <<~"|",
99
+ # Task 2 before
100
+ |
101
+ after: <<~"|",
102
+ # Task 2 after
103
+ |
104
+ },
105
+ ],
106
+ }
107
+
108
+ # ==[ Helpers ]==
109
+
110
+ def wrapper(object, type=nil)
111
+ puts case type
112
+ when :environment then template_for_environment object
113
+ when :context then template_for_context object
114
+ when :task then template_for_task object
115
+ else section object
116
+ end
117
+ end
118
+
119
+ def wrap(list, type=nil, **opts)
120
+ list.each do |item|
121
+ wrapper(item, type)
122
+ yield item
123
+ end
124
+ end
125
+
126
+ def section(text, wide=78, left=0)
127
+ [
128
+ "# ".ljust(wide, "="),
129
+ "# #{text}",
130
+ "# ".ljust(wide, "="),
131
+ ].join("\n")
132
+ end
133
+
134
+ def hr(text, wide=78, left=0)
135
+ [ " " * left, "# ==[ ", text, " ]" ].join.ljust(wide, "=")
136
+ end
137
+
138
+ # ==[ Templates ]==
139
+
140
+ def template_for_environment(environment)
141
+ <<~"|"
142
+ #{ section "Environment: #{environment.name} " }
143
+
144
+ # ==[ Code before environment ]==
145
+
146
+ #{ environment.before }
147
+ |
148
+ end
149
+
150
+ def template_for_context(context)
151
+ <<~"|"
152
+ #{ section "Context: #{context.name} " }
153
+
154
+ # ==[ Code before context ]==
155
+
156
+ #{ context.before }
157
+ |
158
+ end
159
+
160
+ def template_for_task(task)
161
+ <<~"|"
162
+ #{ section "Task: #{task.name} " }
163
+
164
+ # ==[ Code before task ]==
165
+
166
+ #{ task.before }
167
+
168
+ # ==[ Calculate the duration of a loop of empty runs ]==
169
+
170
+ if #{ task.runs } == 1
171
+ __flay_before_empty = 0
172
+ __flay_after_empty = 0
173
+ else
174
+ __flay_before_empty = Process.clock_gettime(Process::CLOCK_MONOTONIC)
175
+ __flay_runs = 0
176
+ while __flay_runs < #{ task.runs } # this empty loop improves accuracy
177
+ __flay_runs += 1
178
+ end
179
+ __flay_after_empty = Process.clock_gettime(Process::CLOCK_MONOTONIC)
180
+ end
181
+
182
+ # ==[ Calculate the duration of a loop of script runs ]==
183
+
184
+ if #{ task.runs } == 1
185
+ __flay_before_script = 0
186
+ __flay_after_script = 0
187
+ else
188
+ __flay_before_script = Process.clock_gettime(Process::CLOCK_MONOTONIC)
189
+ __flay_runs = 0
190
+ while __flay_runs < #{ task.runs }
191
+
192
+ # ==[ Before script ]==
193
+
194
+ #{ task.script }
195
+
196
+ # ==[ After script ]==
197
+
198
+ __flay_runs += 1
199
+ end
200
+ __flay_after_script = Process.clock_gettime(Process::CLOCK_MONOTONIC)
201
+ end
202
+
203
+ # ==[ Code after task ]==
204
+
205
+ #{ task.after }
206
+
207
+ # ==[ Write out timestamps ]==
208
+
209
+ __flay_duration = (__flay_after_script - __flay_before_script) -
210
+ (__flay_after_empty - __flay_before_empty )
211
+
212
+ File.write("/dev/null", __flay_duration.inspect)
213
+ |
214
+ end
215
+
216
+ # ==[ Workflow ]==
217
+
218
+ environments = config.environments
219
+ contexts = config.contexts
220
+ tasks = config.tasks
221
+
222
+ wrap(environments, :environment) do |environment|
223
+ wrap(tasks, :task) do |task|
224
+ wrap(contexts, :context) do |context|
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "./censive"
4
+ require "digest/md5"
5
+
6
+ path = ARGV[0] || "KEN_ALL.CSV"
7
+ mode = path =~ /^ken/i ? "r:cp932" : "r"
8
+
9
+ data = File.open(path, mode).read
10
+ rows = Censive.parse(data)
11
+
12
+ puts "%s %s (%d size)" % [Digest::MD5.hexdigest(rows.join), path, File.stat(path).size], ""
data/lib/test-csv.rb ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "csv"
4
+ require "digest/md5"
5
+
6
+ path = ARGV[0] || "KEN_ALL.CSV"
7
+ mode = path =~ /^ken/i ? "r:cp932" : "r"
8
+
9
+ data = File.open(path, mode).read
10
+ rows = CSV.parse(data)
11
+
12
+ puts "%s %s (%d size)" % [Digest::MD5.hexdigest(rows.join), path, File.stat(path).size], ""
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: censive
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.19'
4
+ version: '0.20'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-05 00:00:00.000000000 Z
11
+ date: 2023-02-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A quick and lightweight CSV handling library for Ruby
14
14
  email: steve.shreeve@gmail.com
@@ -19,7 +19,22 @@ files:
19
19
  - LICENSE
20
20
  - README.md
21
21
  - censive.gemspec
22
+ - diagram/NFA to Regex.pdf
23
+ - diagram/censive@ce9d51d.png
24
+ - diagram/csv-ragel.dot
25
+ - diagram/csv.dot
26
+ - diagram/csv.png
27
+ - diagram/csv.rl
28
+ - diagram/csv.svg
29
+ - diagram/diagram.dot
30
+ - diagram/diagram.rl
22
31
  - lib/censive.rb
32
+ - lib/censive.rb-20230208182732
33
+ - lib/censive.rb-20230208195221
34
+ - lib/censive.rb-20230209050227
35
+ - lib/flay.rb
36
+ - lib/test-censive.rb
37
+ - lib/test-csv.rb
23
38
  - test/a-uses-tabs-and-single-quotes-and-no-trailing-newline.tsv
24
39
  homepage: https://github.com/shreeve/censive
25
40
  licenses: