Design

Design
asp.net mvc

2019年11月7日 星期四

[紀錄]nodejs Mongodb分頁模糊查詢

 
app.post('/api/video', function (req, res) {
  try {
    var PageSize = req.body.PageSize;
    var PageIndex = req.body.PageIndex;
    if (!PageSize || !PageIndex) {
      return;
    }

    var Search = {};
    if (req.body.Search) {
      Search = {
        'name': {
          $regex: `^.*${req.body.Search}.*`,
          $options: 'i'
        }
      }
    }
    var dbo = client.db("db");

    dbo.collection('video').find(Search).sort({
      upload_at: -1
    }).skip(PageSize * PageIndex - PageSize).limit(PageSize).toArray((err, items) => {
      if (err) {
        console.log(err)
        throw err;
      }
      dbo.collection('video').countDocuments(Search, function (err, count) {
        res.end(JSON.stringify({
          "result": items,
          "pageCount": Math.ceil(count / PageSize),
          "videoCount": count,
        }));
      client.close();
      })
    });
  } catch (e) {
    console.error(e);
  }
});

MessagePack vs json.Net 效能PK

Perf comparison graph

一張圖吸引了我去測試MessagePack的效能
至於是不是真的這麼神  我們時間跑看看測試程式碼跟執行結果如下

Main:
    public  static void Main(string[] args)
        {
            MessagePackSerializer.SetDefaultResolver(MessagePack.Resolvers.ContractlessStandardResolver.Instance);

            var summary = BenchmarkRunner.Run();
            Console.Read();
        }


Measure:

 
using BenchmarkDotNet.Attributes;
using MessagePack;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SerializationPerformance
{
    [MemoryDiagnoser]
    public class Measure
    {

        public readonly MyClass data;
        public byte[] bytes;
        public string Json_string;
        public Measure()
        {
            MessagePackSerializer.SetDefaultResolver(MessagePack.Resolvers.ContractlessStandardResolver.Instance);
            data = new MyClass
            {
                Age = 99,
                FirstName = "hoge",
                LastName = "huga",
            };
            bytes = LZ4MessagePackSerializer.Serialize(data);
            Json_string = LZ4MessagePackSerializer.ToJson(bytes);
        }



        [Benchmark]
        public byte[] MessagePack_Serialize_Bytes()
        {
           byte[] result = MessagePackSerializer.Serialize(data);
            return result;
        }


        [Benchmark]
        public string MessagePack_Serialize_String()
        {
            byte[] result = MessagePackSerializer.Serialize(data);
            
            return MessagePackSerializer.ToJson(result);
        }


        [Benchmark]
        public MyClass MessagePack_Deserialize()
        {
            MyClass result = MessagePackSerializer.Deserialize(LZ4MessagePackSerializer.FromJson(Json_string));
            return result;
        }


        [Benchmark]
        public byte[] LZ4MessagePack_Serialize_Bytes()
        {
            var result = LZ4MessagePackSerializer.Serialize(data);
            return result;
        }


        [Benchmark]
        public string LZ4MessagePack_Serialize_String()
        {
            var result = LZ4MessagePackSerializer.Serialize(data);
            return MessagePackSerializer.ToJson(result);
        }

    
        [Benchmark]
        public MyClass LZ4MessagePack_Deserialize()
        {
           
            MyClass result = LZ4MessagePackSerializer.Deserialize(LZ4MessagePackSerializer.FromJson(Json_string));
            return result;
        }

        [Benchmark]
        public string jsonNet_Serialize()
        {

            string result = JsonConvert.SerializeObject(data);
            return result;
        }



        [Benchmark]
        public MyClass jsonNet_Deserialize()
        {
            var result = JsonConvert.DeserializeObject(Json_string);
            return result;
        }
    }

    public class MyClass
    {

        public int Age { get; set; }


        public string FirstName { get; set; }


        public string LastName { get; set; }

        public string FullName { get { return FirstName + LastName; } }
    }

}
可以看到我們較常用的object跟string之間的轉換速度上其實沒太大差異,如果單純只是二進制的轉換會快非常多 至於內存消耗可以看到MessagePack是優於json.Net不過型別的支援程度json.Net還是比較好用的 如果對於效能有要求這邊是滿推薦使用MessagePack只是在一些型別例如datetime jobject jarray等等格式就需要做另外formatter處理,就看個人的喜好決定了 轉為json字串也需要多打一串他的方法