Monday, June 30, 2025
Social icon element need JNews Essential plugin to be activated.
No Result
View All Result
Crypto now 24
  • HOME
  • BITCOIN
  • CRYPTO UPDATES
    • GENERAL
    • ALTCOINS
    • ETHEREUM
    • CRYPTO EXCHANGES
    • CRYPTO MINING
  • BLOCKCHAIN
  • NFT
  • DEFI
  • METAVERSE
  • WEB3
  • REGULATIONS
  • SCAMS
  • ANALYSIS
  • VIDEOS
MARKETCAP
  • HOME
  • BITCOIN
  • CRYPTO UPDATES
    • GENERAL
    • ALTCOINS
    • ETHEREUM
    • CRYPTO EXCHANGES
    • CRYPTO MINING
  • BLOCKCHAIN
  • NFT
  • DEFI
  • METAVERSE
  • WEB3
  • REGULATIONS
  • SCAMS
  • ANALYSIS
  • VIDEOS
No Result
View All Result
Crypto now 24
No Result
View All Result

Practical Go benchmarks – IBM Blog

June 26, 2023
in Blockchain
Reading Time: 13 mins read
A A
0

[ad_1]

This assortment of sensible efficiency benchmarks of Go packages and algorithms is designed to assist builders write quick and environment friendly packages.

The next benchmarks consider varied functionalities with a concentrate on the usability of benchmark outcomes. Atmosphere: Go 1.10, Linux, Intel Core i7-4770HQ CPU @ 2.20GHz:

String concatenation

Numeric conversions

Common expressions

Sorting

Random numbers

Random strings

Slice appending

Map entry

Object creation

Hash features

Base64

File I/O

Serialization

Compression

URL parsing

Templates

HTTP server

See additionally:

Benchmark profiling with pprof

Go efficiency tuning

String concatenation

This benchmark evaluates the efficiency of string concatenation utilizing the + operator, the bytes.Buffer and the strings.Builder when constructing a 1,000-character string. The implementations utilizing the bytes.Buffer and the strings.Builder are the quickest:

package deal foremost

import (
“bytes”https://www.ibm.com/weblog/practical-go-benchmarks/”strings”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

var strLen int = 1000

func BenchmarkConcatString(b *testing.B) {
var str string

i := 0

b.ResetTimer()
for n := 0; n < b.N; n++ {
str += “x”

i++
if i >= strLen {
i = 0
str = “”
}
}
}

func BenchmarkConcatBuffer(b *testing.B) {
var buffer bytes.Buffer

i := 0

b.ResetTimer()
for n := 0; n < b.N; n++ {
buffer.WriteString(“x”)

i++
if i >= strLen {
i = 0
buffer = bytes.Buffer{}
}
}
}

func BenchmarkConcatBuilder(b *testing.B) {
var builder strings.Builder

i := 0

b.ResetTimer()
for n := 0; n < b.N; n++ {
builder.WriteString(“x”)

i++
if i >= strLen {
i = 0
builder = strings.Builder{}
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkConcatString-4 10,000,000 159 ns/op 530 B/op 0 allocs/op
BenchmarkConcatBuffer-4 200,000,000 10 ns/op 2 B/op 0 allocs/op
BenchmarkConcatBuilder-4 100,000,000 11 ns/op 2 B/op 0 allocs/op

Numeric conversions

This benchmark evaluates the efficiency of parsing strings to bool, int64 and float64 sorts utilizing the Go strconv package deal:

package deal foremost
import (
“strconv”
“testing”
)
func BenchmarkParseBool(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := strconv.ParseBool(“true”)
if err != nil {
panic(err)
}
}
}
func BenchmarkParseInt(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := strconv.ParseInt(“7182818284”, 10, 64)
if err != nil {
panic(err)
}
}
}
func BenchmarkParseFloat(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := strconv.ParseFloat(“3.1415926535”, 64)
if err != nil {
panic(err)
}
}
}
$ go take a look at -bench=. -benchmem
BenchmarkParseBool-4 300,000,000 4 ns/op 0 B/op 0 allocs/op
BenchmarkParseInt-4 50,000,000 25 ns/op 0 B/op 0 allocs/op
BenchmarkParseFloat-4 50,000,000 40 ns/op 0 B/op 0 allocs/op

Common expressions

This benchmark evaluates the efficiency of standard expression matching utilizing the Go regexp package deal for compiled and uncompiled common expressions. The instance makes use of a easy e-mail validation regexp. As anticipated, the compiled regexp matching is far sooner:

package deal foremost

import (
“regexp”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

var testRegexp string = `^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]+$`

func BenchmarkMatchString(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := regexp.MatchString(testRegexp, “jsmith@instance.com”)
if err != nil {
panic(err)
}
}
}

func BenchmarkMatchStringCompiled(b *testing.B) {
r, err := regexp.Compile(testRegexp)
if err != nil {
panic(err)
}

b.ResetTimer()
for n := 0; n < b.N; n++ {
r.MatchString(“jsmith@instance.com”)
}
}
$ go take a look at -bench=. -benchmem

BenchmarkMatchString-4 100,000 17,380 ns/op 42,752 B/op 70 allocs/op
BenchmarkMatchStringCompiled-4 2,000,000 843 ns/op 0 B/op 0 allocs/op

Sorting

This benchmark evaluates the efficiency of sorting 1,000, 10,000, 100,000 and 1,000,000-int parts utilizing the built-in sorting algorithm from the Go kind package deal. The time complexity is documented to be O(n*log(n)), which might be noticed within the outcomes:

package deal foremost

import (
“math/rand”https://www.ibm.com/weblog/practical-go-benchmarks/”kind”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

func generateSlice(n int) []int {
s := make([]int, 0, n)
for i := 0; i < n; i++ {
s = append(s, rand.Intn(1e9))
}
return s
}

func BenchmarkSort1000(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
s := generateSlice(1000)
b.StartTimer()
kind.Ints(s)
}
}

func BenchmarkSort10000(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
s := generateSlice(10000)
b.StartTimer()
kind.Ints(s)
}
}

