Skip to content

Commit 4ce3249

Browse files
committed
Initial commit
0 parents  commit 4ce3249

29 files changed

Lines changed: 1327 additions & 0 deletions

.github/workflows/go.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
14+
- name: Get the version
15+
id: release_name
16+
run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)
17+
18+
- name: Set up Go
19+
uses: actions/setup-go@v2
20+
with:
21+
go-version: 1.17
22+
23+
- name: Test
24+
run: go test -v ./...
25+
26+
- name: Create Release
27+
id: create_release
28+
uses: actions/create-release@v1
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
31+
with:
32+
tag_name: ${{ steps.release_name.outputs.VERSION }}
33+
release_name: Release ${{ steps.release_name.outputs.VERSION }}
34+
draft: false
35+
prerelease: false
36+
37+
- name: Push
38+
run: GOPROXY=proxy.golang.org go list -m github.com/arivum/msgpack@${{ steps.release_name.outputs.VERSION }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pprof
2+
*.swp

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 arivum
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
prepend-license:
3+
go run github.com/arivum/json2msgpackStreamer/scripts/prepend-license

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# msgpack
2+
3+
> :warning: DO NOT USE, STILL WIP!
4+
5+
At the minute, this module contains use-case-specific msgpack decoding improved for maximum performance.
6+
7+
[See package documentation](https://pkg.go.dev/github.com/arivum/msgpack)
8+
9+
## How to use
10+
11+
```go
12+
package main
13+
14+
import (
15+
"fmt"
16+
"strings"
17+
18+
"github.com/arivum/json2msgpackStreamer"
19+
"github.com/arivum/msgpack"
20+
)
21+
22+
func main() {
23+
24+
conv := json2msgpackStreamer.NewJSON2MsgPackStreamer(strings.NewReader(
25+
`{"a": "testvar", "b": ["b", "c"], "c": null, "d": false, "e": true, "g": -10, "f": -2.1, "h": "this is a longer text that needs a broader length indicator", "i": "sldkfj"}
26+
{"a": 4, "b": 18000000043000000233, "b2": -9000000043000000233, "c": null, "d": false, "e": true, "g": -10, "f": 2.0, "h": "this is a longer text that needs a broader length indicator", "i": "sldkfj"}
27+
{"a": "testvar", "b": ["b", "c"], "c": null}
28+
29+
{"a":"this is larger map test","b":"b","c":"c","d":"d","e":"e","f":"f","g":"g","h":"h","i":"i","j":"j","k":"k","l":"l","m":"m","n":"n","o":"o","p":"p","q": 1}
30+
31+
{"cmplx": {"a":"this is larger map test","b":"b","c":"c","d":"d","e":"e","f":"f","g":"g","h":"h","i":"i","j":"j","k":"k","l":"l","m":"m","n":"n","o":"o","p":"p","q": 1}}
32+
33+
["this is a larger slice test", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"]
34+
35+
{"cmplx": ["2nd", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"]}
36+
`,
37+
))
38+
39+
d := msgpack.NewDecoder(conv)
40+
for entry := range d.Stream() {
41+
fmt.Printf("%+v\n", entry)
42+
}
43+
fmt.Println(d.LastError())
44+
}
45+
46+
```

accumulated_buffer.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2022, arivum.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: MIT
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6+
*/
7+
8+
package msgpack
9+
10+
func newPreallocBuf() *preallocBuf {
11+
return &preallocBuf{
12+
buf: make([]byte, 1024),
13+
index: 0,
14+
}
15+
}
16+
17+
func (g *preallocBuf) allocateBuffer(len int) []byte {
18+
if g.index+len < 1024 {
19+
g.index += len
20+
return g.buf[g.index-len : g.index]
21+
}
22+
if len >= 1024 {
23+
return make([]byte, len)
24+
}
25+
g.buf = make([]byte, 1024)
26+
g.index = len
27+
return g.buf[0:len]
28+
}

cmd/example/main.go

Lines changed: 42 additions & 0 deletions
Large diffs are not rendered by default.

cmd/performance/helpers.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2022, arivum.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: MIT
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6+
*/
7+
8+
package main
9+
10+
import (
11+
"fmt"
12+
"io"
13+
"time"
14+
15+
"github.com/sirupsen/logrus"
16+
)
17+
18+
func (j *JSONFileInputAdapter) readLoop(w io.WriteCloser, r io.Reader) {
19+
var (
20+
err error
21+
start time.Time
22+
)
23+
defer func() {
24+
_ = w.Close()
25+
//close(j.finished)
26+
}()
27+
28+
start = time.Now()
29+
if _, err = io.Copy(w, readerFunc(func(p []byte) (int, error) {
30+
var (
31+
err error
32+
n int
33+
)
34+
35+
if n, err = r.Read(p); err == nil {
36+
j.readBytes += float64(n)
37+
}
38+
return n, err
39+
})); err != nil {
40+
logrus.Error(err)
41+
}
42+
logrus.Infof("processed file %s with an avg of %s", j.Filename, j.avgThroughput(time.Since(start)))
43+
}
44+
45+
func (j *JSONFileInputAdapter) avgThroughput(duration time.Duration) string {
46+
var (
47+
throughput = float64(j.filesize) / float64(duration.Seconds())
48+
multiplier = 0
49+
units = []string{
50+
"B/s",
51+
"kB/s",
52+
"MB/s",
53+
"GB/s",
54+
}
55+
)
56+
57+
for throughput/1024 > 1 {
58+
throughput /= 1024
59+
multiplier++
60+
}
61+
62+
return fmt.Sprintf("%.2f %s", throughput, units[multiplier])
63+
}
64+
65+
func (j *JSONFileInputAdapter) getMBperSec(resolution time.Duration) float64 {
66+
67+
var (
68+
avg = (j.readBytes - j.oldReadBytes) / (1 << 20) / resolution.Seconds()
69+
)
70+
71+
j.oldReadBytes = j.readBytes
72+
return avg
73+
}
74+
75+
// func (j *JSONFileInputAdapter) printStats() {
76+
// var (
77+
// readBytes = 0
78+
// resolution = 5 * time.Second
79+
// )
80+
81+
// for {
82+
// select {
83+
// case <-j.finished:
84+
// return
85+
// default:
86+
// time.Sleep(resolution)
87+
// logrus.Infof("file %s processed: %.2f%%, avg: %.2fMB/s", j.Filename, j.readBytes*100/float64(j.filesize), (j.readBytes-float64(readBytes))/(1<<20)/resolution.Seconds())
88+
// readBytes = int(j.readBytes)
89+
// }
90+
// }
91+
// }

cmd/performance/jsonfile.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2022, arivum.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: MIT
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6+
*/
7+
8+
package main
9+
10+
import (
11+
"io"
12+
"os"
13+
)
14+
15+
func (j *JSONFileInputAdapter) GetReader() (io.Reader, error) {
16+
var (
17+
file, err = os.OpenFile(j.Filename, os.O_RDONLY, 0600)
18+
r, w = io.Pipe()
19+
fInfo os.FileInfo
20+
)
21+
22+
if fInfo, err = os.Stat(j.Filename); err != nil {
23+
return nil, err
24+
}
25+
26+
j.filesize = fInfo.Size()
27+
28+
go j.readLoop(w, file)
29+
//go j.printStats()
30+
31+
return r, err
32+
}

0 commit comments

Comments
 (0)