mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
Make several small speed tweaks for binary reading & writing
This commit is contained in:
parent
0a90e018cd
commit
c5c474d0a6
@ -2,12 +2,16 @@
|
||||
|
||||
import { Writer } from './buffer-writer'
|
||||
import { serialize } from './index'
|
||||
import { BufferReader } from './buffer-reader'
|
||||
|
||||
const LOOPS = 1000
|
||||
let count = 0
|
||||
let start = Date.now()
|
||||
const writer = new Writer()
|
||||
|
||||
const reader = new BufferReader()
|
||||
const buffer = Buffer.from([33, 33, 33, 33, 33, 33, 33, 0])
|
||||
|
||||
const run = () => {
|
||||
if (count > LOOPS) {
|
||||
console.log(Date.now() - start)
|
||||
@ -15,8 +19,8 @@ const run = () => {
|
||||
}
|
||||
count++
|
||||
for(let i = 0; i < LOOPS; i++) {
|
||||
serialize.describe({ type: 'P'})
|
||||
serialize.describe({ type: 'S'})
|
||||
reader.setBuffer(0, buffer)
|
||||
reader.cstring()
|
||||
}
|
||||
setImmediate(run)
|
||||
}
|
||||
|
||||
@ -8,36 +8,44 @@ export class BufferReader {
|
||||
|
||||
constructor(private offset: number = 0) {
|
||||
}
|
||||
|
||||
public setBuffer(offset: number, buffer: Buffer): void {
|
||||
this.offset = offset;
|
||||
this.buffer = buffer;
|
||||
}
|
||||
public int16() {
|
||||
|
||||
public int16(): number {
|
||||
const result = this.buffer.readInt16BE(this.offset);
|
||||
this.offset += 2;
|
||||
return result;
|
||||
}
|
||||
public byte() {
|
||||
|
||||
public byte(): number {
|
||||
const result = this.buffer[this.offset];
|
||||
this.offset++;
|
||||
return result;
|
||||
}
|
||||
public int32() {
|
||||
|
||||
public int32(): number {
|
||||
const result = this.buffer.readInt32BE(this.offset);
|
||||
this.offset += 4;
|
||||
return result;
|
||||
}
|
||||
|
||||
public string(length: number): string {
|
||||
const result = this.buffer.toString(this.encoding, this.offset, this.offset + length);
|
||||
this.offset += length;
|
||||
return result;
|
||||
}
|
||||
|
||||
public cstring(): string {
|
||||
var start = this.offset;
|
||||
var end = this.buffer.indexOf(0, start);
|
||||
this.offset = end + 1;
|
||||
return this.buffer.toString(this.encoding, start, end);
|
||||
const start = this.offset;
|
||||
let end = start
|
||||
while(this.buffer[end++] !== 0) { };
|
||||
this.offset = end;
|
||||
return this.buffer.toString(this.encoding, start, end - 1);
|
||||
}
|
||||
|
||||
public bytes(length: number): Buffer {
|
||||
const result = this.buffer.slice(this.offset, this.offset + length);
|
||||
this.offset += length;
|
||||
|
||||
@ -214,11 +214,8 @@ export class Parser {
|
||||
const fields: any[] = new Array(fieldCount);
|
||||
for (let i = 0; i < fieldCount; i++) {
|
||||
const len = this.reader.int32();
|
||||
if (len === -1) {
|
||||
fields[i] = null
|
||||
} else if (this.mode === 'text') {
|
||||
fields[i] = this.reader.string(len)
|
||||
}
|
||||
// a -1 for length means the value of the field is null
|
||||
fields[i] = len === -1 ? null : this.reader.string(len)
|
||||
}
|
||||
return new DataRowMessage(length, fields);
|
||||
}
|
||||
@ -290,8 +287,8 @@ export class Parser {
|
||||
|
||||
private parseErrorMessage(offset: number, length: number, bytes: Buffer, name: MessageName) {
|
||||
this.reader.setBuffer(offset, bytes);
|
||||
var fields: Record<string, string> = {}
|
||||
var fieldType = this.reader.string(1)
|
||||
const fields: Record<string, string> = {}
|
||||
let fieldType = this.reader.string(1)
|
||||
while (fieldType !== '\0') {
|
||||
fields[fieldType] = this.reader.cstring()
|
||||
fieldType = this.reader.string(1)
|
||||
|
||||
@ -54,7 +54,7 @@ const run = async () => {
|
||||
queries = await bench(client, seq, seconds * 1000);
|
||||
console.log("sequence queries:", queries);
|
||||
console.log("qps", queries / seconds);
|
||||
console.log("on my laptop best so far seen 1209 qps")
|
||||
console.log("on my laptop best so far seen 1309 qps")
|
||||
|
||||
console.log('')
|
||||
queries = await bench(client, insert, seconds * 1000);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user