-
Notifications
You must be signed in to change notification settings - Fork 262
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
feat(JavaScript): Make PlatformBuffer available if has Buffer polyfill #1373
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f27b3c
feat(JavaScript): Make PlatformBuffer available if has Buffer polyfill
bytemain b702f9e
chore: add platform buffer benchmark
bytemain 12eecee
chore: add license header
bytemain ce9c610
chore: fix style
bytemain e120feb
chore(release): release 0.5.7-beta
bytemain 63b32fe
style: fix style
bytemain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,102 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import matplotlib.pyplot as plt | ||
import sys | ||
|
||
[ | ||
_, | ||
browser_utf8_write, | ||
browser_write, | ||
browser_write_1, | ||
native_write, | ||
browser_to_string, | ||
native_to_string, | ||
] = sys.argv[0:7] | ||
|
||
# 创建图形和子图 | ||
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 5), sharey=True) | ||
|
||
# 绘制第一部分比较:browser utf8Write 和 browser write | ||
axs[0].bar( | ||
["browser utf8Write", "browser write"], | ||
[browser_utf8_write, browser_write], | ||
color=["b", "g"], | ||
) | ||
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") | ||
|
||
|
||
# 在柱形图上添加数值标签 | ||
for p in axs[0].patches: | ||
axs[0].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 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"]) | ||
|
||
# 在柱形图上添加数值标签 | ||
for p in axs[2].patches: | ||
axs[2].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", | ||
) | ||
|
||
# 调整布局以避免重叠 | ||
plt.tight_layout() | ||
|
||
# 显示图形 | ||
plt.show() | ||
fig.savefig("./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.
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 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
const { BrowserBuffer } = require('@furyjs/fury/dist/lib/platformBuffer') | ||
const Benchmark = require("benchmark"); | ||
const { spawn } = require("child_process"); | ||
|
||
const jsonString = JSON.stringify({ | ||
id: 12346, | ||
name: "John Doe", | ||
email: "[email protected]", | ||
}); | ||
|
||
async function start() { | ||
const result = { | ||
browserCompare: {}, | ||
browserVsNativeWrite: {}, | ||
browserVsNativeToString: {}, | ||
} | ||
|
||
{ | ||
const platformBufferA = new BrowserBuffer(jsonString.length); | ||
const platformBufferB = new BrowserBuffer(jsonString.length); | ||
|
||
var suite = new Benchmark.Suite(); | ||
suite | ||
.add("browser utf8Write", function () { | ||
platformBufferA.utf8Write(jsonString, 0); | ||
}) | ||
.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); | ||
}); | ||
}) | ||
.run({ async: false }); | ||
console.log("Write operation per second") | ||
console.table(result.browserVsNativeWrite); | ||
} | ||
|
||
{ | ||
const browserBuffer = new BrowserBuffer(jsonString.length); | ||
const nativeBuffer = Buffer.alloc(jsonString.length); | ||
browserBuffer.write(jsonString, 0, 'utf8'); | ||
nativeBuffer.write(jsonString, 0, 'utf8'); | ||
|
||
var suite = new Benchmark.Suite(); | ||
suite | ||
.add("browser toString", function () { | ||
browserBuffer.toString('utf8', 0, jsonString.length); | ||
}) | ||
.add("native toString", function () { | ||
nativeBuffer.toString('utf8', 0, jsonString.length); | ||
}) | ||
.on("complete", function (e) { | ||
e.currentTarget.forEach(({ name, hz }) => { | ||
result.browserVsNativeToString[name] = Math.ceil(hz); | ||
}); | ||
}) | ||
.run({ async: false }); | ||
console.log("toString operation per second") | ||
console.table(result.browserVsNativeToString); | ||
} | ||
|
||
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"]], | ||
{ | ||
cwd: __dirname, | ||
} | ||
) | ||
} | ||
start(); |
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we change those comments to english?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I will send a pr later