예제 #1
0
  def testSummarizeTemplateBadStacking(self):
    """Tests summarizing a template ad that doesn't stack correctly."""
    # A variety of invalid bounds, each item in the list is a 2 item list, one
    # for each ad.
    bad_bounds_list = [
        # Overlapping vertically.
        [{'left': 1, 'right': 15, 'top': 15, 'bottom': 0},
         {'left': 1, 'right': 15, 'top': 30, 'bottom': 14}],
        # Overlapping horizontally.
        [{'left': 1, 'right': 15, 'top': 15, 'bottom': 0},
         {'left': 14, 'right': 25, 'top': 15, 'bottom': 0}],
        # Overlapping both vertically and  horizontally.
        [{'left': 1, 'right': 15, 'top': 15, 'bottom': 0},
         {'left': 14, 'right': 25, 'top': 25, 'bottom': 10}],
    ]

    for bounds in bad_bounds_list:
      bid_response, record = self.CreateSuccessfulTemplateRecord()
      for i, ad_bounds in enumerate(bounds):
        bid_response.ad[0].template_parameter[i].left = ad_bounds['left']
        bid_response.ad[0].template_parameter[i].right = ad_bounds['right']
        bid_response.ad[0].template_parameter[i].top = ad_bounds['top']
        bid_response.ad[0].template_parameter[i].bottom = ad_bounds['bottom']

      record.payload = bid_response.SerializeToString()
      self.records = [record]
      self.summarizer = log.LogSummarizer(self.records)
      self.summarizer.Summarize()
      self.CheckNProblematicResponses(1)
      self.assertTrue(record in self.summarizer._problematic)
      # For some cases we have a stacking error.
      self.assertEqual(1, len(record.problems))
예제 #2
0
def PrintSummary(logger):
    """
    @note: 打印统计的信息
    @param: 日志统计的对象 
    """
    logger.Done()
    summarizer = log.LogSummarizer(logger)
    summarizer.Summarize()
    timestamp = str(datetime.datetime.now())
    timestamp = timestamp.replace(' ', '-', timestamp.count(' '))
    timestamp = timestamp.replace(':', '', timestamp.count(':'))
    good_log_filename = GOOD_LOG_TEMPLATE % timestamp
    good_log = open(good_log_filename, 'w')
    problematic_log_filename = PROBLEMATIC_LOG_TEMPLATE % timestamp
    problematic_log = open(problematic_log_filename, 'w')
    invalid_log_filename = INVALID_LOG_TEMPLATE % timestamp
    invalid_log = open(invalid_log_filename, 'w')
    error_log_filename = ERROR_LOG_TEMPLATE % (timestamp)
    error_log = open(error_log_filename, 'w')
    summarizer.WriteLogFiles(good_log, problematic_log, invalid_log, error_log)
    good_log.close()
    problematic_log.close()
    invalid_log.close()
    error_log.close()
    summarizer.PrintReport()

    # Cleanup by deleting empty files.
    for file_name in [
            good_log_filename, problematic_log_filename, invalid_log_filename,
            error_log_filename
    ]:
        if not os.path.getsize(file_name):
            os.remove(file_name)
예제 #3
0
  def testSummarizeTemplateBadBoundsBounds(self):
    """Tests summarizing a template ad with a missing left bound."""
    # A variety of invalid bounds.
    bad_bounds_list = [
        # Left < 0.
        {'left': -1, 'right': 15, 'top': 15, 'bottom': 0},
        # Right > slot width.
        {'left': 1, 'right': 100, 'top': 15, 'bottom': 0},
        # Bottom < 0.
        {'left': 1, 'right': 15, 'top': 15, 'bottom': -1},
        # Top > height..
        {'left': 1, 'right': 15, 'top': 600, 'bottom': 0},
        # Width < 10.
        {'left': 1, 'right': 5, 'top': 15, 'bottom': 1},
        {'left': 1, 'right': 10, 'top': 15, 'bottom': 1},
        # Height < 10
        {'left': 1, 'right': 15, 'top': 5, 'bottom': 1},
        {'left': 1, 'right': 15, 'top': 10, 'bottom': 1},
    ]

    for bounds in bad_bounds_list:
      bid_response, record = self.CreateSuccessfulTemplateRecord()
      bid_response.ad[0].template_parameter[0].left = bounds['left']
      bid_response.ad[0].template_parameter[0].right = bounds['right']
      bid_response.ad[0].template_parameter[0].top = bounds['top']
      bid_response.ad[0].template_parameter[0].bottom = bounds['bottom']

      record.payload = bid_response.SerializeToString()
      self.records = [record]
      self.summarizer = log.LogSummarizer(self.records)
      self.summarizer.Summarize()
      self.CheckNProblematicResponses(1)
      self.assertTrue(record in self.summarizer._problematic)
      # For some cases we have a stacking error.
      self.assertTrue(len(record.problems) in [1, 2])
