Created
December 11, 2019 19:21
-
-
Save assada/e93d8033d7542cc9fb3d8f12b6e40a2d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "io/ioutil" | |
| "log" | |
| "os" | |
| "runtime" | |
| "runtime/debug" | |
| ) | |
| type Log struct { | |
| RequestId string `json:"request_id"` | |
| Data string `json:"data"` | |
| } | |
| func check(e error) { | |
| if e != nil { | |
| panic(e) | |
| } | |
| } | |
| func printMemUsage(phase string) { | |
| var m runtime.MemStats | |
| runtime.ReadMemStats(&m) | |
| debug.FreeOSMemory() | |
| 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() { | |
| file := os.Args[1] | |
| var i int32 | |
| printMemUsage("before") | |
| for i = 0; i <= 1000 ; i++ { | |
| if(i == 100 || i == 400 || i == 600) { | |
| printMemUsage(fmt.Sprintf("I: %v", i)) | |
| } | |
| process(file) | |
| } | |
| printMemUsage("after") | |
| } | |
| func process(file string) { | |
| dat, err := ioutil.ReadFile("data/"+file+".json") | |
| check(err) | |
| log.Printf("File Size: %vb", len(dat)) | |
| logStr := new(Log) | |
| e := json.Unmarshal(dat, &logStr) | |
| check(e) | |
| bytes, e := json.Marshal(logStr) | |
| check(e) | |
| fmt.Print(len(bytes)) | |
| bytes = nil | |
| logStr = nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment