|
| 1 | +package formatter |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// closeableBuffer wraps bytes.Buffer to implement io.WriteCloser |
| 9 | +type closeableBuffer struct { |
| 10 | + *bytes.Buffer |
| 11 | +} |
| 12 | + |
| 13 | +func (cb *closeableBuffer) Close() error { |
| 14 | + return nil |
| 15 | +} |
| 16 | + |
| 17 | +// Create a realistic NMAP run for benchmarking |
| 18 | +func createBenchmarkNMAPRun() *NMAPRun { |
| 19 | + return &NMAPRun{ |
| 20 | + Scanner: "nmap", |
| 21 | + Args: "-sV -sC -p- -T4", |
| 22 | + Start: 1234567890, |
| 23 | + StartStr: "2009-02-13 23:31:30 UTC", |
| 24 | + Version: "7.80", |
| 25 | + ScanInfo: ScanInfo{ |
| 26 | + Type: "syn", |
| 27 | + Protocol: "tcp", |
| 28 | + NumServices: 1000, |
| 29 | + Services: "1-1000", |
| 30 | + }, |
| 31 | + Host: []Host{ |
| 32 | + { |
| 33 | + StartTime: 1234567890, |
| 34 | + EndTime: 1234567900, |
| 35 | + HostAddress: []HostAddress{ |
| 36 | + {Address: "192.168.1.1", AddressType: "ipv4"}, |
| 37 | + {Address: "00:11:22:33:44:55", AddressType: "mac", Vendor: "Vendor Inc"}, |
| 38 | + }, |
| 39 | + HostNames: HostNames{ |
| 40 | + HostName: []HostName{ |
| 41 | + {Name: "example.com", Type: "PTR"}, |
| 42 | + }, |
| 43 | + }, |
| 44 | + Status: HostStatus{State: "up", Reason: "echo-reply"}, |
| 45 | + Port: []Port{ |
| 46 | + {PortID: 22, Protocol: "tcp", State: PortState{State: "open", Reason: "syn-ack"}, Service: PortService{Name: "ssh", Product: "OpenSSH", Version: "8.0"}}, |
| 47 | + {PortID: 80, Protocol: "tcp", State: PortState{State: "open", Reason: "syn-ack"}, Service: PortService{Name: "http", Product: "nginx", Version: "1.18"}}, |
| 48 | + {PortID: 443, Protocol: "tcp", State: PortState{State: "open", Reason: "syn-ack"}, Service: PortService{Name: "https", Product: "nginx", Version: "1.18"}}, |
| 49 | + }, |
| 50 | + OS: OS{ |
| 51 | + OSMatch: []OSMatch{ |
| 52 | + {Name: "Linux 4.15 - 5.6", Accuracy: "95"}, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + StartTime: 1234567890, |
| 58 | + EndTime: 1234567905, |
| 59 | + HostAddress: []HostAddress{ |
| 60 | + {Address: "192.168.1.2", AddressType: "ipv4"}, |
| 61 | + }, |
| 62 | + Status: HostStatus{State: "up", Reason: "echo-reply"}, |
| 63 | + Port: []Port{ |
| 64 | + {PortID: 3306, Protocol: "tcp", State: PortState{State: "open", Reason: "syn-ack"}, Service: PortService{Name: "mysql", Product: "MySQL", Version: "5.7.30"}}, |
| 65 | + {PortID: 8080, Protocol: "tcp", State: PortState{State: "open", Reason: "syn-ack"}, Service: PortService{Name: "http-proxy", Product: "Squid", Version: "4.10"}}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + Verbose: Verbose{Level: 1}, |
| 70 | + Debugging: Debugging{Level: 0}, |
| 71 | + RunStats: RunStats{ |
| 72 | + Finished: Finished{ |
| 73 | + Time: 1234567900, |
| 74 | + TimeStr: "2009-02-13 23:31:40 UTC", |
| 75 | + Elapsed: 10.5, |
| 76 | + Summary: "Nmap done: 2 IP addresses (2 hosts up) scanned in 10.50 seconds", |
| 77 | + Exit: "success", |
| 78 | + }, |
| 79 | + Hosts: StatHosts{Up: 2, Down: 0, Total: 2}, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func BenchmarkJSONFormatter_Default(b *testing.B) { |
| 85 | + nmapRun := createBenchmarkNMAPRun() |
| 86 | + td := &TemplateData{ |
| 87 | + NMAPRun: *nmapRun, |
| 88 | + OutputOptions: OutputOptions{ |
| 89 | + JSONOptions: JSONOutputOptions{ |
| 90 | + PrettyPrint: false, |
| 91 | + SnakeCase: false, |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + b.ResetTimer() |
| 97 | + for i := 0; i < b.N; i++ { |
| 98 | + buf := &closeableBuffer{Buffer: new(bytes.Buffer)} |
| 99 | + formatter := &JSONFormatter{ |
| 100 | + &Config{Writer: buf}, |
| 101 | + } |
| 102 | + if err := formatter.Format(td, ""); err != nil { |
| 103 | + b.Fatal(err) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func BenchmarkJSONFormatter_SnakeCase(b *testing.B) { |
| 109 | + nmapRun := createBenchmarkNMAPRun() |
| 110 | + td := &TemplateData{ |
| 111 | + NMAPRun: *nmapRun, |
| 112 | + OutputOptions: OutputOptions{ |
| 113 | + JSONOptions: JSONOutputOptions{ |
| 114 | + PrettyPrint: false, |
| 115 | + SnakeCase: true, |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + b.ResetTimer() |
| 121 | + for i := 0; i < b.N; i++ { |
| 122 | + buf := &closeableBuffer{Buffer: new(bytes.Buffer)} |
| 123 | + formatter := &JSONFormatter{ |
| 124 | + &Config{Writer: buf}, |
| 125 | + } |
| 126 | + if err := formatter.Format(td, ""); err != nil { |
| 127 | + b.Fatal(err) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func BenchmarkJSONFormatter_PrettyPrint(b *testing.B) { |
| 133 | + nmapRun := createBenchmarkNMAPRun() |
| 134 | + td := &TemplateData{ |
| 135 | + NMAPRun: *nmapRun, |
| 136 | + OutputOptions: OutputOptions{ |
| 137 | + JSONOptions: JSONOutputOptions{ |
| 138 | + PrettyPrint: true, |
| 139 | + SnakeCase: false, |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + b.ResetTimer() |
| 145 | + for i := 0; i < b.N; i++ { |
| 146 | + buf := &closeableBuffer{Buffer: new(bytes.Buffer)} |
| 147 | + formatter := &JSONFormatter{ |
| 148 | + &Config{Writer: buf}, |
| 149 | + } |
| 150 | + if err := formatter.Format(td, ""); err != nil { |
| 151 | + b.Fatal(err) |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func BenchmarkJSONFormatter_SnakeCasePretty(b *testing.B) { |
| 157 | + nmapRun := createBenchmarkNMAPRun() |
| 158 | + td := &TemplateData{ |
| 159 | + NMAPRun: *nmapRun, |
| 160 | + OutputOptions: OutputOptions{ |
| 161 | + JSONOptions: JSONOutputOptions{ |
| 162 | + PrettyPrint: true, |
| 163 | + SnakeCase: true, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + b.ResetTimer() |
| 169 | + for i := 0; i < b.N; i++ { |
| 170 | + buf := &closeableBuffer{Buffer: new(bytes.Buffer)} |
| 171 | + formatter := &JSONFormatter{ |
| 172 | + &Config{Writer: buf}, |
| 173 | + } |
| 174 | + if err := formatter.Format(td, ""); err != nil { |
| 175 | + b.Fatal(err) |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func BenchmarkToSnakeCase(b *testing.B) { |
| 181 | + testCases := []string{ |
| 182 | + "CamelCase", |
| 183 | + "StartStr", |
| 184 | + "NumServices", |
| 185 | + "HTTPServer", |
| 186 | + "HostAddress", |
| 187 | + "AddressType", |
| 188 | + } |
| 189 | + |
| 190 | + b.ResetTimer() |
| 191 | + for i := 0; i < b.N; i++ { |
| 192 | + for _, tc := range testCases { |
| 193 | + _ = toSnakeCase(tc) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +func BenchmarkConvertKeysToSnakeCase(b *testing.B) { |
| 199 | + // Sample JSON data |
| 200 | + jsonData := []byte(`{"Scanner":"nmap","Args":"-sV","Start":1234567890,"StartStr":"2009-02-13 23:31:30 UTC","Version":"7.80","ScanInfo":{"Type":"syn","Protocol":"tcp","NumServices":1000},"Host":[{"StartTime":1234567890,"EndTime":1234567900,"HostAddress":[{"Address":"192.168.1.1","AddressType":"ipv4"}],"Status":{"State":"up","Reason":"echo-reply"}}]}`) |
| 201 | + |
| 202 | + b.ResetTimer() |
| 203 | + for i := 0; i < b.N; i++ { |
| 204 | + _ = convertKeysToSnakeCase(jsonData) |
| 205 | + } |
| 206 | +} |
0 commit comments