예제 #4
0
 def testValidateHtmlSnippetSubstitutions(self):
   """Tests that ValidateHtmlSnippet does correct macro substitutions."""
   snippet = ('%%CLICK_URL_UNESC%%\n'
              '%%CLICK_URL_ESC%%\n'
              '%%SITE%%\n'
              '%%CACHEBUSTER%%\n'
              '%%WINNING_PRICE%%\n'
              '%%WINNING_PRICE_ESC%%\n')
   bid_response, record = self.CreateSuccessfulRecord()
   record.bid_request.url = 'http://some.url.com/page?q=v'
   bid_response.ad[0].html_snippet = snippet
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.ValidateHtmlSnippet(bid_response.ad[0], 0,  # ad index
                                       record)
   self.assertEqual(0, len(record.problems))
   self.assertTrue(0 in record.html_snippets)
   substituted_snippet = record.html_snippets[0]
   pattern_str = '%s\n%s\n%s\n%s\n%s\n%s\n' % (
       re.escape(log.LogSummarizer.CLICK_URL_UNESC),
       re.escape(log.LogSummarizer.CLICK_URL_ESC),
       re.escape('some.url.com'),  # The domain we set in the request.
       '\d+',  # Cachebuster.
       '[\d\.]+',  # Winning price.
       '[\d\.]+',  # Winning price.
   )
   pattern_obj = re.compile(pattern_str, re.MULTILINE)
   self.assertFalse(pattern_obj.match(substituted_snippet) is None)
예제 #5
0
 def testSummarizeWithGoodVideoAdRecord(self):
   """Tests summarizing with one good record."""
   _, record = self.CreateSuccessfulVideoRecord()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNGoodRequests(1)
   self.assertTrue(record in self.summarizer._good)
   self.assertEqual(0, len(record.problems))
예제 #6
0
 def testSummarizeOneGoodTemplateAd(self):
   """Tests summarizing a response with one template ad."""
   _, record = self.CreateSuccessfulTemplateRecord()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNGoodRequests(1)
   self.assertTrue(record in self.summarizer._good)
   self.assertEqual(0, len(record.problems))
예제 #7
0
 def testSummarizeWithoutAds(self):
   """Tests that lack of ads is considered a valid response."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ClearField('ad')
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNGoodRequests(1, no_bids=1)
   self.assertTrue(record in self.summarizer._good)
예제 #8
0
 def testSummarizeUnparseableResonse(self):
   """Tests summarizing with multiple good records."""
   _, record = self.CreateSuccessfulRecord()
   record.payload = 'garbage'
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNInvalidResponses(1)
   self.assertTrue(record in self.summarizer._invalid)
   self.assertEqual(1, len(record.problems))
예제 #9
0
 def testSummarizeTemplateWithNonIntParameters(self):
   """Tests summarizing a template ad with non-integer parameters."""
   bid_response, record = self.CreateSuccessfulTemplateRecord()
   bid_response.ad[0].snippet_template = 'Bad %%Px%% and %%Py%% params!'
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #10
0
 def testSummarizeInvalidAdSlot(self):
   """Tests summarizing an adslot with an invalid id."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ad[0].adslot[0].id = record.bid_request.adslot[0].id + 1
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #11
0
 def testSummarizeZeroMinCpm(self):
   """Tests summarizing an adslot with zero min CPM."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ad[0].adslot[0].min_cpm_micros = 0
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #12
0
 def testSummarizeManyGoodRecords(self):
   """Tests summarizing with multiple good records."""
   for _ in range(10):
     _, record = self.CreateSuccessfulRecord()
     self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNGoodRequests(10)
   for record in self.records:
     self.assertTrue(record in self.summarizer._good)
     self.assertEqual(0, len(record.problems))
예제 #13
0
 def testSummarizeWithoutProcessingTime(self):
   """Tests that lack of processing time generates an error message."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ClearField('processing_time_ms')
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #14
0
 def testSummarizeTemplateWithBuyerCreativeIdInAd(self):
   """Tests summarizing a template ad a buyer_creative_id in the parent."""
   bid_response, record = self.CreateSuccessfulTemplateRecord()
   bid_response.ad[0].buyer_creative_id = '123'
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #15
0
 def testSummarizeHasUnescapedClickMacro(self):
   """Tests summarizing when a snippet has the unescaped click url macro."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ad[0].html_snippet = '%%CLICK_URL_UNESC%%'
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNGoodRequests(1)
   self.assertTrue(record in self.summarizer._good)
   self.assertEqual(0, len(record.problems))
예제 #16
0
 def testSummarizeMissingClickMacros(self):
   """Tests summarizing an ad with a snippet missing the click url macros."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ad[0].html_snippet = 'No click url macro.'
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #17
0
 def testSummarizeEmptySnippet(self):
   """Tests summarizing an ad with an empty HTML snippet."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ad[0].html_snippet = ""
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #18
0
 def testSummarizeInvalidVideoUrl(self):
   """Tests summarizing an invalid video url."""
   bid_response, record = self.CreateSuccessfulVideoRecord()
   bid_response.ad[0].video_url = 'invalid video url'
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #19
0
 def testSummarizeSnippetAndHosted(self):
   """Tests summarizing an ad with both a creative_id and an HTML snippet."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ad[0].creative_id = 12345
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #20
0
 def testSummarizeTemplateWithParameterMismatch(self):
   """Tests summarizing a template ad with mismatched parameters."""
   bid_response, record = self.CreateSuccessfulTemplateRecord()
   bid_response.ad[0].snippet_template = '%%P0%%, %%P1%% and %%P2%%!'
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #21
0
 def testSummarizeTemplateWithMisnumberedParameters(self):
   """Tests summarizing a template ad with non-consecutive parameters."""
   bid_response, record = self.CreateSuccessfulTemplateRecord()
   bid_response.ad[0].snippet_template = 'Two %%P0%% params %%P2%%!'
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #22
0
 def testSummarizeTemplateWithClickThroughUrlInAd(self):
   """Tests summarizing a template ad with landing page url in the parent."""
   bid_response, record = self.CreateSuccessfulTemplateRecord()
   bid_response.ad[0].click_through_url.append('http://url.com')
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #23
0
 def testSummarizeTemplateMissingBuyerCreativeId(self):
   """Tests summarizing a template ad missing a buyer_creative_id."""
   bid_response, record = self.CreateSuccessfulTemplateRecord()
   bid_response.ad[0].template_parameter[0].ClearField('buyer_creative_id')
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #24
0
 def testSummarizeNonOkHttpStatus(self):
   """Tests summarizing a response with a non-200 HTTP status."""
   _, record = self.CreateSuccessfulRecord()
   record.status = 404
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.assertEqual(0, self.summarizer._responses_ok)
   self.assertEqual(1, len(self.summarizer._error))
   self.assertTrue(record in self.summarizer._error)
   self.assertEqual(1, len(record.problems))
