Skip to content

Instantly share code, notes, and snippets.

@assada
Last active December 11, 2019 17:39
Show Gist options
  • Select an option

  • Save assada/e07177eb50f44ec3b6f6579af1e649d4 to your computer and use it in GitHub Desktop.

Select an option

Save assada/e07177eb50f44ec3b6f6579af1e649d4 to your computer and use it in GitHub Desktop.
$ ./test small 345ms ? Wed Dec 11 19:35:24 2019
2019/12/11 19:35:25 File Size: 66b
2019/12/11 19:35:26 [before Unmarshal] Alloc = 0 MiB | TotalAlloc = 0 MiB | Sys = 66 MiB | NumGC = 0
2019/12/11 19:35:26 [after Unmarshal] Alloc = 0 MiB | TotalAlloc = 0 MiB | Sys = 66 MiB | NumGC = 0
2019/12/11 19:35:26 [before Marshal] Alloc = 0 MiB | TotalAlloc = 0 MiB | Sys = 66 MiB | NumGC = 0
2019/12/11 19:35:26 [after Marshal] Alloc = 0 MiB | TotalAlloc = 0 MiB | Sys = 66 MiB | NumGC = 0
$ ./test medium ~/g/s/g/a/awesomeProject ? ./test medium Wed Dec 11 19:35:26 2019
2019/12/11 19:35:29 File Size: 100054b
2019/12/11 19:35:29 [before Unmarshal] Alloc = 0 MiB | TotalAlloc = 0 MiB | Sys = 66 MiB | NumGC = 0
2019/12/11 19:35:29 [after Unmarshal] Alloc = 0 MiB | TotalAlloc = 0 MiB | Sys = 66 MiB | NumGC = 0
2019/12/11 19:35:29 [before Marshal] Alloc = 0 MiB | TotalAlloc = 0 MiB | Sys = 66 MiB | NumGC = 0
2019/12/11 19:35:29 [after Marshal] Alloc = 0 MiB | TotalAlloc = 0 MiB | Sys = 66 MiB | NumGC = 0
$ ./test large ~/g/s/g/a/awesomeProject ? ./test medium2 Wed Dec 11 19:35:29 2019
2019/12/11 19:35:33 File Size: 11500054b
2019/12/11 19:35:33 [before Unmarshal] Alloc = 11 MiB | TotalAlloc = 11 MiB | Sys = 68 MiB | NumGC = 1
2019/12/11 19:35:33 [after Unmarshal] Alloc = 22 MiB | TotalAlloc = 22 MiB | Sys = 68 MiB | NumGC = 2
2019/12/11 19:35:33 [before Marshal] Alloc = 22 MiB | TotalAlloc = 22 MiB | Sys = 68 MiB | NumGC = 2
2019/12/11 19:35:33 [after Marshal] Alloc = 22 MiB | TotalAlloc = 43 MiB | Sys = 68 MiB | NumGC = 3
package main
import (
"encoding/json"
"fmt"
"github.com/assada/awesomeProject/data"
"io/ioutil"
"log"
"os"
"runtime"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func printMemUsage(phase string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Printf(
"[%v] Alloc = %v MiB | TotalAlloc = %v MiB | Sys = %v MiB | NumGC = %v",
phase,
bToMb(m.Alloc),
bToMb(m.TotalAlloc),
bToMb(m.Sys),
m.NumGC,
)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func main() {
arg := os.Args[1]
dat, err := ioutil.ReadFile("data/"+arg+".json")
check(err)
log.Printf("File Size: %vb", len(dat))
logStr := new(data.Log)
printMemUsage("before Unmarshal")
e := json.Unmarshal(dat, &logStr)
printMemUsage("after Unmarshal")
check(e)
printMemUsage("before Unmarshal")
bytes, e := json.Marshal(logStr)
printMemUsage("after Unmarshal")
check(e)
fmt.Print(len(bytes) / 1024 / 1024)
}
package data
type Log struct {
RequestId string `json:"request_id"`
Data string `json:"data"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment