Skip to content

Commit

Permalink
chore: add platform buffer benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Feb 19, 2024
1 parent 1f27b3c commit c02fc14
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
73 changes: 73 additions & 0 deletions javascript/benchmark/platform-buffer-draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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

[_, browser_utf8_write, browser_write, browser_write_1, native_write, browser_to_string, native_to_string] = [0, 5495109, 5406273, 5444026, 9991813, 3492259, 15399687]

# 创建图形和子图
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., 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., 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., p.get_height()),
ha = 'center', va = 'center',
xytext = (0, 9),
textcoords = 'offset points')

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

# 显示图形
plt.show()
fig.savefig("./platform-buffer.jpg")
Binary file added 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.
113 changes: 113 additions & 0 deletions javascript/benchmark/platform-buffer.js
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();

0 comments on commit c02fc14

Please sign in to comment.