-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: borker concurrency stats (#93)
* feat: broker concurrency stats
- Loading branch information
Showing
24 changed files
with
513 additions
and
19 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
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
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
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
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 |
---|---|---|
|
@@ -92,6 +92,7 @@ function generateBrokerData( | |
activeRequestCount, | ||
}, | ||
], | ||
concurrency: activeRequestCount, | ||
}; | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -94,6 +94,7 @@ function generateBrokerData( | |
activeRequestCount, | ||
}, | ||
], | ||
concurrency: activeRequestCount, | ||
}; | ||
} | ||
|
||
|
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
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
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
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
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,113 @@ | ||
import { bufferFromStream, sleep } from '#self/lib/util'; | ||
import * as common from '#self/test/common'; | ||
import { DefaultEnvironment } from '#self/test/env/environment'; | ||
import sinon from 'sinon'; | ||
import assert from 'assert'; | ||
import { Metadata } from '#self/delegate/request_response'; | ||
import { TriggerErrorStatus } from '../request_logger'; | ||
import { Broker } from '#self/control_plane/worker_stats/broker'; | ||
import { ConcurrencyStatsMode } from '#self/lib/json/function_profile'; | ||
import { assertCloseTo } from '#self/test/util'; | ||
|
||
const { baselineDir } = common; | ||
|
||
describe(common.testName(__filename), function () { | ||
this.timeout(60_000); | ||
|
||
const env = new DefaultEnvironment(); | ||
|
||
it('should instant concurrency stats work as legacy mode', async () => { | ||
await env.agent.setFunctionProfile([ | ||
{ | ||
name: 'aworker_echo', | ||
runtime: 'aworker', | ||
url: `file://${baselineDir}/aworker_echo`, | ||
sourceFile: 'sleep.js', | ||
signature: 'md5:234234', | ||
worker: { | ||
maxActivateRequests: 1, | ||
}, | ||
}, | ||
]); | ||
|
||
request(env, 500); | ||
request(env, 500); | ||
|
||
await sleep(100); | ||
|
||
const dataFlowController = env.data.dataFlowController; | ||
const broker = dataFlowController.brokers.get('aworker_echo$$noinspect'); | ||
|
||
assert.strictEqual(broker?.toJSON().concurrency, 2); | ||
|
||
await sleep(1000); | ||
|
||
assert.strictEqual(broker?.toJSON().concurrency, 0); | ||
}); | ||
|
||
it('should periodic_max concurrency stats work', async () => { | ||
await env.agent.setFunctionProfile([ | ||
{ | ||
name: 'aworker_echo', | ||
runtime: 'aworker', | ||
url: `file://${baselineDir}/aworker_echo`, | ||
sourceFile: 'sleep.js', | ||
signature: 'md5:234234', | ||
worker: { | ||
maxActivateRequests: 1, | ||
concurrencyStatsMode: ConcurrencyStatsMode.PERIODIC_MAX, | ||
}, | ||
}, | ||
]); | ||
|
||
request(env, 500); | ||
request(env, 500); | ||
|
||
await sleep(100); | ||
|
||
const dataFlowController = env.data.dataFlowController; | ||
const broker = dataFlowController.brokers.get('aworker_echo$$noinspect'); | ||
|
||
await sleep(1000); | ||
|
||
assert.strictEqual(broker?.toJSON().concurrency, 2); | ||
}); | ||
|
||
it('should periodic_avg concurrency stats work', async () => { | ||
await env.agent.setFunctionProfile([ | ||
{ | ||
name: 'aworker_echo', | ||
runtime: 'aworker', | ||
url: `file://${baselineDir}/aworker_echo`, | ||
sourceFile: 'sleep.js', | ||
signature: 'md5:234234', | ||
worker: { | ||
maxActivateRequests: 1, | ||
concurrencyStatsMode: ConcurrencyStatsMode.PERIODIC_AVG, | ||
}, | ||
}, | ||
]); | ||
|
||
request(env, 500); | ||
request(env, 500); | ||
|
||
await sleep(1000); | ||
|
||
const dataFlowController = env.data.dataFlowController; | ||
const broker = dataFlowController.brokers.get('aworker_echo$$noinspect'); | ||
|
||
assertCloseTo(broker?.toJSON().concurrency!, 1, 0.2); | ||
}); | ||
}); | ||
|
||
async function request(env: DefaultEnvironment, timeout: number) { | ||
const response = await env.agent.invoke( | ||
'aworker_echo', | ||
Buffer.from('' + timeout), | ||
{ | ||
method: 'POST', | ||
} | ||
); | ||
|
||
await bufferFromStream(response); | ||
} |
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
35 changes: 35 additions & 0 deletions
35
src/lib/__test__/concurrency_stats/avg_concurrency_stats.test.ts
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,35 @@ | ||
import * as common from '#self/test/common'; | ||
import { AvgConcurrencyStats } from '#self/lib/concurrency_stats/avg_concurrency_stats'; | ||
import assert from 'assert'; | ||
import { sleep } from '#self/lib/util'; | ||
import { assertCloseTo } from '#self/test/util'; | ||
|
||
describe(common.testName(__filename), function () { | ||
this.timeout(30_000); | ||
|
||
it('should calculate concurrency correctly', async () => { | ||
const calculator: AvgConcurrencyStats = new AvgConcurrencyStats(console); | ||
// 假设有三个请求同时到达 | ||
const r1 = calculator.requestStarted(); | ||
const r2 = calculator.requestStarted(); | ||
const r3 = calculator.requestStarted(); | ||
|
||
// 请求结束 | ||
setTimeout(() => calculator.requestFinished(r1), 100); | ||
setTimeout(() => calculator.requestFinished(r2), 200); | ||
setTimeout(() => calculator.requestFinished(r3), 1500); | ||
|
||
await sleep(1000); | ||
// 剩一个 1500 未结束 | ||
// (3 / 1) * (((100 + 200 + 1000) / 3) / 1000) | ||
assertCloseTo(calculator.getConcurrency(), 1.3, 0.01); | ||
|
||
await sleep(1000); | ||
// 1500 的剩 500 | ||
// (1 / 1) * (((500) / 1) / 1000) | ||
assertCloseTo(calculator.getConcurrency(), 0.5, 0.01); | ||
|
||
await sleep(1000); | ||
assert.strictEqual(calculator.getConcurrency(), 0); | ||
}); | ||
}); |
Oops, something went wrong.