Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(JavaScript): update benchmark #1375

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 18 additions & 42 deletions javascript/benchmark/platform-buffer-draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@
import sys

[
_,
browser_utf8_write,
browser_write,
browser_write_1,
native_write,
browser_to_string,
native_to_string,
] = sys.argv[0:7]
] = sys.argv[1:6]

# 创建图形和子图
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 5), sharey=True)
browser_utf8_write = int(browser_utf8_write)
browser_write = int(browser_write)
native_write = int(native_write)
browser_to_string = int(browser_to_string)
native_to_string = int(native_to_string)

fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5), sharey=True)

# 绘制第一部分比较:browser utf8Write 和 browser write
axs[0].bar(
["browser utf8Write", "browser write"],
[browser_utf8_write, browser_write],
color=["b", "g"],
["browser utf8Write", "browser write", "native write"],
[browser_utf8_write, browser_write, native_write],
color=["b", "g", "r"],
)
axs[0].set_title("Browser UTF8 Write vs Browser Write")
axs[0].set_xlabel("Operation Type")
axs[0].set_xticklabels(["browser utf8Write", "browser write"])
axs[0].set_ylabel("Tps")
axs[0].set_title("Write Comparison")
axs[0].set_xticklabels(["browser utf8Write", "browser write", "native write"])
axs[0].set_ylabel("TPS")


# 在柱形图上添加数值标签
for p in axs[0].patches:
axs[0].annotate(
format(p.get_height(), ".0f"),
Expand All @@ -54,38 +54,16 @@
textcoords="offset points",
)

# 绘制第二部分比较:browser write 和 native write
axs[1].bar(
["browser write", "native write"], [browser_write_1, native_write], color=["g", "r"]
)
axs[1].set_title("Browser Write vs Native Write")
axs[1].set_xlabel("Operation Type")
axs[1].set_xticklabels(["browser write", "native write"])

# 在柱形图上添加数值标签
for p in axs[1].patches:
axs[1].annotate(
format(p.get_height(), ".0f"),
(p.get_x() + p.get_width() / 2.0, p.get_height()),
ha="center",
va="center",
xytext=(0, 9),
textcoords="offset points",
)

# 绘制第三部分比较:browser toString 和 native toString
axs[2].bar(
["browser toString", "native toString"],
[browser_to_string, native_to_string],
color=["b", "r"],
)
axs[2].set_title("Browser ToString vs Native ToString")
axs[2].set_xlabel("Operation Type")
axs[2].set_xticklabels(["browser toString", "native toString"])
axs[1].set_title("toString Comparison")
axs[1].set_xticklabels(["browser toString", "native toString"])

# 在柱形图上添加数值标签
for p in axs[2].patches:
axs[2].annotate(
for p in axs[1].patches:
axs[1].annotate(
format(p.get_height(), ".0f"),
(p.get_x() + p.get_width() / 2.0, p.get_height()),
ha="center",
Expand All @@ -94,9 +72,7 @@
textcoords="offset points",
)

# 调整布局以避免重叠
plt.tight_layout()

# 显示图形
plt.show()
fig.savefig("./platform-buffer.jpg")
Binary file modified javascript/benchmark/platform-buffer.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 13 additions & 27 deletions javascript/benchmark/platform-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const jsonString = JSON.stringify({

async function start() {
const result = {
browserCompare: {},
browserVsNativeWrite: {},
browserVsNativeToString: {},
writeComparison: {},
toStringComparison: {},
}

{
const platformBufferA = new BrowserBuffer(jsonString.length);
const platformBufferB = new BrowserBuffer(jsonString.length);
const nativeBuffer = Buffer.alloc(jsonString.length);

var suite = new Benchmark.Suite();
suite
Expand All @@ -46,36 +46,17 @@ async function start() {
.add("browser write", function () {
platformBufferB.write(jsonString, 0, 'utf8');
})
.on("complete", function (e) {
e.currentTarget.forEach(({ name, hz }) => {
result.browserCompare[name] = Math.ceil(hz);
});
})
.run({ async: false });
console.log("Write operation per second")
console.table(result.browserCompare);
}

{
const browserBuffer = new BrowserBuffer(jsonString.length);
const nativeBuffer = Buffer.alloc(jsonString.length);

var suite = new Benchmark.Suite();
suite
.add("browser write", function () {
browserBuffer.write(jsonString, 0, 'utf8');
})
.add("native write", function () {
nativeBuffer.write(jsonString, 0, 'utf8');
})
.on("complete", function (e) {
e.currentTarget.forEach(({ name, hz }) => {
result.browserVsNativeWrite[name] = Math.ceil(hz);
result.writeComparison[name] = Math.ceil(hz);
});
})
.run({ async: false });
console.log("Write operation per second")
console.table(result.browserVsNativeWrite);
console.table(result.writeComparison);
}

{
Expand All @@ -94,17 +75,22 @@ async function start() {
})
.on("complete", function (e) {
e.currentTarget.forEach(({ name, hz }) => {
result.browserVsNativeToString[name] = Math.ceil(hz);
result.toStringComparison[name] = Math.ceil(hz);
});
})
.run({ async: false });
console.log("toString operation per second")
console.table(result.browserVsNativeToString);
console.table(result.toStringComparison);
}

const args = ['platform-buffer-draw.py', result.writeComparison['browser utf8Write'], result.writeComparison['browser write'], result.writeComparison['native write'], result.toStringComparison['browser toString'], result.toStringComparison['native toString']];

console.log("Running python script to draw the graph")
console.log("python3", ...args)

spawn(
`python3`,
['platform-buffer-draw.py', result.browserCompare["browser utf8Write"], result.browserCompare["browser write"], result.browserVsNativeWrite["browser write"], result.browserVsNativeWrite["native write"], result.browserVsNativeToString["browser toString"], result.browserVsNativeToString["native toString"]],
['platform-buffer-draw.py', result.writeComparison['browser utf8Write'], result.writeComparison['browser write'], result.writeComparison['native write'], result.toStringComparison['browser toString'], result.toStringComparison['native toString']],
{
cwd: __dirname,
}
Expand Down
Loading