-
Notifications
You must be signed in to change notification settings - Fork 1
/
write-log.rb
executable file
·70 lines (61 loc) · 1.71 KB
/
write-log.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env ruby
require 'arrow'
require 'oj'
require 'msgpack'
require 'faker'
require 'benchmark'
record_num = ENV.fetch('RECORDS', 1).to_i
bench_times = ENV.fetch('BENCH', 100).to_i
fields = [
Arrow::Field.new('ipaddress', :string),
Arrow::Field.new('timestamp', :uint32),
Arrow::Field.new('execution', :float),
Arrow::Field.new('method', :string),
Arrow::Field.new('path', :string),
Arrow::Field.new('response', :uint16),
Arrow::Field.new('body', :string)
]
schema = Arrow::Schema.new(fields)
responses = [200] * 30 + [401] + [404] * 3 + [500] * 2 + [301] * 5
now = Time.now.to_i
records = record_num.times.map do
[Faker::Internet.ip_v4_address,
(now * rand).to_i,
rand,
%w(GET POST SHOW DELETE).sample,
'/',
responses.sample,
'{"hello": "world"}']
end
columns = [Arrow::StringArray, Arrow::UInt32Array,
Arrow::FloatArray, Arrow::StringArray,
Arrow::StringArray, Arrow::UInt16Array,
Arrow::StringArray].each_with_index.map do |klazz, i|
klazz.new(records.map {|x| x[i]})
end
Benchmark.bm(10) do |bm|
bm.report(:redarrow) do
bench_times.times do
Arrow::FileOutputStream.open("/tmp/log.arrow", false) do |output|
Arrow::RecordBatchFileWriter.open(output, schema) do |writer|
record_batch = Arrow::RecordBatch.new(schema, records.size, columns)
writer.write_record_batch(record_batch)
end
end
end
end
bm.report(:json) do
bench_times.times do
File.open('/tmp/log.json', 'w') do |f|
f.write Oj.dump(records)
end
end
end
bm.report(:msgpack) do
bench_times.times do
File.open('/tmp/log.msgpack', 'w') do |f|
f.write MessagePack.pack(records)
end
end
end
end