| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/asn1" |
| 5 | "fmt" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | type Example struct { |
| 10 | Greeting string `asn1:"utf8"` |
| 11 | Answer int |
| 12 | Tipe asn1.ObjectIdentifier `asn1:"explicit,tag:1"` |
| 13 | } |
| 14 | |
| 15 | func main() { |
| 16 | iterations := 1000 |
| 17 | expected_output := []byte{0x30, 18, 12, 5, 72, 101, 108, 108, 111, 2, 1, 42, 0xA1, |
| 18 | 6, 6, 4, 43, 6, 1, 3} |
| 19 | |
| 20 | oid := []int{1, 3, 6, 1, 3} |
| 21 | |
| 22 | ex := Example{ |
| 23 | Greeting: "Hello", |
| 24 | Answer: 42, |
| 25 | Tipe: asn1.ObjectIdentifier(oid), |
| 26 | } |
| 27 | fmt.Println("Benchmarking golang Marshal...") |
| 28 | var totalMarshalTime int64 |
| 29 | for i := 0; i < iterations; i++ { |
| 30 | start := time.Now() |
| 31 | _, err := asn1.Marshal(ex) |
| 32 | if err != nil { |
| 33 | panic(err) |
| 34 | } |
| 35 | totalMarshalTime += time.Since(start).Microseconds() |
| 36 | } |
| 37 | avgMarshalTime := totalMarshalTime / int64(iterations) |
| 38 | fmt.Printf("Average Marshal time: %d µs\n", avgMarshalTime) |
| 39 | |
| 40 | fmt.Println("Benchmarking Unmarshal...") |
| 41 | var totalUnmarshalTime int64 |
| 42 | var xx Example |
| 43 | for i := 0; i < iterations; i++ { |
| 44 | start := time.Now() |
| 45 | _, err := asn1.Unmarshal(expected_output, &xx) |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | totalUnmarshalTime += time.Since(start).Microseconds() |
| 50 | } |
| 51 | avgUnmarshalTime := totalUnmarshalTime / int64(iterations) |
| 52 | fmt.Printf("Average Unmarshal time: %d µs\n", avgUnmarshalTime) |
| 53 | } |
| 54 | |