AnyWhichWay
AnyWhichWay

Reputation: 774

Why is thin wrapper around Google Cloud Storage so slow for upload?

I built a very thin wrapper around Google Cloud Storage to make it look more like localStorage. I know that the same object can only be written once per second, but this code never writes anything faster than once per second and I can't figure out why. I have confirmed that the object names are different when actually using the code.

(function() {
    // a Google Cloud Storage API modeled somewhat after localStorage
    class CloudStore {
        constructor(name) {
            const me = this,
                Storage = require('@google-cloud/storage');
            me.storage = Storage();
            me.bucket = me.storage.bucket(name+"_anywhichway");
            me.bucket.exists().then((data) => {
                data[0] || me.storage.createBucket(name+"_anywhichway")
                           .then((data) => me.bucket = data[0]);
                me.bucket.getFiles((data) => {
                    !data || (me.files = data[0]);
                })
            });
        }
        async clear() {
            await this.bucket.deleteFiles({force:true});
        }
        async count() {
            return (this.files ? this.files.length : 0);
        }
        async delete(id) {
            this.storageProvider.removeItem(id);
            if(!this.files) {
                [this.files] = await this.bucket.getFiles();
            } else {
                const i = this.files.findIndex((item) => item.name===id);
                i===-1 || this.files.splice(0,i,1);
            }
        }
        async get(id) { // usually around 3,300 rec/sec
            const me = this,
                streams = require('memory-streams');
            return new Promise((resolve,reject) => {
                const writable = new streams.WritableStream();
                me.bucket.file(id).createReadStream({validation:false})
                  .on('error', (err) => { console.log(id,err.message); })
                  .on('response', (response) => {  })
                  .on('end', () =>  { resolve(writable.toString()); })
                  .pipe(writable);
            });
        }
        async key(number) {
            if(this.files[number]) return this.files[number].name;
        }
        async set(id,data) { // never faster than 1.01 rec/sec
            const me = this;
            return new Promise((resolve,reject) => {
                const file = me.bucket.file(id),
                    stream = file.createWriteStream();
                stream.end(data,"utf8",async () => {
                    // only get the file list once from Google 
                    // then manage a pseudo-list locally for speed
                    // tried commenting this out to see if it improved
                    // performance, it did not
                    if(!me.files) {
                        [me.files] = await me.bucket.getFiles();
                    } else {
                        me.files.push({name:id});
                    }
                    resolve(id);
                });
            });
        }
    }
    module.exports = CloudStore;
}).call(this);

Test code:

const storage = new CloudStore("test");
const size = 10,
start = Date.now();
for(let i=0;i<size;i++) {
        await storage.set("Object#"+i,JSON.stringify({item:i}));
}
const set= Date.now();
let expired = set- start,
    persec = size / (expired / 1000);
console.log("set",expired,persec);

Upvotes: 0

Views: 799

Answers (1)

AnyWhichWay
AnyWhichWay

Reputation: 774

It turns out that the API provided by Storage = require('@google-cloud/storage'); is pretty slow and only appropriate for larger objects. Using the more direct JSON API with POST moves right along at 150+ object saves per second.

Upvotes: 2

Related Questions