Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 62 additions & 14 deletions cmd/nuclei/main_test.go → cmd/nuclei/main_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,56 @@ package main_test
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/projectdiscovery/goflags"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/levels"
"github.com/projectdiscovery/nuclei/v3/internal/runner"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
)

func BenchmarkRunEnumeration(b *testing.B) {
var (
projectPath string
targetURL string
)

func TestMain(m *testing.M) {
// Set up

gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
os.Setenv("DISABLE_STDOUT", "true")

var err error

projectPath, err = os.MkdirTemp("", "nuclei-benchmark-")
if err != nil {
panic(err)
}

dummyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
defer dummyServer.Close()

options := &types.Options{
RemoteTemplateDomainList: goflags.StringSlice{
"cloud.projectdiscovery.io",
},
ProjectPath: "/tmp",
Targets: goflags.StringSlice{dummyServer.URL},
targetURL = dummyServer.URL

// Execute tests

exitCode := m.Run()

// Tear down

dummyServer.Close()
_ = os.RemoveAll(projectPath)
os.Unsetenv("DISABLE_STDOUT")

os.Exit(exitCode)
}

func getDefaultOptions() *types.Options {
return &types.Options{
RemoteTemplateDomainList: []string{"cloud.projectdiscovery.io"},
ProjectPath: projectPath,
StatsInterval: 5,
MetricsPort: 9092,
MaxHostError: 30,
Expand Down Expand Up @@ -66,22 +94,42 @@ func BenchmarkRunEnumeration(b *testing.B) {
// DialerKeepAlive: time.Duration(0),
// DASTServerAddress: "localhost:9055",
}
}

func runEnumBenchmark(b *testing.B, options *types.Options) {
runner.ParseOptions(options)

// Disable logging to reduce benchmark noise.
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)

nucleiRunner, err := runner.New(options)
if err != nil {
b.Fatalf("failed to create runner: %s", err)
}
defer nucleiRunner.Close()

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
if err := nucleiRunner.RunEnumeration(); err != nil {
b.Fatalf("RunEnumeration failed: %s", err)
b.Fatalf("%s failed: %s", b.Name(), err)
}
}
}

func BenchmarkRunEnumeration(b *testing.B) {
// Default case: run enumeration with default options == all nuclei-templates
// b.Run("Default", func(b *testing.B) {
// options := getDefaultOptions()
// options.Targets = []string{targetURL}

// runEnumBenchmark(b, options)
// })

// Case: https://github.com/projectdiscovery/nuclei/pull/6258
b.Run("Multiproto", func(b *testing.B) {
options := getDefaultOptions()
options.Targets = []string{targetURL}
options.Templates = []string{"./cmd/nuclei/testdata/benchmark/multiproto/"}

runEnumBenchmark(b, options)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
id: basic-template-multiproto-mixed

info:
name: Test Template Multiple Protocols (Mixed)
author: pdteam
severity: info

http:
- method: GET
id: first_iter_http
path:
- '{{BaseURL}}/1'

matchers:
- type: word
words:
- "Test is test matcher text"

- method: GET
path:
- '{{BaseURL}}/2'

matchers:
- type: word
words:
- "Test is test matcher text"

- method: GET
path:
- '{{BaseURL}}/3'

matchers:
- type: word
words:
- "Test is test matcher text"

- method: GET
path:
- '{{BaseURL}}/4'

matchers:
- type: word
words:
- "Test is test matcher text"

- method: GET
path:
- '{{BaseURL}}/5'

matchers:
- type: word
words:
- "Test is test matcher text"

- method: GET
path:
- '{{BaseURL}}/6'

matchers:
- type: word
words:
- "Test is test matcher text"

- method: GET
path:
- '{{BaseURL}}/7'

matchers:
- type: word
words:
- "Test is test matcher text"

- method: GET
path:
- '{{BaseURL}}/8'

matchers:
- type: word
words:
- "Test is test matcher text"

- method: GET
path:
- '{{BaseURL}}/9'

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /10 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /11 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /12 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /13 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /14 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET / HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /15 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /16 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /17 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"

- raw:
- |
GET /18 HTTP/1.1
Host: {{Hostname}}
Origin: {{BaseURL}}
Connection: close
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9

matchers:
- type: word
words:
- "Test is test matcher text"
Loading
Loading