Coder Social home page Coder Social logo

demo-db-benchmark's Introduction

demo-db-benchmark

数据库性能测试:

  • 插入 500 万条数据
  • 分组查询

10万分组

针对 10w 条数据

  • 根据 category 分组
  • 对 age 进行 sum 聚合
  • 根据 rank 排序
import mongoose, { Schema, model } from "mongoose";

interface IUser {
  name: string;
  age: number;
  rank: number;
  category: string;
}

const userSchema = new Schema<IUser>({
  name: { type: String, required: true },
  age: { type: Number, required: true },
  rank: { type: Number, required: true },
  category: { type: String, required: true },
});

const UserModel = model<IUser>("TUser", userSchema);

async function query() {
  const start = Date.now();
  UserModel.aggregate(
    [
      {
        $group: {
          _id: "$name",
          count: { $sum: 1 },
          totalAge: { $sum: '$age' }
        }
      },
      {
        $sort: {
          totalAge: -1
        }
      },
      {
        $skip: 10000,
      },
      {
        $limit: 10
      },
    ],
    (_: any, data: any) => {
      # 345 稳定
      console.log(Date.now()  - start);
      console.log(data);
    }
  );
}

mongoose.connect("mongodb://47.112.180.188:27017").then(() => {
  query();
});

500 万分组

  • 并发数 1 CPU 飙升到 50%
  • 并发数 2 CPU 飙升到 100%
import mongoose, { Schema, model } from "mongoose";

interface IUser {
  name: string;
  age: number;
  rank: number;
  category: string;
}

const userSchema = new Schema<IUser>({
  name: { type: String, required: true },
  age: { type: Number, required: true },
  rank: { type: Number, required: true },
  category: { type: String, required: true },
});

const UserModel = model<IUser>("TUser", userSchema);

async function query() {
  const start = Date.now();
  UserModel.aggregate(
    [
      {
        $group: {
          _id: "$name",
          count: { $sum: 1 },
          totalAge: { $sum: '$age' }
        }
      },
      {
        $sort: {
          totalAge: -1
        }
      },
      {
        $skip: 0,
      },
      {
        $limit: 10
      },
    ],
    (_: any, data: any) => {
      # 8930
      console.log(Date.now()  - start);
      console.log(data);
    }
  );
}

mongoose.connect("mongodb://47.112.180.188:27017").then(() => {
  query();
});

demo-db-benchmark's People

Contributors

wasaier avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.