我正在做我自己的樂趣的基準測試!我用許多編程語言編寫了一部分代碼,並使用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
什麼表現的結果是,你得到什麼?你期待什麼呢? –