Last active
June 30, 2017 08:59
-
-
Save will/df23fddbbedfd90cb960d15a78d3cd0f to your computer and use it in GitHub Desktop.
using binary result type for timestamptz and ruby pg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "pg" | |
require "time" | |
PG_EPOCH = Time.gm(2000,1,1).to_i | |
conn = PG.connect | |
tm = PG::TypeMapByOid.new | |
tm.add_coder PG::BinaryDecoder::Integer.new(oid: 1184) | |
conn.type_map_for_results = tm | |
def parse_text(r) | |
Time.parse(r.first["now"]) | |
end | |
def parse_binary(r) | |
sec, msec = r.first["now"].divmod 1000000 | |
Time.at(sec + PG_EPOCH, msec) | |
end | |
conn.exec_params("select now()") {|r| a = parse_text(r); p [a,a.to_f] } | |
conn.exec_params("select now()", [], 1) {|r| a = parse_binary(r); p [a,a.to_f]} | |
exit unless ARGV[0] | |
require "benchmark/ips" | |
Benchmark.ips do |x| | |
x.report("text") { conn.exec_params("select now()") {|r| parse_text(r) } } | |
x.report("binary") { conn.exec_params("select now()", [], 1) {|r| parse_binary(r) } } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[2017-06-15 16:32:39 -0700, 1497569559.781141] | |
[2017-06-15 16:32:39 -0700, 1497569559.7826529] | |
Warming up -------------------------------------- | |
text 1.232k i/100ms | |
binary 2.184k i/100ms | |
Calculating ------------------------------------- | |
text 12.993k (± 8.4%) i/s - 65.296k in 5.064765s | |
binary 22.205k (± 3.4%) i/s - 111.384k in 5.022365s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment