Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Dec 17, 2023
1 parent 27aa45b commit 401a5aa
Show file tree
Hide file tree
Showing 49 changed files with 7,319 additions and 6,167 deletions.
11,890 changes: 5,945 additions & 5,945 deletions benchmarks.json

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions benchmarks/counter/_meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@ description: >
This benchmark shows which implementation is the fastest, in which use-case.
tags:
- demo
- counter

contributors:
- MarvinJWendt

meta:
- implementation: Faster Over Time
- implementation: Atomic Pointer Counter
description: >
This benchmark gets faster, the more runs it has.
The Atomic Pointer Counter uses an `uint64` to store the counter value.
It will then use `atomic.AddUint64` to increase the counter value via a pointer.
- implementation: Slower Over Time
- implementation: Atomic Uint Counter
description: >
This benchmark gets slower, the more runs it has.
This implementation is very similar to the Atomic Pointer Counter,
but it uses an `atomic.Uint64` instead of a `uint64`.
That way, the counter value can be incremented directly, by calling its `Add` method.
- implementation: Faster With More CPU Cores
- implementation: Int Counter
description: >
This benchmark gets faster, the more CPU cores it has.
This implementation uses a simple `int` to store the counter value.
It will then use `i++` to increase the counter value.
This implementation is not thread-safe.
- implementation: Int Counter With Mutex
description: >
This implementation uses a simple `int` to store the counter value.
It will then use `i++` to increase the counter value.
This implementation is thread-safe, because it uses a mutex to synchronize the accesses.
36 changes: 36 additions & 0 deletions benchmarks/string-concatination/_meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: String Concatination
headline: A benchmark to compare the performance of different string concatination implementations in Go.
description: >
The classic string concatination is done by using the `+` operator.
This implementation works fine in the most cases, but it has some drawbacks.
One of them is that it is not very fast.
A solution is to use the `strings.Builder` or `buffer.Bytes` type.
This benchmark shows which implementation is the fastest, in which use-case.
tags:
- string
- concatination
- append

contributors:
- MarvinJWendt

meta:
- implementation: Append To Slice And Join
description: >
This implementation uses the `append` function to append the strings to a slice.
After that, the `strings.Join` function is used to join the strings together.
- implementation: Buffer
description: >
This implementation uses the `bytes.Buffer` type to append the strings.
After that, the `buffer.String` function is used to get the final string.
- implementation: Simple Append
description: >
This implementation uses the `+` operator to append the strings together.
- implementation: String Builder
description: >
This implementation uses the `strings.Builder` type to append the strings.
After that, the `builder.String` function is used to get the final string.
95 changes: 47 additions & 48 deletions cmd/internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/go-benchmarks/benchmarks/cmd/internal/utils"
"github.com/go-benchmarks/benchmarks/cmd/logger"
"github.com/goccy/go-yaml"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"golang.org/x/tools/benchmark/parse"
"log/slog"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -103,6 +102,8 @@ func ProcessBenchmarkGroups(logger *slog.Logger, benchmarksDir string) (groups [
return nil
})

benchmarkGroup.Code = strings.TrimSpace(benchmarkGroup.Code)

benchmarkGroup.Name = meta.Name
benchmarkGroup.Description = meta.Description
benchmarkGroup.Headline = meta.Headline
Expand Down Expand Up @@ -176,6 +177,8 @@ func ProcessBenchmarkGroups(logger *slog.Logger, benchmarksDir string) (groups [
return fmt.Errorf("failed to get benchmark code: %w", err)
}

benchmark.Code = strings.TrimSpace(benchmark.Code)

results = append(results, benchmark)
}

