Skip to content

Instantly share code, notes, and snippets.

@FrancisGX
Created January 31, 2014 02:22
Show Gist options
  • Save FrancisGX/8725582 to your computer and use it in GitHub Desktop.
Save FrancisGX/8725582 to your computer and use it in GitHub Desktop.
require 'active_record'
require 'postgres-copy'
require 'pg_data_encoder'
require 'benchmark'
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "localhost",
:database => "pg_test"
)
ActiveRecord::Base.connection.execute(
"DROP TABLE IF EXISTS pg_test;
CREATE TABLE pg_test (id serial PRIMARY KEY, data VARCHAR);"
)
class PGTest < ActiveRecord::Base
acts_as_copy_target
end
pg_encoder = PgDataEncoder::EncodeForCopy.new
puts "add data to encoder"
puts Benchmark.measure {
1_000_000.times { |number|
pg_encoder.add ["#{number}"]
}
}
puts "copy from encoder to table"
puts Benchmark.measure {
PGTest.copy_from(pg_encoder.get_io, :format => :binary, :columns => [:data])
}
pg_encoder.remove
puts "copy from table to tempfile"
puts Benchmark.measure {
PGTest.copy_to '/tmp/pg_test.csv'
}
puts "copy from tempfile to table"
puts Benchmark.measure {
PGTest.copy_from '/tmp/pg_test.csv'
}
# add data to encoder
# 20.850000 0.060000 20.910000 ( 20.917008)
# copy from encoder to table
# 0.040000 0.020000 0.060000 ( 9.828958)
# copy from table to tempfile
# 0.000000 0.000000 0.000000 ( 3.170732)
# copy from tempfile to table
# 38.780000 2.330000 41.110000 ( 41.116339)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment