2025-02-24 18:22:57 +07:00
|
|
|
package main_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2025-06-25 00:03:44 +07:00
|
|
|
"os"
|
2025-02-24 18:22:57 +07:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/gologger/levels"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/internal/runner"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-25 00:03:44 +07:00
|
|
|
var (
|
|
|
|
|
projectPath string
|
|
|
|
|
targetURL string
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
|
// Set up
|
|
|
|
|
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
|
2025-07-01 00:40:44 +07:00
|
|
|
_ = os.Setenv("DISABLE_STDOUT", "true")
|
2025-06-25 00:03:44 +07:00
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
projectPath, err = os.MkdirTemp("", "nuclei-benchmark-")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 18:22:57 +07:00
|
|
|
dummyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}))
|
2025-06-25 00:03:44 +07:00
|
|
|
targetURL = dummyServer.URL
|
|
|
|
|
|
|
|
|
|
// Execute tests
|
|
|
|
|
|
|
|
|
|
exitCode := m.Run()
|
|
|
|
|
|
|
|
|
|
// Tear down
|
|
|
|
|
|
|
|
|
|
dummyServer.Close()
|
|
|
|
|
_ = os.RemoveAll(projectPath)
|
2025-07-01 00:40:44 +07:00
|
|
|
_ = os.Unsetenv("DISABLE_STDOUT")
|
2025-06-25 00:03:44 +07:00
|
|
|
|
|
|
|
|
os.Exit(exitCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getDefaultOptions() *types.Options {
|
|
|
|
|
return &types.Options{
|
|
|
|
|
RemoteTemplateDomainList: []string{"cloud.projectdiscovery.io"},
|
|
|
|
|
ProjectPath: projectPath,
|
2025-02-24 18:22:57 +07:00
|
|
|
StatsInterval: 5,
|
|
|
|
|
MetricsPort: 9092,
|
|
|
|
|
MaxHostError: 30,
|
|
|
|
|
NoHostErrors: true,
|
|
|
|
|
BulkSize: 25,
|
|
|
|
|
TemplateThreads: 25,
|
|
|
|
|
HeadlessBulkSize: 10,
|
|
|
|
|
HeadlessTemplateThreads: 10,
|
|
|
|
|
Timeout: 10,
|
|
|
|
|
Retries: 1,
|
|
|
|
|
RateLimit: 150,
|
|
|
|
|
RateLimitDuration: time.Duration(time.Second),
|
|
|
|
|
RateLimitMinute: 0,
|
|
|
|
|
PageTimeout: 20,
|
|
|
|
|
InteractionsCacheSize: 5000,
|
|
|
|
|
InteractionsPollDuration: 5,
|
|
|
|
|
InteractionsEviction: 60,
|
|
|
|
|
InteractionsCoolDownPeriod: 5,
|
|
|
|
|
MaxRedirects: 10,
|
|
|
|
|
Silent: true,
|
|
|
|
|
VarDumpLimit: 255,
|
|
|
|
|
JSONRequests: true,
|
|
|
|
|
StoreResponseDir: "output",
|
|
|
|
|
InputFileMode: "list",
|
|
|
|
|
ResponseReadSize: 0,
|
|
|
|
|
ResponseSaveSize: 1048576,
|
|
|
|
|
InputReadTimeout: time.Duration(3 * time.Minute),
|
|
|
|
|
UncoverField: "ip:port",
|
|
|
|
|
UncoverLimit: 100,
|
|
|
|
|
UncoverRateLimit: 60,
|
|
|
|
|
ScanStrategy: "auto",
|
|
|
|
|
FuzzAggressionLevel: "low",
|
|
|
|
|
FuzzParamFrequency: 10,
|
|
|
|
|
TeamID: "none",
|
|
|
|
|
JsConcurrency: 120,
|
|
|
|
|
PayloadConcurrency: 25,
|
|
|
|
|
ProbeConcurrency: 50,
|
|
|
|
|
LoadHelperFileFunction: types.DefaultOptions().LoadHelperFileFunction,
|
|
|
|
|
// DialerKeepAlive: time.Duration(0),
|
|
|
|
|
// DASTServerAddress: "localhost:9055",
|
2025-07-09 14:47:26 -05:00
|
|
|
ExecutionId: "test",
|
|
|
|
|
Logger: gologger.DefaultLogger,
|
2025-02-24 18:22:57 +07:00
|
|
|
}
|
2025-06-25 00:03:44 +07:00
|
|
|
}
|
2025-02-24 18:22:57 +07:00
|
|
|
|
2025-06-25 00:03:44 +07:00
|
|
|
func runEnumBenchmark(b *testing.B, options *types.Options) {
|
2025-02-24 18:22:57 +07:00
|
|
|
runner.ParseOptions(options)
|
|
|
|
|
|
|
|
|
|
nucleiRunner, err := runner.New(options)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatalf("failed to create runner: %s", err)
|
|
|
|
|
}
|
2025-06-25 00:03:44 +07:00
|
|
|
defer nucleiRunner.Close()
|
2025-02-24 18:22:57 +07:00
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2025-06-25 00:03:44 +07:00
|
|
|
b.ReportAllocs()
|
2025-02-24 18:22:57 +07:00
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
if err := nucleiRunner.RunEnumeration(); err != nil {
|
2025-06-25 00:03:44 +07:00
|
|
|
b.Fatalf("%s failed: %s", b.Name(), err)
|
2025-02-24 18:22:57 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-25 00:03:44 +07:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
})
|
|
|
|
|
}
|