Expand All @@ -198,97 +201,93 @@ func ProcessBenchmarkGroups(logger *slog.Logger, benchmarksDir string) (groups [
}

func cleanCode(src string) (string, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "", src, parser.ParseComments)
if err != nil {
return "", err
}
// Remove import blocks and lines that start with "package"
re := regexp.MustCompile(`(?m)^import \([\s\S]*?\)\n|^package .*\n`)
src = re.ReplaceAllString(src, "")

var buf bytes.Buffer
for _, decl := range file.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
if decl.Tok == token.IMPORT {
continue
}
case *ast.FuncDecl:
if decl.Name.Name == "init" {
continue
}
}
printer.Fprint(&buf, fset, decl)
buf.WriteString("\n\n") // Add an additional newline character
// Replace multiple consecutive newline characters with a single newline character
re = regexp.MustCompile(`\n{3,}`)
src = re.ReplaceAllString(src, "\n")

src = strings.TrimSpace(src)
src += "\n\n"

if src == "\n\n" {
src = ""
}

return buf.String(), nil
return src, nil
}

func getConsts(src string) (string, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "", src, 0)
file, err := decorator.Parse(src)
if err != nil {
return "", err
}

var buf bytes.Buffer
for _, decl := range file.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
case *dst.GenDecl:
if decl.Tok == token.CONST {
printer.Fprint(&buf, fset, decl)
decorator.Fprint(&buf, file)
buf.WriteString("\n\n") // Add an additional newline character
}
}
}

return buf.String(), nil
return cleanCode(buf.String())
}

func getBenchmarkCode(src, name string) (string, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "", "package main\n"+src, parser.ParseComments)
src, _ = cleanCode(src)
src = "package dummy\n\n" + src
file, err := decorator.Parse(src)
if err != nil {
return "", err
}

if file == nil {
return "", fmt.Errorf("parsed file is nil")
}

var buf bytes.Buffer
ast.Inspect(file, func(n ast.Node) bool {
newFile := &dst.File{}
dst.Inspect(file, func(n dst.Node) bool {
switch decl := n.(type) {
case *ast.GenDecl:
case *dst.GenDecl:
if decl.Tok == token.TYPE {
for _, spec := range decl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
typeSpec, ok := spec.(*dst.TypeSpec)
if ok && typeSpec.Name.Name == name {
printer.Fprint(&buf, fset, decl)
buf.WriteString("\n\n")
newFile.Decls = append(newFile.Decls, decl)
}
}
}
case *ast.FuncDecl:
case *dst.FuncDecl:
if decl.Recv != nil && len(decl.Recv.List) > 0 {
var recvTypeName string

switch expr := decl.Recv.List[0].Type.(type) {
case *ast.StarExpr:
recvTypeName = expr.X.(*ast.Ident).Name
case *ast.Ident:
case *dst.StarExpr:
if expr.X != nil {
recvTypeName = expr.X.(*dst.Ident).Name
}
case *dst.Ident:
recvTypeName = expr.Name
}

if recvTypeName == name {
printer.Fprint(&buf, fset, decl)
buf.WriteString("\n\n")
newFile.Decls = append(newFile.Decls, decl)
}
} else if strings.HasPrefix(decl.Name.Name, "Benchmark"+name+"_") {
printer.Fprint(&buf, fset, decl)
buf.WriteString("\n\n")
newFile.Decls = append(newFile.Decls, decl)
}
case *ast.Comment, *ast.CommentGroup:
logger.New(true).Debug("comment", "comment")

}
return true
})

return buf.String(), nil
newFile.Name = dst.NewIdent("dummy")
decorator.Fprint(&buf, newFile)
return cleanCode(buf.String())
}

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/_app/immutable/assets/6.2f30cb5d.css

This file was deleted.

1 change: 1 addition & 0 deletions docs/_app/immutable/assets/6.4bc083bf.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.description.svelte-8jjkd2{max-width:48rem}

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/_app/immutable/assets/_page.2f30cb5d.css

This file was deleted.

1 change: 1 addition & 0 deletions docs/_app/immutable/assets/_page.4bc083bf.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.description.svelte-8jjkd2{max-width:48rem}
Loading

0 comments on commit 401a5aa

Please sign in to comment.