func BenchmarkSort100000(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
s := generateSlice(100000)
b.StartTimer()
kind.Ints(s)
}
}

func BenchmarkSort1000000(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
s := generateSlice(1000000)
b.StartTimer()
kind.Ints(s)
}
}
$ go take a look at -bench=. -benchmem

BenchmarkSort1000-4 10,000 121,720 ns/op 32 B/op 1 allocs/op
BenchmarkSort10000-4 1,000 1,477,141 ns/op 32 B/op 1 allocs/op
BenchmarkSort100000-4 100 19,211,037 ns/op 32 B/op 1 allocs/op
BenchmarkSort1000000-4 5 220,539,215 ns/op 32 B/op 1 allocs/op

Random numbers

This benchmark compares the efficiency of pseudorandom quantity technology utilizing the Go math/rand and crypto/rand packages. The random quantity technology utilizing the mathematics/rand package deal is significantly sooner than the cryptographically safe random quantity technology utilizing the crypto/rand package deal:

package deal foremost

import (
crand “crypto/rand”https://www.ibm.com/weblog/practical-go-benchmarks/”math/large”https://www.ibm.com/weblog/practical-go-benchmarks/”math/rand”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

func BenchmarkMathRand(b *testing.B) {
for n := 0; n < b.N; n++ {
rand.Int63()
}
}

func BenchmarkCryptoRand(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := crand.Int(crand.Reader, large.NewInt(27))
if err != nil {
panic(err)
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkMathRand-4 50,000,000 23 ns/op 0 B/op 0 allocs/op
BenchmarkCryptoRand-4 1,000,000 1,336 ns/op 161 B/op 5 allocs/op

Random strings

This benchmark compares the efficiency of 16-character, uniformly distributed random string technology based mostly on the Go math/rand and crypto/rand. The random string technology utilizing math/rand package deal is quicker than the cryptographically safe random string technology utilizing the crypto/rand package deal:

package deal foremost

import (
crand “crypto/rand”https://www.ibm.com/weblog/practical-go-benchmarks/”math/rand”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

// 64 letters
const letters = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/”

func randomBytes(n int) []byte {
bytes := make([]byte, n)
_, err := rand.Learn(bytes)
if err != nil {
panic(err)
}

return bytes
}

func cryptoRandomBytes(n int) []byte {
bytes := make([]byte, n)
_, err := crand.Learn(bytes)
if err != nil {
panic(err)
}

return bytes
}

func randomString(bytes []byte) string {
for i, b := vary bytes {
bytes[i] = letters[b%64]
}

return string(bytes)
}

func BenchmarkMathRandString(b *testing.B) {
for n := 0; n < b.N; n++ {
randomString(randomBytes(16))
}
}

func BenchmarkCryptoRandString(b *testing.B) {
for n := 0; n < b.N; n++ {
randomString(cryptoRandomBytes(16))
}
}
$ go take a look at -bench=. -benchmem

BenchmarkMathRandString-4 10,000,000 119 ns/op 32 B/op 2 allocs/op
BenchmarkCryptoRandString-4 2,000,000 864 ns/op 32 B/op 2 allocs/op

Slice appending

This benchmark evaluates the efficiency of appending a byte to a slice with and with out slice preallocation:

package deal foremost

import (
“testing”
)

var numItems int = 1000000

func BenchmarkSliceAppend(b *testing.B) {
s := make([]byte, 0)

i := 0
b.ResetTimer()
for n := 0; n < b.N; n++ {
s = append(s, 1)

i++
if i == numItems {
b.StopTimer()
i = 0
s = make([]byte, 0)
b.StartTimer()
}
}
}

func BenchmarkSliceAppendPrealloc(b *testing.B) {
s := make([]byte, 0, numItems)

i := 0
b.ResetTimer()
for n := 0; n < b.N; n++ {
s = append(s, 1)

i++
if i == numItems {
b.StopTimer()
i = 0
s = make([]byte, 0, numItems)
b.StartTimer()
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkSliceAppend-4 1,000,000,000 2 ns/op 5 B/op 0 allocs/op
BenchmarkSliceAppendPrealloc-4 2,000,000,000 1 ns/op 0 B/op 0 allocs/op

Map entry

This benchmark evaluates the entry efficiency of maps with int vs. string keys for 1,000,000-item maps:

package deal foremost

import (
“math/rand”https://www.ibm.com/weblog/practical-go-benchmarks/”strconv”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

var NumItems int = 1000000

func BenchmarkMapStringKeys(b *testing.B) {
m := make(map[string]string)
ok := make([]string, 0)

for i := 0; i < NumItems; i++ {
key := strconv.Itoa(rand.Intn(NumItems))
m[key] = “worth” + strconv.Itoa(i)
ok = append(ok, key)
}

i := 0
l := len(m)

b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, okay := m[k[i]]; okay {
}

i++
if i >= l {
i = 0
}
}
}

func BenchmarkMapIntKeys(b *testing.B) {
m := make(map[int]string)
ok := make([]int, 0)

for i := 0; i < NumItems; i++ {
key := rand.Intn(NumItems)
m[key] = “worth” + strconv.Itoa(i)
ok = append(ok, key)
}

i := 0
l := len(m)

b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, okay := m[k[i]]; okay {
}

i++
if i >= l {
i = 0
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkMapStringKeys-4 20,000,000 107 ns/op 0 B/op 0 allocs/op
BenchmarkMapIntKeys-4 20,000,000 65 ns/op 0 B/op 0 allocs/op

Object creation

This benchmark evaluates the efficiency of object creation vs. object reuse utilizing sync.Pool:

package deal foremost

import (
“sync”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

kind E-book struct {
Title string
Creator string
Pages int
Chapters []string
}

var pool = sync.Pool{
New: func() interface{} {
return &E-book{}
},
}

func BenchmarkNoPool(b *testing.B) {
var e-book *E-book

for n := 0; n < b.N; n++ {
e-book = &E-book{
Title: “The Artwork of Laptop Programming, Vol. 1”,
Creator: “Donald E. Knuth”,
Pages: 672,
}
}

_ = e-book
}

func BenchmarkPool(b *testing.B) {
for n := 0; n < b.N; n++ {
e-book := pool.Get().(*E-book)
e-book.Title = “The Artwork of Laptop Programming, Vol. 1”
e-book.Creator = “Donald E. Knuth”
e-book.Pages = 672

pool.Put(e-book)
}
}
$ go take a look at -bench=. -benchmem

BenchmarkNoPool-4 30,000,000 45 ns/op 64 B/op 1 allocs/op
BenchmarkPool-4 100,000,000 22 ns/op 0 B/op 0 allocs/op

Hash features

This benchmark compares the efficiency of a number of hash features, together with MD5, SHA1, SHA256, SHA512, SHA3-256, SHA3-512, BLAKE2d-256 and BLAKE2d-256, from inside and exterior Go crypto subpackages on random 1 KB knowledge:

package deal foremost

import (
“crypto/md5″https://www.ibm.com/weblog/practical-go-benchmarks/”crypto/sha1″https://www.ibm.com/weblog/practical-go-benchmarks/”crypto/sha256″https://www.ibm.com/weblog/practical-go-benchmarks/”crypto/sha512″https://www.ibm.com/weblog/practical-go-benchmarks/”golang.org/x/crypto/blake2b”https://www.ibm.com/weblog/practical-go-benchmarks/”golang.org/x/crypto/sha3″https://www.ibm.com/weblog/practical-go-benchmarks/”hash”https://www.ibm.com/weblog/practical-go-benchmarks/”math/rand”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

func benchmarkHash(b *testing.B, h hash.Hash) {
knowledge := make([]byte, 1024)
rand.Learn(knowledge)

b.ResetTimer()
for n := 0; n < b.N; n++ {
h.Write(knowledge)
h.Sum(nil)
}
}

func BenchmarkMD5(b *testing.B) {
benchmarkHash(b, md5.New())
}

func BenchmarkSHA1(b *testing.B) {
benchmarkHash(b, sha1.New())
}

func BenchmarkSHA256(b *testing.B) {
benchmarkHash(b, sha256.New())
}

func BenchmarkSHA512(b *testing.B) {
benchmarkHash(b, sha512.New())
}

func BenchmarkSHA3256(b *testing.B) {
benchmarkHash(b, sha3.New256())
}

func BenchmarkSHA3512(b *testing.B) {
benchmarkHash(b, sha3.New512())
}

func BenchmarkBLAKE2b256(b *testing.B) {
h, _ := blake2b.New256(nil)
benchmarkHash(b, h)
}

func BenchmarkBLAKE2b512(b *testing.B) {
h, _ := blake2b.New512(nil)
benchmarkHash(b, h)
}
$ go take a look at -bench=. -benchmem

BenchmarkMD5-4 1,000,000 1,783 ns/op 16 B/op 1 allocs/op
BenchmarkSHA1-4 1,000,000 1,504 ns/op 32 B/op 1 allocs/op
BenchmarkSHA256-4 500,000 3,201 ns/op 32 B/op 1 allocs/op
BenchmarkSHA512-4 500,000 2,596 ns/op 64 B/op 1 allocs/op
BenchmarkSHA3256-4 300,000 4,485 ns/op 512 B/op 3 allocs/op
BenchmarkSHA3512-4 200,000 7,722 ns/op 576 B/op 3 allocs/op
BenchmarkBLAKE2b256-4 1,000,000 1,311 ns/op 32 B/op 1 allocs/op
BenchmarkBLAKE2b512-4 1,000,000 1,260 ns/op 64 B/op 1 allocs/op

Base64

This benchmark evaluates the efficiency of Base64 encoding and decoding utilizing the Go encoding/base64 package deal on 1 KB knowledge:

package deal foremost

import (
“encoding/base64″https://www.ibm.com/weblog/practical-go-benchmarks/”math/rand”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

func BenchmarkEncode(b *testing.B) {
knowledge := make([]byte, 1024)
rand.Learn(knowledge)

b.ResetTimer()
for n := 0; n < b.N; n++ {
base64.StdEncoding.EncodeToString([]byte(knowledge))
}
}

func BenchmarkDecode(b *testing.B) {
knowledge := make([]byte, 1024)
rand.Learn(knowledge)
encoded := base64.StdEncoding.EncodeToString([]byte(knowledge))

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
panic(err)
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkEncode-4 1,000,000 1,876 ns/op 2,816 B/op 2 allocs/op
BenchmarkDecode-4 500,000 2,957 ns/op 2,560 B/op 2 allocs/op

File I/O

This benchmark evaluates the efficiency of file writing and studying a 1 MB textual content file line by line with and with out buffering. The bufio package deal is used for buffered I/O:

package deal foremost

import (
“bufio”https://www.ibm.com/weblog/practical-go-benchmarks/”io”https://www.ibm.com/weblog/practical-go-benchmarks/”os”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

func BenchmarkWriteFile(b *testing.B) {
for n := 0; n < b.N; n++ {
f, err := os.Create(“/tmp/take a look at.txt”)
if err != nil {
panic(err)
}

for i := 0; i < 100000; i++ {
f.WriteString(“some textual content!n”)
}

f.Shut()
}
}

func BenchmarkWriteFileBuffered(b *testing.B) {
for n := 0; n < b.N; n++ {
f, err := os.Create(“/tmp/take a look at.txt”)
if err != nil {
panic(err)
}

w := bufio.NewWriter(f)

for i := 0; i < 100000; i++ {
w.WriteString(“some textual content!n”)
}

w.Flush()
f.Shut()
}
}

func BenchmarkReadFile(b *testing.B) {
for n := 0; n < b.N; n++ {
f, err := os.Open(“/tmp/take a look at.txt”)
if err != nil {
panic(err)
}

b := make([]byte, 10)

_, err = f.Learn(b)
for err == nil {
_, err = f.Learn(b)
}
if err != io.EOF {
panic(err)
}

f.Shut()
}
}

func BenchmarkReadFileBuffered(b *testing.B) {
for n := 0; n < b.N; n++ {
f, err := os.Open(“/tmp/take a look at.txt”)
if err != nil {
panic(err)
}

r := bufio.NewReader(f)

_, err = r.ReadString(‘n’)
for err == nil {
_, err = r.ReadString(‘n’)
}
if err != io.EOF {
panic(err)
}

f.Shut()
}
}
$ go take a look at -bench=. -benchmem

BenchmarkWriteFile-4 10 127,205,360 ns/op 118 B/op 4 allocs/op
BenchmarkWriteFileBuffered-4 300 5,978,219 ns/op 4,208 B/op 5 allocs/op
BenchmarkReadFile-4 20 53,226,890 ns/op 115 B/op 4 allocs/op
BenchmarkReadFileBuffered-4 200 7,518,203 ns/op 3,204,217 B/op 200,005 allocs/op

Serialization

This benchmark evaluates the efficiency of serialization and deserialization utilizing the json, protobuf, msgp and gob packages. The Protocol Buffers and MessagePack sorts must be generated first:

package deal foremost

import (
“bytes”https://www.ibm.com/weblog/practical-go-benchmarks/”encoding/gob”https://www.ibm.com/weblog/practical-go-benchmarks/”encoding/json”https://www.ibm.com/weblog/practical-go-benchmarks/”github.com/golang/protobuf/proto”https://www.ibm.com/weblog/practical-go-benchmarks/”io/ioutil”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

kind E-book struct {
Title string `json:”title”`
Creator string `json:”writer”`
Pages int `json:”num_pages”`
Chapters []string `json:”chapters”`
}

/*
syntax = “proto2”;
package deal foremost;

message BookProto {
required string title = 1;
required string writer = 2;
elective int64 pages = 3;
repeated string chapters = 4;
}
*/
// protoc –go_out=. e-book.proto

/*
//go:generate msgp -tests=false
kind BookDef struct {
Title string `msg:”title”`
Creator string `msg:”writer”`
Pages int `msg:”num_pages”`
Chapters []string `msg:”chapters”`
}
*/
// go generate

func generateObject() *E-book {
return &E-book{
Title: “The Artwork of Laptop Programming, Vol. 2”,
Creator: “Donald E. Knuth”,
Pages: 784,
Chapters: []string{“Random numbers”, “Arithmetic”},
}
}

func generateMessagePackObject() *BookDef {
obj := generateObject()
return &BookDef{
Title: obj.Title,
Creator: obj.Creator,
Pages: obj.Pages,
Chapters: obj.Chapters,
}
}

func generateProtoBufObject() *BookProto {
obj := generateObject()
return &BookProto{
Title: proto.String(obj.Title),
Creator: proto.String(obj.Creator),
Pages: proto.Int64(int64(obj.Pages)),
Chapters: obj.Chapters,
}
}

func BenchmarkJSONMarshal(b *testing.B) {
obj := generateObject()

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := json.Marshal(obj)
if err != nil {
panic(err)
}
}
}

func BenchmarkJSONUnmarshal(b *testing.B) {
out, err := json.Marshal(generateObject())
if err != nil {
panic(err)
}

obj := &E-book{}

b.ResetTimer()
for n := 0; n < b.N; n++ {
err = json.Unmarshal(out, obj)
if err != nil {
panic(err)
}
}
}

func BenchmarkProtoBufMarshal(b *testing.B) {
obj := generateProtoBufObject()

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := proto.Marshal(obj)
if err != nil {
panic(err)
}
}
}

func BenchmarkProtoBufUnmarshal(b *testing.B) {
out, err := proto.Marshal(generateProtoBufObject())
if err != nil {
panic(err)
}

obj := &BookProto{}

b.ResetTimer()
for n := 0; n < b.N; n++ {
err = proto.Unmarshal(out, obj)
if err != nil {
panic(err)
}
}
}

func BenchmarkMessagePackMarshal(b *testing.B) {
obj := generateMessagePackObject()

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := obj.MarshalMsg(nil)
if err != nil {
panic(err)
}
}
}

func BenchmarkMessagePackUnmarshal(b *testing.B) {
obj := generateMessagePackObject()
msg, err := obj.MarshalMsg(nil)
if err != nil {
panic(err)
}

obj = &BookDef{}

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err = obj.UnmarshalMsg(msg)
if err != nil {
panic(err)
}
}
}

func BenchmarkGobMarshal(b *testing.B) {
obj := generateObject()

enc := gob.NewEncoder(ioutil.Discard)

b.ResetTimer()
for n := 0; n < b.N; n++ {
err := enc.Encode(obj)
if err != nil {
panic(err)
}
}
}

func BenchmarkGobUnmarshal(b *testing.B) {
obj := generateObject()

var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(obj)
if err != nil {
panic(err)
}

for n := 0; n < b.N; n++ {
err = enc.Encode(obj)
if err != nil {
panic(err)
}
}

dec := gob.NewDecoder(&buf)

b.ResetTimer()
for n := 0; n < b.N; n++ {
err = dec.Decode(&E-book{})
if err != nil {
panic(err)
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkJSONMarshal-4 1,000,000 1,239 ns/op 640 B/op 3 allocs/op
BenchmarkJSONUnmarshal-4 500,000 3,249 ns/op 432 B/op 8 allocs/op
BenchmarkProtoBufMarshal-4 3,000,000 504 ns/op 552 B/op 5 allocs/op
BenchmarkProtoBufUnmarshal-4 2,000,000 692 ns/op 432 B/op 10 allocs/op
BenchmarkMessagePackMarshal-4 10,000,000 134 ns/op 160 B/op 1 allocs/op
BenchmarkMessagePackUnmarshal-4 5,000,000 252 ns/op 112 B/op 4 allocs/op
BenchmarkGobMarshal-4 2,000,000 737 ns/op 32 B/op 1 allocs/op
BenchmarkGobUnmarshal-4 1,000,000 1,005 ns/op 272 B/op 8 allocs/op

Compression

This benchmark evaluates the efficiency of compressing and decompressing 100 KB of JSON knowledge utilizing the Go compress/gzip package deal:

package deal foremost

import (
“bytes”https://www.ibm.com/weblog/practical-go-benchmarks/”compress/gzip”https://www.ibm.com/weblog/practical-go-benchmarks/”io/ioutil”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

func BenchmarkWrite(b *testing.B) {
knowledge, err := ioutil.ReadFile(“take a look at.json”)
if err != nil {
panic(err)
}

zw := gzip.NewWriter(ioutil.Discard)

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err = zw.Write(knowledge)
if err != nil {
panic(err)
}
}
}

func BenchmarkRead(b *testing.B) {
knowledge, err := ioutil.ReadFile(“take a look at.json”)
if err != nil {
panic(err)
}

var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
_, err = zw.Write(knowledge)
if err != nil {
panic(err)
}

err = zw.Shut()
if err != nil {
panic(err)
}

r := bytes.NewReader(buf.Bytes())
zr, _ := gzip.NewReader(r)

b.ResetTimer()
for n := 0; n < b.N; n++ {
r.Reset(buf.Bytes())
zr.Reset(r)
_, err := ioutil.ReadAll(zr)
if err != nil {
panic(err)
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkWrite-4 500 2,869,299 ns/op 1,627 B/op 0 allocs/op
BenchmarkRead-4 2,000 748,719 ns/op 261,088 B/op 22 allocs/op

URL parsing

This benchmark evaluates the efficiency of URL parsing utilizing the Go web/url package deal:

package deal foremost

import (
“web/url”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

func BenchmarkParse(b *testing.B) {
testUrl := “https://www.instance.com/path/file.html?param1=value1&param2=123”

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := url.Parse(testUrl)
if err != nil {
panic(err)
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkParse-4 3,000,000 413 ns/op 128 B/op 1 allocs/op

Templates

This benchmark evaluates the efficiency of template execution utilizing the Go textual content/template package deal:

package deal foremost

import (
“io/ioutil”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”https://www.ibm.com/weblog/practical-go-benchmarks/”textual content/template”
)

var bookTemplate string = `
Title: {{.Title}},
Creator: {{.Creator}}
{{ if .Pages}}
Variety of pages: {{ .Pages }}.
{{ finish }}
{{ vary .Chapters }}
{{ . }},
{{ finish }}
`

kind E-book struct {
Title string `json:”title”`
Creator string `json:”writer”`
Pages int `json:”num_pages”`
Chapters []string `json:”chapters”`
}

var e-book *E-book = &E-book{
Title: “The Artwork of Laptop Programming, Vol. 3”,
Creator: “Donald E. Knuth”,
Pages: 800,
Chapters: []string{“Sorting”, “Looking out”},
}

func BenchmarkExecute(b *testing.B) {
t := template.Should(template.New(“e-book”).Parse(bookTemplate))

for n := 0; n < b.N; n++ {
err := t.Execute(ioutil.Discard, e-book)
if err != nil {
panic(err)
}
}
}
$ go take a look at -bench=. -benchmem

BenchmarkExecute-4 500,000 2,986 ns/op 168 B/op 12 allocs/op

HTTP server

This benchmark evaluates the efficiency of HTTP and HTTPS native spherical journeys utilizing the default ServeMux from the Go web/http package deal:

package deal foremost

import (
“crypto/tls”https://www.ibm.com/weblog/practical-go-benchmarks/”io/ioutil”https://www.ibm.com/weblog/practical-go-benchmarks/”web”https://www.ibm.com/weblog/practical-go-benchmarks/”web/http”https://www.ibm.com/weblog/practical-go-benchmarks/”testing”
)

var httpServer *http.Server
var httpsServer *http.Server

kind testHandler struct {
}

func (th *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(“Content material-Kind”, “textual content/plain”)
w.Write([]byte(“OK.n”))
}

func startHTTPServer() {
if httpServer != nil {
return
}

httpServer = &http.Server{
Handler: &testHandler{},
}

listener, err := web.Hear(“tcp”, “:8080”)
if err != nil {
panic(err)
}

go func() {
err := httpServer.Serve(listener)
if err != nil {
panic(err)
}
}()
}

func startHTTPSServer() {
if httpsServer != nil {
return
}

httpsServer = &http.Server{
Handler: &testHandler{},
}

listener, err := web.Hear(“tcp”, “:8443”)
if err != nil {
panic(err)
}

go func() {
err := httpServer.ServeTLS(listener, “server.crt”, “server.key”)
if err != nil {
panic(err)
}
}()
}

func sendRequest(consumer *http.Shopper, addr string) {
res, err := consumer.Get(addr)
if err != nil {
panic(err)
}

if res.StatusCode != 200 {
panic(“request failed”)
}

_, err = ioutil.ReadAll(res.Physique)
if err != nil {
panic(err)
}

err = res.Physique.Shut()
if err != nil {
panic(err)
}
}

func BenchmarkHTTP(b *testing.B) {
startHTTPServer()

consumer := &http.Shopper{}

b.ResetTimer()
for n := 0; n < b.N; n++ {
sendRequest(consumer, “http://127.0.0.1:8080/”)
}
}

func BenchmarkHTTPNoKeepAlive(b *testing.B) {
startHTTPServer()

consumer := &http.Shopper{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}

b.ResetTimer()
for n := 0; n < b.N; n++ {
sendRequest(consumer, “http://127.0.0.1:8080/”)
}
}

func BenchmarkHTTPSNoKeepAlive(b *testing.B) {
startHTTPSServer()

consumer := &http.Shopper{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

b.ResetTimer()
for n := 0; n < b.N; n++ {
sendRequest(consumer, “https://127.0.0.1:8443/”)
}
}
$ go take a look at -bench=. -benchmem

BenchmarkHTTP-4 10,000 189,912 ns/op 5,736 B/op 70 allocs/op
BenchmarkHTTPNoKeepAlive-4 5,000 359,027 ns/op 17,204 B/op 123 allocs/op
BenchmarkHTTPSNoKeepAlive-4 300 4,052,008 ns/op 116,289 B/op 843 allocs/op

IBM Instana and Go benchmarks

In conclusion, the IBM Instana™ platform stands as a strong observability software that empowers organizations to achieve deep insights into their programs and functions. With its complete monitoring capabilities, the IBM Instana answer helps companies proactively detect and resolve points, optimize efficiency and facilitate the graceful operation of their infrastructure.

Moreover, the gathering of sensible efficiency benchmarks for Go packages provides immense worth to builders utilizing the Go programming language. By offering standardized measurements and comparisons, these benchmarks help in making knowledgeable selections about package deal choice, figuring out bottlenecks and optimizing code for optimum efficiency.

Mixed, the IBM Instana platform and the sensible efficiency benchmarks for Go packages contribute to enhancing system reliability, scalability and general software efficiency. With these instruments at their disposal, organizations and builders can confidently navigate the complexities of contemporary software program environments and ship distinctive person experiences.

Join a free, two-week trial of IBM Instana

Tags

[ad_2]

Source link

Tags: BenchmarksBlogIBMPractical
Previous Post

This is the reason YOU NEED to invest in Crypto Gaming! (BEST COINS & GAMES)

Next Post

Profiling CPU usage in Go

Next Post
Profiling CPU usage in Go

Profiling CPU usage in Go

Is it the Right Time to Buy Solana (SOL) and Polygon (MATIC)?

Is it the Right Time to Buy Solana (SOL) and Polygon (MATIC)?

Super Mario Game Is Loaded With Crypto Malware That Can Steal Your Coins

Super Mario Game Is Loaded With Crypto Malware That Can Steal Your Coins

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Social icon element need JNews Essential plugin to be activated.

CATEGORIES

  • Altcoin
  • Analysis
  • Bitcoin
  • Blockchain
  • Crypto Exchanges
  • Crypto Mining
  • Crypto Updates
  • DeFi
  • Ethereum
  • Metaverse
  • NFT
  • Regulations
  • Scam Alert
  • Uncategorized
  • Videos
  • Web3

SITE MAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 Crypto Now 24.
Crypto Now 24 is not responsible for the content of external sites.

No Result
View All Result
  • HOME
  • BITCOIN
  • CRYPTO UPDATES
    • GENERAL
    • ALTCOINS
    • ETHEREUM
    • CRYPTO EXCHANGES
    • CRYPTO MINING
  • BLOCKCHAIN
  • NFT
  • DEFI
  • METAVERSE
  • WEB3
  • REGULATIONS
  • SCAMS
  • ANALYSIS
  • VIDEOS

Copyright © 2023 Crypto Now 24.
Crypto Now 24 is not responsible for the content of external sites.