Skip to content

Commit

Permalink
Merge pull request #11 from consenlabs/performance-tunning
Browse files Browse the repository at this point in the history
  • Loading branch information
kaichen authored Jun 23, 2020
2 parents 8c927ff + 6341145 commit 21b9e2d
Show file tree
Hide file tree
Showing 28 changed files with 439 additions and 183 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# don't ever lint node_modules
node_modules
# don't lint build output (make sure it's set to your correct build folder name)
dist
# don't lint nyc coverage output
coverage
14 changes: 14 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
}
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Node.js CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Cache Node.js modules
uses: actions/cache@v1
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- run: npm install
- run: npm run build --if-present
- run: npm test
env:
CI: true
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ npm-debug.log
npm-debug.log.*
yarn-error.log
package-lock.json
lib
lib
.env
*.tgz
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# whitelist
!app/**/*
!lib/**/*
!package.json
!package-lock.json
!LICENSE
# blacklist
.*
node_modules
src
test
tsconfig.json

8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 100,
"tabWidth": 2,
"bracketSpacing": true,
"trailingComma": "es5",
"semi": false,
"singleQuote": true
}
20 changes: 19 additions & 1 deletion app/start.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
const mmConf = require('./mmConfig')
const mmsk = require('../lib')

mmsk.startMMSK(mmConf)
const cluster = require('cluster')
const numCPUs = require('os').cpus().length
const parallels = numCPUs > 8 ? 8 : numCPUs

if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);

// Fork workers.
for (let i = 0; i < parallels; i++) {
cluster.fork()
}

cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`)
})
} else {
mmsk.startMMSK(mmConf)
console.log(`Worker ${process.pid} started`)
}
143 changes: 143 additions & 0 deletions benchmark/MockServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import * as Koa from 'koa'
import * as Router from 'koa-router'
import * as BodyParser from 'koa-bodyparser'
import * as logger from 'koa-logger'
import * as JsonRPC from '@koalex/koa-json-rpc'

function makeMarketMakerRounter(): Router {
const router = new Router()
router.get('/pairs', function(ctx, _next) {
ctx.body = {
'result': true,
'pairs': [
'DAI/ETH',
],
}
})
router.get('/indicativePrice', function(ctx, _next) {
ctx.body = {
'result': true,
'exchangeable': true,
'price': 0.00017508,
'minAmount': 0.002,
'maxAmount': 100,
}
})
router.get('/price', function(ctx, _next) {
ctx.body = {
'result': true,
'exchangeable': true,
'price': 0.00017508,
'minAmount': 0.0002,
'maxAmount': 100,
'quoteId': 'asfadsf-dsfsdf-ggsd-qwe-rgjty',
}
})
router.get('/deal', function(ctx, _next) {
ctx.body = { result: true }
})
return router
}

function createRPCHandler(): JsonRPC {
const endpoint = new JsonRPC({
bodyParser: BodyParser({
onerror: (_err, ctx) => {
ctx.status = 200;
ctx.body = JsonRPC.parseError;
}
})
})
endpoint.method('tokenlon.getMarketMakerConfig', (ctx, _next) => {
// test address on kovan network
ctx.body = {
"mmId": 1,
"networkId": 3000,
"erc20ProxyContractAddress": "0xf1ec01d6236d3cd881a0bf0130ea25fe4234003e",
"exchangeContractAddress": "0x30589010550762d2f0d06f650d8e8b6ade6dbf4b",
"forwarderContractAddress": "0xd85e2fa7e7e252b27b01bf0d65c946959d2f45b8",
"zrxContractAddress": "0x2002d3812f58e35f0ea1ffbf80a75a38c32175fa",
"tokenlonExchangeContractAddress": "0xc23dc48e847ea67cde9a93d0df242f9584abc90d",
"wethContractAddress": "0xd0a1e359811322d97991e03f863a0c30c2cf029c",
"userProxyContractAddress": "0x97959853a0fb9a28432f1d4f46654fe524a12d81",
"orderExpirationSeconds": 600,
"mmProxyContractAddress": "0x974afc6906cdeb17f163b7a5a2d2a59aa488b94e",
"feeFactor": 30
}
})
endpoint.method('auth.getMMJwtToken', (ctx, _next) => {
ctx.body = "TODO"
})

endpoint.method('tokenlon.getTokenList', (ctx, _next) => {
ctx.body = [
{
"symbol": "ETH",
"logo": "https://cdn.example.com/mainnet-production/tokens/icons/eth%403x.png",
"contractAddress": "0x0000000000000000000000000000000000000000",
"decimal": 18,
"precision": 4,
"minTradeAmount": 1E-7,
"maxTradeAmount": 1E+1
},
{
"symbol": "DAI",
"logo": "https://cdn.example.com/mainnet-production/exchange-pairs/DAI.png",
"contractAddress": "0x5c964665b6379527b625be996020d861f27aa31d",
"decimal": 18,
"precision": 4,
"minTradeAmount": 0.001,
"maxTradeAmount": 1E+4
}]
})

endpoint.method('tokenlon.getTokenConfigsForMM', (ctx, _next) => {
ctx.body = [
{
"symbol": "ETH",
"feeFactor": 30
},
{
"symbol": "DAI",
"feeFactor": 30
}
]
})

endpoint.method('tokenlon.getOrdersHistoryForMM', (ctx, _next) => {
ctx.body = "TODO"
})

endpoint.method('tokenlon.getOrderStateForMM', (ctx, _next) => {
ctx.body = "TODO"
})

endpoint.method('hello', (ctx, _next) => {
// ctx.jsonrpc available
/*
ctx.jsonrpc.request
ctx.jsonrpc.id
ctx.jsonrpc.method [[Get]]
ctx.jsonrpc.params [[Get]]
ctx.jsonrpc.response
ctx.jsonrpc.result
ctx.jsonrpc.error
ctx.jsonrpc.code
ctx.jsonrpc.message
ctx.jsonrpc.data
*/
ctx.body = 'Hello world!'
})

return endpoint
}

const app = new Koa()
const router = makeMarketMakerRounter()
router.post('/rpc', createRPCHandler().middleware)
app
.use(BodyParser())
.use(logger())
.use(router.routes())
.use(router.allowedMethods())
app.listen(8088)
11 changes: 11 additions & 0 deletions benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Benchmark

```shell script
vim .env # <- prepare .env config file, see app/mmConfig for config items
npm instlal -g ganache-cli # <- install ganache chain
ganache-cli -i 3000 # <- launch ganache chain
npm install # <- install dependency
ts-node benchmark/MockServer.ts # <- launch mock server
node --inspect benchmark/Start.js # <- launch mmsk server
bash benchmark/bench.sh # <- run wrk
```
39 changes: 39 additions & 0 deletions benchmark/Start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const startMMSK = require("../lib").startMMSK
const loadEnvConfig = require("dotenv").config

async function main() {
loadEnvConfig()
startMMSK({
EXCHANGE_URL: process.env.EXCHANGE_URL,
WEBSOCKET_URL: process.env.WEBSOCKET_URL,
PROVIDER_URL: process.env.PROVIDER_URL,
USE_KEYSTORE: false,
WALLET_ADDRESS: process.env.WALLET_ADDRESS,
WALLET_PRIVATE_KEY: process.env.WALLET_PRIVATE_KEY,
MMSK_SERVER_PORT: 8080,
USE_ZERORPC: false,
HTTP_SERVER_ENDPOINT: process.env.HTTP_SERVER_ENDPOINT,
NODE_ENV: 'DEVELOPMENT',
})
}

const cluster = require('cluster')
const numCPUs = require('os').cpus().length
const parallels = numCPUs > 8 ? 8 : numCPUs

if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);

// Fork workers.
for (let i = 0; i < parallels; i++) {
cluster.fork()
}

cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`)
})
} else {
main().catch(console.error)

console.log(`Worker ${process.pid} started`)
}
4 changes: 4 additions & 0 deletions benchmark/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

