-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from consenlabs/performance-tunning
- Loading branch information
Showing
28 changed files
with
439 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ npm-debug.log | |
npm-debug.log.* | ||
yarn-error.log | ||
package-lock.json | ||
lib | ||
lib | ||
.env | ||
*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"e=ETH&side=BUY&amount=1&userAddr=0xaaaaaaaaaa222222222233333333334444444444&uniqId=tqrcftxg&feefactor=0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.