Add benchmark for copy-from

This commit is contained in:
jeromew 2019-03-21 13:22:13 +00:00
parent cffae659b8
commit ff39922e86
5 changed files with 135 additions and 0 deletions

4
.npmignore Normal file
View File

@ -0,0 +1,4 @@
.gitignore
.travis.yml
bench/
test/

View File

@ -70,6 +70,26 @@ Before you set out on this magical piping journey, you _really_ should read this
Take note of the following warning in the PostgreSQL documentation: Take note of the following warning in the PostgreSQL documentation:
> COPY stops operation at the first error. This should not lead to problems in the event of a COPY TO, but the target table will already have received earlier rows in a COPY FROM. These rows will not be visible or accessible, but they still occupy disk space. This might amount to a considerable amount of wasted disk space if the failure happened well into a large copy operation. You might wish to invoke VACUUM to recover the wasted space. > COPY stops operation at the first error. This should not lead to problems in the event of a COPY TO, but the target table will already have received earlier rows in a COPY FROM. These rows will not be visible or accessible, but they still occupy disk space. This might amount to a considerable amount of wasted disk space if the failure happened well into a large copy operation. You might wish to invoke VACUUM to recover the wasted space.
## benchmarks
The COPY command is commonly used to move huge sets of data. This can put some pressure on the node.js loop, the amount of CPU or the amount of memory used.
There is a bench/ directory in the repository where benchmark scripts are stored. If you have performance issues with `pg-copy-stream` do not hesitate to write a new benchmark that highlights your issue. Please avoid to commit huge files (PR won't be accepted) and find other ways to generate huge datasets.
If you have a local instance of postgres on your machine, you can start a benchmark for example with
```sh
$ cd bench
$ PGPORT=5432 PGDATABASE=postgres node copy-from.js
```
## tests
In order to launch the test suite, you need to have a local instance of postgres running on your machine.
```sh
$ PGPORT=5432 PGDATABASE=postgres make test
```
## contributing ## contributing
Instead of adding a bunch more code to the already bloated [node-postgres](https://github.com/brianc/node-postgres) I am trying to make the internals extensible and work on adding edge-case features as 3rd party modules. Instead of adding a bunch more code to the already bloated [node-postgres](https://github.com/brianc/node-postgres) I am trying to make the internals extensible and work on adding edge-case features as 3rd party modules.
@ -88,6 +108,7 @@ Since this isn't a module with tons of installs and dependent modules I hope we
### version 2.x - published YYYY-MM-DD ### version 2.x - published YYYY-MM-DD
* Small refactor in copy-from passing from 3 push to 2 push in every chunk transform loop * Small refactor in copy-from passing from 3 push to 2 push in every chunk transform loop
* Add bench/ directory for benchmarks
### version 2.1.0 - published 2019-03-19 ### version 2.1.0 - published 2019-03-19

86
bench/copy-from.js Normal file
View File

@ -0,0 +1,86 @@
var Benchmark = require('benchmark');
var cp = require('duplex-child-process');
var pg = require('pg')
var copy = require('../').from
var client = function() {
var client = new pg.Client()
client.connect()
return client
}
var psql = '/opt/postgresql-9.6.1/bin/psql'
var limit = 999999;
var inStream = function() {
return cp.spawn('seq', ['0', ''+limit]);
}
var suite = new Benchmark.Suite;
suite
.add({
name: 'unix pipe into psql COPY',
defer: true,
fn: function(d) {
var c = client();
c.query('DROP TABLE IF EXISTS plugnumber', function() {
c.query('CREATE TABLE plugnumber (num int)', function() {
c.end();
var from = cp.spawn('sh', ['-c', 'seq 0 '+limit+' | '+psql+' postgres -c \'COPY plugnumber FROM STDIN\''])
from.on('close', function() {
d.resolve();
})
})
})
}
})
.add({
name: 'pipe into psql COPY',
defer: true,
fn: function(d) {
var c = client();
c.query('DROP TABLE IF EXISTS plugnumber', function() {
c.query('CREATE TABLE plugnumber (num int)', function() {
c.end();
var seq = inStream();
var from = cp.spawn(psql, ['postgres', '-c', 'COPY plugnumber FROM STDIN'])
seq.pipe(from);
from.on('close', function() {
d.resolve();
})
})
})
}
})
.add({
name: 'pipe into pg-copy-stream COPY',
defer: true,
fn: function(d) {
var c = client();
c.query('DROP TABLE IF EXISTS plugnumber', function() {
c.query('CREATE TABLE plugnumber (num int)', function() {
var seq = inStream()
var from = c.query(copy('COPY plugnumber FROM STDIN'))
seq.pipe(from);
from.on('end', function() {
c.end();
d.resolve();
})
})
})
}
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
});
var c = client()
c.query('DROP TABLE IF EXISTS plugnumber', function() {
c.end();
suite.run();
})

22
package-lock.json generated
View File

@ -16,6 +16,16 @@
"integrity": "sha1-Ak8Pcq+iW3X5wO5zzU9V7Bvtl4Q=", "integrity": "sha1-Ak8Pcq+iW3X5wO5zzU9V7Bvtl4Q=",
"dev": true "dev": true
}, },
"benchmark": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/benchmark/-/benchmark-2.1.4.tgz",
"integrity": "sha1-CfPeMckWQl1JjMLuVloOvzwqVik=",
"dev": true,
"requires": {
"lodash": "^4.17.4",
"platform": "^1.3.3"
}
},
"bops": { "bops": {
"version": "0.0.6", "version": "0.0.6",
"resolved": "https://registry.npmjs.org/bops/-/bops-0.0.6.tgz", "resolved": "https://registry.npmjs.org/bops/-/bops-0.0.6.tgz",
@ -41,6 +51,12 @@
"bops": "0.0.6" "bops": "0.0.6"
} }
}, },
"duplex-child-process": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/duplex-child-process/-/duplex-child-process-1.0.0.tgz",
"integrity": "sha1-SpSXQob7x4QNCFPSs/5ZCp20YUc=",
"dev": true
},
"gonna": { "gonna": {
"version": "0.0.0", "version": "0.0.0",
"resolved": "https://registry.npmjs.org/gonna/-/gonna-0.0.0.tgz", "resolved": "https://registry.npmjs.org/gonna/-/gonna-0.0.0.tgz",
@ -129,6 +145,12 @@
"split": "^1.0.0" "split": "^1.0.0"
} }
}, },
"platform": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/platform/-/platform-1.3.5.tgz",
"integrity": "sha512-TuvHS8AOIZNAlE77WUDiR4rySV/VMptyMfcfeoMgs4P8apaZM3JrnbzBiixKUv+XR6i+BXrQh8WAnjaSPFO65Q==",
"dev": true
},
"postgres-array": { "postgres-array": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",

View File

@ -24,7 +24,9 @@
}, },
"devDependencies": { "devDependencies": {
"async": "~0.2.10", "async": "~0.2.10",
"benchmark": "^2.1.4",
"concat-stream": "~1.1.0", "concat-stream": "~1.1.0",
"duplex-child-process": "^1.0.0",
"gonna": "0.0.0", "gonna": "0.0.0",
"heroku-env": "~0.1.1", "heroku-env": "~0.1.1",
"lodash": "^4.17.11", "lodash": "^4.17.11",