Skip to content

Commit 30c11c5

Browse files
committed
Improvements to parse script and test-loaders profiler.
1 parent 4a49f22 commit 30c11c5

File tree

2 files changed

+133
-31
lines changed

2 files changed

+133
-31
lines changed

profiler/test-loaders.rb

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,66 @@
44
require "bundler/setup"
55
require 'json/ld'
66
require 'ruby-prof'
7+
require 'getoptlong'
8+
9+
parser_options = {
10+
base: nil,
11+
progress: false,
12+
profile: false,
13+
validate: false,
14+
}
15+
16+
options = {
17+
parser_options: parser_options,
18+
output: STDOUT,
19+
output_format: :nquads,
20+
input_format: :jsonld,
21+
}
22+
input = nil
23+
24+
OPT_ARGS = [
25+
["--compact", GetoptLong::NO_ARGUMENT, "Compact input, using context"],
26+
["--context", GetoptLong::REQUIRED_ARGUMENT, "Context used for compaction"],
27+
["--expand", GetoptLong::NO_ARGUMENT, "Expand input"],
28+
["--expanded", GetoptLong::NO_ARGUMENT, "Input is already expanded"],
29+
["--flatten", GetoptLong::NO_ARGUMENT, "Flatten input"],
30+
["--frame", GetoptLong::REQUIRED_ARGUMENT, "Frame input, option value is frame to use"],
31+
["--help", "-?", GetoptLong::NO_ARGUMENT, "This message"],
32+
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT, "Where to store output (default STDOUT)"],
33+
["--uri", GetoptLong::REQUIRED_ARGUMENT, "Run with argument value as base"],
34+
]
35+
36+
opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
37+
38+
def usage
39+
STDERR.puts %{Usage: #{$0} [options] file ...}
40+
width = OPT_ARGS.map do |o|
41+
l = o.first.length
42+
l += o[1].length + 2 if o[1].is_a?(String)
43+
l
44+
end.max
45+
OPT_ARGS.each do |o|
46+
s = " %-*s " % [width, (o[1].is_a?(String) ? "#{o[0,2].join(', ')}" : o[0])]
47+
s += o.last
48+
STDERR.puts s
49+
end
50+
exit(1)
51+
end
52+
53+
opts.each do |opt, arg|
54+
case opt
55+
when '--compact' then options[:compact] = true
56+
when '--context' then options[:context] = arg
57+
when '--expand' then options[:expand] = true
58+
when '--expanded' then options[:expanded] = true
59+
when '--flatten' then options[:flatten] = true
60+
when '--format' then options[:output_format] = arg.to_sym
61+
when '--frame' then options[:frame] = arg
62+
when "--help" then usage
63+
when '--output' then options[:output] = File.open(arg, "w")
64+
when '--uri' then parser_options[:base] = arg
65+
end
66+
end
767

868
doc_cache = {}
969
la = File.read(File.expand_path("../linked-art.json", __FILE__))
@@ -12,18 +72,37 @@
1272
documentUrl: "https://linked.art/ns/v1/linked-art.json",
1373
contentType: "application/ld+json")
1474

15-
loader = Proc.new do |url, **options, &block|
75+
options[:documentLoader] = Proc.new do |url, **options, &block|
1676
raise "Context not pre-cached" unless doc_cache.has_key?(url)
1777
block.call doc_cache[url]
1878
end
1979

2080
all_data = JSON.parse(File.read(File.expand_path("../all_data.json", __FILE__)))
2181

22-
result = RubyProf.profile do
23-
all_data.each do |indata|
24-
JSON::LD::API.expand(all_data, documentLoader: loader, ordered: false)
82+
output_dir = File.expand_path("../../doc/profiles/#{File.basename __FILE__, ".rb"}", __FILE__)
83+
FileUtils.mkdir_p(output_dir)
84+
profile = RubyProf::Profile.new
85+
profile.exclude_methods!(Array, :each, :map)
86+
profile.exclude_method!(Hash, :each)
87+
profile.exclude_method!(Kernel, :require)
88+
profile.exclude_method!(Object, :run)
89+
profile.exclude_common_methods!
90+
profile.start
91+
all_data.each do |indata|
92+
if options[:flatten]
93+
JSON::LD::API.flatten(indata, options[:context], **options)
94+
elsif options[:compact]
95+
JSON::LD::API.compact(indata, options[:context], **options)
96+
elsif options[:frame]
97+
JSON::LD::API.frame(indata, options[:frame], **options)
98+
else
99+
options[:expandContext] = options[:context]
100+
JSON::LD::API.expand(indata, **options)
25101
end
26102
end
103+
result = profile.stop
27104

28-
printer = RubyProf::GraphPrinter.new(result)
29-
printer.print(STDOUT)
105+
# Print a graph profile to text
106+
printer = RubyProf::MultiPrinter.new(result)
107+
printer.print(path: output_dir, profile: "profile")
108+
puts "output saved in #{output_dir}"

script/parse

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ require 'logger'
1111
require 'json/ld'
1212
require 'getoptlong'
1313
require 'open-uri'
14+
require 'ruby-prof'
1415