예제 #25
0
 def testSummarizeTemplateMissingClickThroughUrl(self):
   """Tests summarizing a template ad missing a landing page url."""
   bid_response, record = self.CreateSuccessfulTemplateRecord()
   bid_response.ad[0].template_parameter[0].ClearField('click_through_url')
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #26
0
 def testSummarizeTemplateAdForVideoRequest(self):
   """Tests summarizing a template ad for a video ad request.."""
   bid_response, record = self.CreateSuccessfulTemplateRecord()
   record.bid_request.video.videoad_start_delay = 1000
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #27
0
 def testSummarizeNoAdSlot(self):
   """Tests summarizing an ad without any ad slots."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ad[0].ClearField('adslot')
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1, no_bids=1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))
예제 #28
0
 def testSummarizeSnippetAndVideo(self):
     """Tests summarizing an ad with both a video url and an HTML snippet."""
     bid_response, record = self.CreateSuccessfulRecord()
     bid_response.ad[0].video_url = "http://video.com"
     record.payload = bid_response.SerializeToString()
     self.records.append(record)
     self.summarizer = log.LogSummarizer(self.records)
     self.summarizer.Summarize()
     self.CheckNProblematicResponses(1)
     self.assertTrue(record in self.summarizer._problematic)
     self.assertEqual(1, len(record.problems))
예제 #29
0
 def testWriteLogsWhileEmpty(self):
   """Tests writing logs with empty record lists."""
   self.SetupLogs()
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.WriteLogFiles(self.good_log, self.problematic_log,
                                 self.invalid_log, self.error_log,
                                 self.snippet_log)
   self.CheckLogHasNLines(self.good_log, 0, exact=True)
   self.CheckLogHasNLines(self.problematic_log, 0, exact=True)
   self.CheckLogHasNLines(self.invalid_log, 0, exact=True)
   self.CheckLogHasNLines(self.error_log, 0, exact=True)
예제 #30
0
 def testSummarizeEmptyClickThroughUrl(self):
   """Tests summarizing an ad with an empty click through url."""
   bid_response, record = self.CreateSuccessfulRecord()
   bid_response.ad[0].click_through_url[0] = ""
   record.payload = bid_response.SerializeToString()
   self.records.append(record)
   self.summarizer = log.LogSummarizer(self.records)
   self.summarizer.Summarize()
   self.CheckNProblematicResponses(1)
   self.assertTrue(record in self.summarizer._problematic)
   self.assertEqual(1, len(record.problems))