wrk -t8 -c128 -d10s \
"http://127.0.0.1:8080/newOrder?base=DAI&quote=ETH&side=BUY&amount=1&userAddr=0xaaaaaaaaaa222222222233333333334444444444&uniqId=tqrcftxg&feefactor=0"
52 changes: 36 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
{
"name": "tokenlon-mmsk",
"version": "0.6.1",
"description": "",
"version": "0.6.2",
"description": "tokenlon market maker server kit",
"author": "imToken PTE. LTD.",
"license": "MIT",
"main": "lib/index.js",
"types": "src/globals.d.ts",
"homepage": "https://github.com/consenlabs/tokenlon-mmsk#readme",
"bugs": {
"url": "https://github.com/consenlabs/tokenlon-mmsk/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/consenlabs/tokenlon-mmsk.git"
},
"author": "imToken PTE. LTD.",
"license": "MIT",
"files": [
"app/**/*",
"lib/**/*"
],
"scripts": {
"watch": "tsc -w",
"clean": "rm -rf ./lib",
"build:commonjs": "tsc",
"build": "run-s clean build:commonjs",
"build:commonjs": "tsc",
"check": "node ./app/check.js",
"clean": "rm -rf ./lib",
"start": "node ./app/start.js",
"check": "node ./app/check.js"
},
"bugs": {
"url": "https://github.com/consenlabs/tokenlon-mmsk/issues"
"test": "mocha --exit -r ts-node/register test/**/*.spec.ts",
"watch": "tsc -w",
"release": "rm -f *.tgz{,.sha1sum.txt} && npm pack && sha1sum *.tgz > \"$(ls *.tgz | head -n 1).sha1sum.txt\""
},
"homepage": "https://github.com/consenlabs/tokenlon-mmsk#readme",
"devDependencies": {
"@types/node": "10.11.5",
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@types/node": "10.17.25",
"@typescript-eslint/eslint-plugin": "^3.2.0",
"@typescript-eslint/parser": "^3.2.0",
"chai": "^4.2.0",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.3",
"mocha": "^8.0.1",
"npm-run-all": "4.1.5",
"prettier": "^2.0.5",
"ts-node": "7.0.1",
"tslint": "5.11.0",
"typescript": "3.1.1"
"typescript": "^3.8.3"
},
"dependencies": {
"0x.js": "1.0.8",
"@0xproject/order-utils": "^1.0.1",
"@0xproject/utils": "^2.0.0",
"@babel/runtime": "7.3.1",
"@koalex/koa-json-rpc": "^1.0.3",
"@sentry/node": "4.5.3",
"axios": "0.19.0",
"axios-logger": "^2.3.1",
"babel-polyfill": "6.26.0",
"binance-api-node": "0.8.18",
"dotenv": "^8.2.0",
"keythereum": "1.0.4",
"koa": "2.5.3",
"koa-bodyparser": "4.2.1",
"koa-logger": "^3.2.1",
"koa-router": "7.4.0",
"lodash": "4.17.14",
"readline-sync": "1.4.9",
Expand Down
Loading

0 comments on commit 21b9e2d

Please sign in to comment.