github.com/prebid/prebid-server@v0.275.0/endpoints/openrtb2/auction_benchmark_test.go (about)

     1  package openrtb2
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	analyticsConf "github.com/prebid/prebid-server/analytics/config"
    14  	"github.com/prebid/prebid-server/config"
    15  	"github.com/prebid/prebid-server/currency"
    16  	"github.com/prebid/prebid-server/exchange"
    17  	"github.com/prebid/prebid-server/experiment/adscert"
    18  	"github.com/prebid/prebid-server/hooks"
    19  	"github.com/prebid/prebid-server/macros"
    20  	metricsConfig "github.com/prebid/prebid-server/metrics/config"
    21  	"github.com/prebid/prebid-server/openrtb_ext"
    22  	"github.com/prebid/prebid-server/stored_requests/backends/empty_fetcher"
    23  	"github.com/prebid/prebid-server/usersync"
    24  )
    25  
    26  // benchmarkTestServer returns the header bidding test ad. This response was scraped from a real appnexus server response.
    27  func benchmarkTestServer(w http.ResponseWriter, r *http.Request) {
    28  	w.Write([]byte(`{"id":"some-request-id","seatbid":[{"bid":[{"id":"4625436751433509010","impid":"my-imp-id","price":0.5,"adm":"\u003cscript type=\"application/javascript\" src=\"http://nym1-ib.adnxs.com/ab?e=wqT_3QKABqAAAwAAAwDWAAUBCM-OiNAFELuV09Pqi86EVRj6t-7QyLin_REqLQkAAAECCOA_EQEHNAAA4D8ZAAAAgOtR4D8hERIAKREJoDDy5vwEOL4HQL4HSAJQ1suTDljhgEhgAGiRQHixhQSAAQGKAQNVU0SSBQbwUpgBrAKgAfoBqAEBsAEAuAECwAEDyAEC0AEA2AEA4AEB8AEAigI6dWYoJ2EnLCA0OTQ0NzIsIDE1MTAwODIzODMpO3VmKCdyJywgMjk2ODExMTAsMh4A8JySAvkBIVR6WGNkQWk2MEljRUVOYkxrdzRZQUNEaGdFZ3dBRGdBUUFSSXZnZFE4dWI4QkZnQVlQX19fXzhQYUFCd0FYZ0JnQUVCaUFFQmtBRUJtQUVCb0FFQnFBRURzQUVBdVFFcGk0aURBQURnUDhFQktZdUlnd0FBNERfSkFTZlJKRUdtbi00XzJRRUFBQUFBQUFEd1AtQUJBUFVCBQ8oSmdDQUtBQ0FMVUMFEARMMAkI8ExNQUNBY2dDQWRBQ0FkZ0NBZUFDQU9nQ0FQZ0NBSUFEQVpBREFKZ0RBYWdEdXRDSEJMb0RDVTVaVFRJNk16STNOdy4umgItITh3aENuZzb8ALg0WUJJSUFRb0FEb0pUbGxOTWpvek1qYzPYAugH4ALH0wHyAhAKBkFEVl9JRBIGNCV1HPICEQoGQ1BHARMcBzE5Nzc5MzMBJwgFQ1AFE_B-ODUxMzU5NIADAYgDAZADAJgDFKADAaoDAMADrALIAwDYAwDgAwDoAwD4AwCABACSBAkvb3BlbnJ0YjKYBACoBACyBAwIABAAGAAgADAAOAC4BADABADIBADSBAlOWU0yOjMyNzfaBAIIAeAEAPAE1suTDogFAZgFAKAF_____wUDXAGqBQ9zb21lLXJlcXVlc3QtaWTABQDJBUmbTPA_0gUJCQAAAAAAAAAA2AUB4AUB\u0026s=61dc0e8770543def5a3a77b4589830d1274b26f1\u0026test=1\u0026pp=${AUCTION_PRICE}\u0026\"\u003e\u003c/script\u003e","adid":"29681110","adomain":["appnexus.com"],"iurl":"http://nym1-ib.adnxs.com/cr?id=29681110","cid":"958","crid":"29681110","w":300,"h":250,"ext":{"bidder":{"appnexus":{"brand_id":1,"auction_id":6127490747252132539,"bidder_id":2}}}}],"seat":"appnexus"}],"ext":{"debug":{"httpcalls":{"appnexus":[{"uri":"http://ib.adnxs.com/openrtb2","requestbody":"{\"id\":\"some-request-id\",\"imp\":[{\"id\":\"my-imp-id\",\"banner\":{\"format\":[{\"w\":300,\"h\":250},{\"w\":300,\"h\":600}]},\"ext\":{\"appnexus\":{\"placement_id\":12883451}}}],\"test\":1,\"tmax\":500}","responsebody":"{\"id\":\"some-request-id\",\"seatbid\":[{\"bid\":[{\"id\":\"4625436751433509010\",\"impid\":\"my-imp-id\",\"price\": 0.500000,\"adid\":\"29681110\",\"adm\":\"\u003cscript type=\\\"application/javascript\\\" src=\\\"http://nym1-ib.adnxs.com/ab?e=wqT_3QKABqAAAwAAAwDWAAUBCM-OiNAFELuV09Pqi86EVRj6t-7QyLin_REqLQkAAAECCOA_EQEHNAAA4D8ZAAAAgOtR4D8hERIAKREJoDDy5vwEOL4HQL4HSAJQ1suTDljhgEhgAGiRQHixhQSAAQGKAQNVU0SSBQbwUpgBrAKgAfoBqAEBsAEAuAECwAEDyAEC0AEA2AEA4AEB8AEAigI6dWYoJ2EnLCA0OTQ0NzIsIDE1MTAwODIzODMpO3VmKCdyJywgMjk2ODExMTAsMh4A8JySAvkBIVR6WGNkQWk2MEljRUVOYkxrdzRZQUNEaGdFZ3dBRGdBUUFSSXZnZFE4dWI4QkZnQVlQX19fXzhQYUFCd0FYZ0JnQUVCaUFFQmtBRUJtQUVCb0FFQnFBRURzQUVBdVFFcGk0aURBQURnUDhFQktZdUlnd0FBNERfSkFTZlJKRUdtbi00XzJRRUFBQUFBQUFEd1AtQUJBUFVCBQ8oSmdDQUtBQ0FMVUMFEARMMAkI8ExNQUNBY2dDQWRBQ0FkZ0NBZUFDQU9nQ0FQZ0NBSUFEQVpBREFKZ0RBYWdEdXRDSEJMb0RDVTVaVFRJNk16STNOdy4umgItITh3aENuZzb8ALg0WUJJSUFRb0FEb0pUbGxOTWpvek1qYzPYAugH4ALH0wHyAhAKBkFEVl9JRBIGNCV1HPICEQoGQ1BHARMcBzE5Nzc5MzMBJwgFQ1AFE_B-ODUxMzU5NIADAYgDAZADAJgDFKADAaoDAMADrALIAwDYAwDgAwDoAwD4AwCABACSBAkvb3BlbnJ0YjKYBACoBACyBAwIABAAGAAgADAAOAC4BADABADIBADSBAlOWU0yOjMyNzfaBAIIAeAEAPAE1suTDogFAZgFAKAF_____wUDXAGqBQ9zb21lLXJlcXVlc3QtaWTABQDJBUmbTPA_0gUJCQAAAAAAAAAA2AUB4AUB\u0026s=61dc0e8770543def5a3a77b4589830d1274b26f1\u0026test=1\u0026pp=${AUCTION_PRICE}\u0026\\\"\u003e\u003c/script\u003e\",\"adomain\":[\"appnexus.com\"],\"iurl\":\"http://nym1-ib.adnxs.com/cr?id=29681110\",\"cid\":\"958\",\"crid\":\"29681110\",\"h\": 250,\"w\": 300,\"ext\":{\"appnexus\":{\"brand_id\": 1,\"auction_id\": 6127490747252132539,\"bidder_id\": 2}}}],\"seat\":\"958\"}],\"bidid\":\"8271358638249766712\",\"cur\":\"USD\"}","status":200}]}},"responsetimemillis":{"appnexus":42}}}`))
    29  }
    30  
    31  // benchmarkBuildTestRequest returns a request which fetches the header bidding test ad.
    32  func benchmarkBuildTestRequest() *http.Request {
    33  	request, _ := http.NewRequest("POST", "/openrtb2/auction", strings.NewReader(`{
    34    "id": "some-request-id",
    35    "imp": [
    36      {
    37        "id": "my-imp-id",
    38        "banner": {
    39      	"format": [
    40      	  {
    41      	    "w": 300,
    42      	    "h": 250
    43      	  },
    44      	  {
    45      	    "w": 300,
    46      	    "h": 600
    47      	  }
    48      	]
    49        },
    50        "ext": {
    51          "appnexus": {
    52            "placementId": 12883451
    53          }
    54        }
    55      }
    56    ],
    57    "test": 1,
    58    "tmax": 500
    59  }`))
    60  	return request
    61  }
    62  
    63  // BenchmarkOpenrtbEndpoint measures the performance of the endpoint, mocking out the external server dependency.
    64  func BenchmarkOpenrtbEndpoint(b *testing.B) {
    65  	server := httptest.NewServer(http.HandlerFunc(benchmarkTestServer))
    66  	defer server.Close()
    67  
    68  	var infos = make(config.BidderInfos, 0)
    69  	infos["appnexus"] = config.BidderInfo{Capabilities: &config.CapabilitiesInfo{Site: &config.PlatformInfo{MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeBanner}}}}
    70  	paramValidator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
    71  	if err != nil {
    72  		return
    73  	}
    74  
    75  	nilMetrics := &metricsConfig.NilMetricsEngine{}
    76  
    77  	adapters, adaptersErr := exchange.BuildAdapters(server.Client(), &config.Configuration{}, infos, nilMetrics)
    78  	if adaptersErr != nil {
    79  		b.Fatal("unable to build adapters")
    80  	}
    81  
    82  	gdprPermsBuilder := fakePermissionsBuilder{
    83  		permissions: &fakePermissions{},
    84  	}.Builder
    85  
    86  	exchange := exchange.NewExchange(
    87  		adapters,
    88  		nil,
    89  		&config.Configuration{},
    90  		map[string]usersync.Syncer{},
    91  		nilMetrics,
    92  		infos,
    93  		gdprPermsBuilder,
    94  		currency.NewRateConverter(&http.Client{}, "", time.Duration(0)),
    95  		empty_fetcher.EmptyFetcher{},
    96  		&adscert.NilSigner{},
    97  		macros.NewStringIndexBasedReplacer(),
    98  	)
    99  
   100  	endpoint, _ := NewEndpoint(
   101  		fakeUUIDGenerator{},
   102  		exchange,
   103  		paramValidator,
   104  		empty_fetcher.EmptyFetcher{},
   105  		empty_fetcher.EmptyFetcher{},
   106  		&config.Configuration{MaxRequestSize: maxSize},
   107  		nilMetrics,
   108  		analyticsConf.NewPBSAnalytics(&config.Analytics{}),
   109  		map[string]string{},
   110  		[]byte{},
   111  		nil,
   112  		empty_fetcher.EmptyFetcher{},
   113  		hooks.EmptyPlanBuilder{},
   114  		nil,
   115  	)
   116  
   117  	b.ResetTimer()
   118  	for n := 0; n < b.N; n++ {
   119  		endpoint(httptest.NewRecorder(), benchmarkBuildTestRequest(), nil)
   120  	}
   121  }
   122  
   123  // BenchmarkValidWholeExemplary benchmarks the process that results of hitting the `openrtb2/auction` with
   124  // the different JSON bid requests found in the `sample-requests/valid-whole/exemplary/` directory. As
   125  // especified in said file, we expect this bid request to succeed with a 200 status code.
   126  func BenchmarkValidWholeExemplary(b *testing.B) {
   127  	var benchInput = []string{
   128  		"sample-requests/valid-whole/exemplary/all-ext.json",
   129  		"sample-requests/valid-whole/exemplary/interstitial-no-size.json",
   130  		"sample-requests/valid-whole/exemplary/prebid-test-ad.json",
   131  		"sample-requests/valid-whole/exemplary/simple.json",
   132  		"sample-requests/valid-whole/exemplary/skadn.json",
   133  	}
   134  
   135  	for _, testFile := range benchInput {
   136  		b.Run(fmt.Sprintf("input_file_%s", testFile), func(b *testing.B) {
   137  			b.StopTimer()
   138  			// Set up
   139  			fileData, err := os.ReadFile(testFile)
   140  			if err != nil {
   141  				b.Fatalf("unable to read file %s", testFile)
   142  			}
   143  			test, err := parseTestData(fileData, testFile)
   144  			if err != nil {
   145  				b.Fatal(err.Error())
   146  			}
   147  			test.endpointType = OPENRTB_ENDPOINT
   148  
   149  			cfg := &config.Configuration{
   150  				MaxRequestSize:     maxSize,
   151  				BlacklistedApps:    test.Config.BlacklistedApps,
   152  				BlacklistedAppMap:  test.Config.getBlacklistedAppMap(),
   153  				BlacklistedAccts:   test.Config.BlacklistedAccounts,
   154  				BlacklistedAcctMap: test.Config.getBlackListedAccountMap(),
   155  				AccountRequired:    test.Config.AccountRequired,
   156  			}
   157  
   158  			auctionEndpointHandler, _, mockBidServers, mockCurrencyRatesServer, err := buildTestEndpoint(test, cfg)
   159  			if err != nil {
   160  				b.Fatal(err.Error())
   161  			}
   162  			request := httptest.NewRequest("POST", "/openrtb2/auction", bytes.NewReader(test.BidRequest))
   163  			recorder := httptest.NewRecorder()
   164  
   165  			// Run benchmark
   166  			b.ResetTimer()
   167  			for n := 0; n < b.N; n++ {
   168  				b.StartTimer()
   169  				auctionEndpointHandler(recorder, request, nil) //Request comes from the unmarshalled mockBidRequest
   170  				b.StopTimer()
   171  			}
   172  			for _, mockBidServer := range mockBidServers {
   173  				mockBidServer.Close()
   174  			}
   175  			mockCurrencyRatesServer.Close()
   176  		})
   177  	}
   178  }