-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #479 from WillAbides/go-reorg3
Reorganize Go code
- Loading branch information
Showing
37 changed files
with
646 additions
and
728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"benchmark" | ||
"encoding/base64" | ||
) | ||
|
||
func main() { | ||
err := benchmark.Run("", base64.StdEncoding) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"benchmark" | ||
"github.com/chenzhuoyu/base64x" | ||
) | ||
|
||
func main() { | ||
err := benchmark.Run("base64x", base64x.StdEncoding) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package benchmark | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"benchmarks/common" | ||
) | ||
|
||
const ( | ||
strSize = 131_072 | ||
tries = 8_192 | ||
) | ||
|
||
type Encoding interface { | ||
EncodeToString([]byte) string | ||
DecodeString(string) ([]byte, error) | ||
} | ||
|
||
func verify(encoding Encoding) error { | ||
for _, fixture := range [][]string{ | ||
{"hello", "aGVsbG8="}, | ||
{"world", "d29ybGQ="}, | ||
} { | ||
src := fixture[0] | ||
dst := fixture[1] | ||
encoded := encoding.EncodeToString([]byte(src)) | ||
if encoded != dst { | ||
return fmt.Errorf("%+v != %+v\n", encoded, dst) | ||
} | ||
decoded, err := encoding.DecodeString(dst) | ||
if err != nil { | ||
return err | ||
} | ||
if string(decoded) != src { | ||
return fmt.Errorf("%+v != %+v\n", decoded, src) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func Run(name string, encoding Encoding) error { | ||
err := verify(encoding) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bytes := []byte(strings.Repeat("a", strSize)) | ||
str2 := encoding.EncodeToString(bytes) | ||
str3, err := encoding.DecodeString(str2) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var encStart, encEnd, decStart, decEnd time.Time | ||
sizeEncoded, sizeDecoded := 0, 0 | ||
|
||
err = common.RunBenchmark(name, func() { | ||
encStart = time.Now() | ||
for i := 0; i < tries; i += 1 { | ||
sizeEncoded += len(encoding.EncodeToString(bytes)) | ||
} | ||
encEnd = time.Now() | ||
|
||
decStart = time.Now() | ||
for i := 0; i < tries; i += 1 { | ||
decoded, err := encoding.DecodeString(str2) | ||
if err != nil { | ||
return | ||
} | ||
sizeDecoded += len(decoded) | ||
} | ||
decEnd = time.Now() | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("encode %s... to %s...: %d, %.4f\n", | ||
string(bytes[:4]), str2[:4], sizeEncoded, encEnd.Sub(encStart).Seconds()) | ||
fmt.Printf("decode %s... to %s...: %d, %.4f\n", | ||
str2[:4], string(str3[:4]), sizeDecoded, decEnd.Sub(decStart).Seconds()) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module benchmark | ||
|
||
go 1.21 | ||
|
||
require ( | ||
benchmarks/common v0.0.0-00010101000000-000000000000 | ||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d | ||
) | ||
|
||
require ( | ||
github.com/bytedance/sonic v1.10.2 // indirect | ||
github.com/chenzhuoyu/iasm v0.9.1 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.6 // indirect | ||
golang.org/x/arch v0.6.0 // indirect | ||
golang.org/x/sys v0.14.0 // indirect | ||
) | ||
|
||
replace benchmarks/common => ../../common/go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.