为什么谷歌云存储的薄包装上传速度如此之慢?

时间:2017-07-25 04:13:20

标签: google-cloud-storage

我在Google云端存储周围构建了一个非常薄的包装器,使其看起来更像localStorage。我知道同一个对象每秒只能写一次,但是这段代码永远不会写得比每秒一次更快,我无法弄清楚原因。我确认在实际使用代码时对象名称是不同的。

(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);

测试代码:

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);

1 个答案:

答案 0 :(得分:1)

事实证明,Storage = require('@google-cloud/storage');提供的API非常慢,仅适用于较大的对象。使用带{POST的more direct JSON API可以每秒150+对象保存。