2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

aws-sdkでアップロードした時の速度を計測する

Last updated at Posted at 2015-07-07

aws-sdkを用いて、s3にファイルをアップロードした時の速度を計測したときのメモ。

app.js
var AWS = require('aws-sdk');
var fs = require('fs');

var filename = './testfile';
var filesize = fs.statSync(filename).size;
var totalTime;

AWS.events.on('send', function startSend(resp) {
  resp.startTime = new Date().getTime();
}).on('complete', function calculateTime(resp) {
  var time = (new Date().getTime() - resp.startTime) / 1000;

  switch(resp.request.operation) {
  case 'createMultipartUpload':
    totalTime = time;
    break;
  case 'uploadPart':
    totalTime += time;
    break;
  case 'completeMultipartUpload':
    totalTime += time;
    console.log(filesize + ' bytes uploaded, ' + round(totalTime, 3) + ' s, ' + round((filesize / totalTime) / 1024 / 1024, 1) + ' MB/s');
    break;
  case 'putObject':
    totalTime = time;
    console.log(filesize + ' bytes uploaded, ' + round(totalTime, 3) + ' s, ' + round((filesize / totalTime) / 1024 / 1024, 1) + ' MB/s');
    break;
  }
});

var s3 = new AWS.S3();

var params = {
  Bucket: 'bucket',
  Key: 'key',
  Body: fs.createReadStream(filename)
};

s3.upload(params, function(err, data) {
  if (err) console.log(err, err.stack);
});

function round(val, n) {
  var pow = Math.pow(10, n);
  return Math.round(val * pow) / pow;
}

アップロードしたサイズをかかった時間で割っただけ。
丸め誤差は気にしない。

#References

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?