我正在做我自己的乐趣的基准测试!我用许多编程语言编写了一部分代码,并使用ab进行基准测试,以查看哪一个更快,更多。我知道这个方法可能不是那么有效,不能用作一些明显的用法,但是为了我自己的信息,我正在这样做。我想知道的另一个因素是,在每种语言中编写相同的样本是多么容易/困难。我用Python/Python(asyncio),Haskell,Go,Kotlin和D编写了代码。我认为D端口比Go更快(或者至少等于速度)。但不幸的是我的D代码比Go慢得多。在这里,我放置了其他代码,请帮助我为什么代码不像预期那样快。或者我的期望绝对错了?为什么我的D代码不像预期的那样高性能?
import cbor;
import std.array : appender;
import std.format;
import std.json;
import vibe.vibe;
struct Location
{
float latitude;
float longitude;
float altitude;
float bearing;
}
RedisClient redis;
void main()
{
auto settings = new HTTPServerSettings;
redis = connectRedis("localhost", 6379);
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, &hello);
logInfo("Please open http://127.0.0.1:8080/ in your browser.");
runApplication();
}
void hello(HTTPServerRequest req, HTTPServerResponse res)
{
if (req.path == "/locations") {
immutable auto data = req.json;
immutable auto loc = deserializeJson!Location(data);
auto buffer = appender!(ubyte[])();
encodeCborAggregate!(Flag!"WithFieldName".yes)(buffer, loc);
auto db = redis.getDatabase(0);
db.set("Vehicle", cast(string) buffer.data);
res.writeBody("Ok");
}
}
这里是围棋
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
import "github.com/go-redis/redis"
import (
"bytes"
"github.com/2tvenom/cbor"
)
type Location struct {
Latitude float32 `json:"latitude"`
Longitude float32 `json:"longitude"`
Altitude float32 `json:"altitude"`
Bearing float32 `json:"bearing"`
}
func main() {
app := iris.New()
client := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
app.Post("/locations", func(ctx context.Context) {
var loc Location
ctx.ReadJSON(&loc)
var buffTest bytes.Buffer
encoder := cbor.NewEncoder(&buffTest)
encoder.Marshal(loc)
client.Set("vehicle", buffTest.Bytes(), 0)
client.Close()
ctx.Writef("ok")
})
app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8"))
}
使用AB,进去约4200请求/秒的结果,而d约2800请求/秒!
我没有看到任何明显的性能问题,但很难说,因为您的示例代码不可编译,因此我无法对其进行配置。有什么办法可以将它转换成http://www.sscce.org/?编辑:忘了说,但也请包括有关您使用什么编译器和在什么设置,这可能会导致巨大的差异的信息。 – cym13
我正在使用默认的D anf Go编译器!我的意思是最新版本的配音和Google Go!这些要求只是2个包: \t“依赖性”:{ \t“vibe-d”:“〜> 0.7.30”, “cbor-d”:“〜> 0.5。4" \t}, – Kamyar
什么表现的结果是,你得到什么?你期待什么呢? –