0

基本的にマングースクライアントを介してDBにデータを追加/取得する非常に単純なRESTfulサーバーがあります。サーバーはAmazonマイクロインスタンスであり、サーバーの統計を確認すると、非常に低い同時実行性で50%のCPU使用率のピークが時々表示されることがあります.10人以下のユーザーのようなものです. アイデアを得るために絵を見てください。

CPU使用率

別のタイムゾーンであるため、時間は無視してください。

私が考えることができる唯一の問題は、正しい「node.js」コールバック コードを記述していないことです。これは私のメイン モデルであり、関数はルートによって呼び出されます。

var _         = require('underscore'),
    mongoose  = require('mongoose'),
    sanitize  = require('validator').sanitize,
    Validator = require('validator').Validator,
    redis     = require('../models/reds'),
    v         = new Validator();

v.error = function(msg){
  return false;
};

var itemSchema = mongoose.Schema({
  adm:  { type: String },
  url:  { type: String, required: true },
  desc: { type: String, required: true, trim: true },
  tags: { type: [String] },
}, { autoIndex: false }).index({ tags: 1 });

var Male   = mongoose.model('Male', itemSchema),
    Female = mongoose.model('Female', itemSchema);

var idFromTime = function(time){
  var createId = function(int){ return mongoose.Types.ObjectId(int + '0000000000000000') };

  if(!time)
    return createId(Math.round(new Date().getTime() / 1000).toString(16));
    else return createId(time);
};

exports.getPhotos = function(options, next){
  var query,
      parameters = {},
      tags = [];

  if( v.validate(options.tags).is(/^[-\sa-z]+$/) && options.tags != 'undefined' ){
    tags = options.tags.split(' ');
    parameters['tags'] = { '$all': tags };
  }

  if(options.time && options.time.length == 8 && options.time.match(/[0-9a-f]+/))
    parameters['_id'] = { '$lt': idFromTime(options.time) };
    else parameters['_id'] = { '$lt': idFromTime() };

  if(options.female == true || options.male == false)      query = Female.find(parameters);
  else if(options.male == true || options.female == false) query = Male.find(parameters); 

  query
    .limit(29)
    .sort('-_id')
    .select('url desc')
    .exec(function(err, items) { next(err, items) });
};

exports.addPhoto = function(options, next){
  var photo,
      parameters,
      tags = [];

  if(
    options.desc != 'undefined' &&
    v.validate(options.desc).len(2, 100) &&
    v.validate(options.url).isUrl() &&
    v.validate(options.tags).is(/^[-\sa-z]+$/) ){
      tags = options.tags.split(' ');
      tags = _.compact(tags)
      tags = _.uniq(tags);

      parameters = {
        adm:  options.admin,
        url:  options.url,
        desc: options.desc,
        tags: tags,
      };
  } else {
    next(true);
    return false;
  }

  if(options.female == true || options.male == false)      photo = new Female(parameters);
  else if(options.male == true || options.female == false) photo = new Male(parameters);

  photo.save(function(err, photo){
    next(err, photo);
  });
};

exports.getComments = function(id, next){
  redis.lrange(id, 0, -1, function(err, list){
    next(err, list);
  });
};

exports.setComment = function(id, text, next){
  if(!text || 0 === text.length) {
    next(true);
  } else{
    redis.multi()
      .lpush(id, text)
      .ltrim(id, 0, 19)
      .expire(id, 2628000)
      .exec(function(err, reply){
        if(!err)
          next(false);
          else next(true);
      });
  }
}

サーバー上で mongodb と redis も実行しており、Web サイトで唯一の単一ページを表示するために、ジェイドとスタイラスの両方を使用しています。

コードにパフォーマンス関連の問題がありますか?それとも、マイクロ インスタンスのパフォーマンスが思ったほど高くないだけなので安心できますか?

4

0 に答える 0