1516
def run(input, options)
1617
if options[:profile]
1718
output_dir = File.expand_path("../../doc/profiles/#{File.basename __FILE__, ".rb"}", __FILE__)
1819
FileUtils.mkdir_p(output_dir)
19-
result = RubyProf.profile do
20-
run(input, profile: false, **options)
21-
end
22-
result.eliminate_methods!([/Hash#each/, /JSON::LD::Utils#debug/, /Array#map/, /JSON::LD::Utils#depth/])
20+
profile = RubyProf::Profile.new
21+
profile.exclude_methods!(Array, :each, :map)
22+
profile.exclude_method!(Hash, :each)
23+
profile.exclude_method!(Kernel, :require)
24+
profile.exclude_method!(Object, :run)
25+
profile.exclude_common_methods!
26+
profile.start
27+
run(input, **options.merge(profile: false))
28+
result = profile.stop
29+
2330
# Print a graph profile to text
2431
printer = RubyProf::MultiPrinter.new(result)
2532
printer.print(path: output_dir, profile: "profile")
@@ -29,7 +36,6 @@ def run(input, options)
2936
reader_class = RDF::Reader.for(options[:input_format].to_sym)
3037
raise "Reader not found for #{options[:input_format]}" unless reader_class
3138

32-
prefixes = {}
3339
start = Time.new
3440
if options[:flatten]
3541
output = JSON::LD::API.flatten(input, options.delete(:context), **options)
@@ -95,25 +101,42 @@ options = {
95101
}
96102
input = nil
97103

98-
opts = GetoptLong.new(
99-
["--debug", GetoptLong::NO_ARGUMENT],
100-
["--automatic", GetoptLong::NO_ARGUMENT],
101-
["--compact", GetoptLong::NO_ARGUMENT],
102-
["--context", GetoptLong::REQUIRED_ARGUMENT],
103-
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT],
104-
["--expand", GetoptLong::NO_ARGUMENT],
105-
["--expanded", GetoptLong::NO_ARGUMENT],
106-
["--flatten", GetoptLong::NO_ARGUMENT],
107-
["--format", GetoptLong::REQUIRED_ARGUMENT],
108-
["--frame", GetoptLong::REQUIRED_ARGUMENT],
109-
["--input-format", GetoptLong::REQUIRED_ARGUMENT],
110-
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT],
111-
["--profile", GetoptLong::NO_ARGUMENT],
112-
["--parse-only", GetoptLong::NO_ARGUMENT],
113-
["--quiet", GetoptLong::NO_ARGUMENT],
114-
["--uri", GetoptLong::REQUIRED_ARGUMENT],
115-
["--validate", GetoptLong::NO_ARGUMENT]
116-
)
104+
OPT_ARGS = [
105+
["--debug", GetoptLong::NO_ARGUMENT, "Debug output"],
106+
["--compact", GetoptLong::NO_ARGUMENT, "Compact input, using context"],
107+
["--context", GetoptLong::REQUIRED_ARGUMENT, "Context used for compaction"],
108+
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT, "Use option argument as the patch input"],
109+
["--expand", GetoptLong::NO_ARGUMENT, "Expand input"],
110+
["--expanded", GetoptLong::NO_ARGUMENT, "Input is already expanded"],
111+
["--flatten", GetoptLong::NO_ARGUMENT, "Flatten input"],
112+
["--format", GetoptLong::REQUIRED_ARGUMENT, "Output format, for RDF output"],
113+
["--frame", GetoptLong::REQUIRED_ARGUMENT, "Frame input, option value is frame to use"],
114+
["--help", "-?", GetoptLong::NO_ARGUMENT, "This message"],
115+
["--input-format", GetoptLong::REQUIRED_ARGUMENT, "Format of input, if not JSON-LD"],
116+
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT, "Where to store output (default STDOUT)"],
117+
["--profile", GetoptLong::NO_ARGUMENT, "Run profiler with output to doc/profiles/"],
118+
["--quiet", GetoptLong::NO_ARGUMENT, "Reduce output"],
119+
["--uri", GetoptLong::REQUIRED_ARGUMENT, "Run with argument value as base"],
120+
["--validate", GetoptLong::NO_ARGUMENT, "Validate input"],
121+
]
122+
123+
opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
124+
125+
def usage
126+
STDERR.puts %{Usage: #{$0} [options] file ...}
127+
width = OPT_ARGS.map do |o|
128+
l = o.first.length
129+
l += o[1].length + 2 if o[1].is_a?(String)
130+
l
131+
end.max
132+
OPT_ARGS.each do |o|
133+
s = " %-*s " % [width, (o[1].is_a?(String) ? "#{o[0,2].join(', ')}" : o[0])]
134+
s += o.last
135+
STDERR.puts s
136+
end
137+
exit(1)
138+
end
139+
117140
opts.each do |opt, arg|
118141
case opt
119142
when '--debug' then logger.level = Logger::DEBUG
@@ -125,9 +148,9 @@ opts.each do |opt, arg|
125148
when '--flatten' then options[:flatten] = true
126149
when '--format' then options[:output_format] = arg.to_sym
127150
when '--frame' then options[:frame] = arg
151+
when "--help" then usage
128152
when '--input-format' then options[:input_format] = arg.to_sym
129153
when '--output' then options[:output] = File.open(arg, "w")
130-
when '--parse-only' then options[:parse_only] = true
131154
when '--profile' then options[:profile] = true
132155
when '--quiet'
133156
options[:quiet] = true

0 commit comments

Comments
 (0)