Cukes C0 Coverage Information - Simploco - RCov

library/generic/performance_report.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
library/generic/performance_report.rb 518 255
54.44%
7.45%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

2 =begin
3 *Name           : CustomHtmlReport
4 *Description    : class that holds performance report data generator methods
5 *Author         : Chandra sekaran
6 *Creation Date  : 05/03/2015
7 *Updation Date  :
8 =end
9 
10 module CUKES
11   class PerformanceReport
12 
13     # Description       : invoked automatically when an object of the class type is created
14     # Author            : Chandra sekaran
15     # Arguments         :
16     #   arr_file_paths  : array of html report file paths
17     #
18     def initialize(arr_file_path)
19       @arr_file_name = arr_file_path
20       @num_build_duration = 0
21       @bool_build_passed = true
22       @num_feature_count = 0
23       @num_feature_pass_count = 0
24       @num_feature_fail_count = 0
25       @num_feature_skip_count = 0
26       @str_connection_url = "DBI:SQLAnywhere:SERVER=#{DB_SERVER};DBN=#{DB_NAME}"
27     end
28 
29     # Description       : parses the json object saves the required execution data into Sybase DB
30     # Author            : Chandra sekaran
31     #
32     def parse_json
33       @json.each_with_index do |json, index|         # iterate each feature
34         str_feature_id, num_feature_result_id = set_feature(json["name"].to_s)      # add feature name
35         @num_feature_count += 1
36         scenario_count = 0
37         feature_duration = 0
38         bool_feature_passed = true
39         num_scenario_pass_count = 0
40         num_scenario_fail_count = 0
41         num_scenario_skip_count = 0
42 
43         json["elements"].each do |element|
44           # for including the first background steps duration to the first scenario background steps as Cucumber json
45           # report considers background for the first scenario and hence will have 0 duration for the background steps
46           # under first scenario and the actual duration for the background will be set for the subsequent scenarios
47           if index == 0
48             if element["keyword"] == "Background"
49               element["steps"].each do |step|
50                 @arr_background_step_duration << step["result"]["duration"].to_i
51                 @bool = true
52               end
53             end
54           end
55           num_step_pass_count = 0
56           num_step_fail_count = 0
57           num_step_skip_count = 0
58 
59           if element["keyword"] == "Scenario"       # take scenario for scenario level details
60             str_scenario_id, scenario_result_id = set_scenario(element["name"], element["tags"][0]["name"], str_feature_id)     # add scenario name
61 
62             scenario_count += 1  # as it counts only 'Scenarios' and not 'Backgrounds'
63             step_count = 0
64             scenario_duration = 0
65             bool_scenario_passed = true
66 
67             element["steps"].each_with_index do |step, indx|    # iterate each steps of the current scenario
68               #num_step_id, num_step_result_id = set_step(step['keyword']+step['name'], str_scenario_id, scenario_result_id)    # add step name
69               num_step_id = set_step(step['keyword']+step['name'], str_scenario_id, scenario_result_id)    # add step name
70 
71               step_count += 1
72               step_duration = 0
73               bool_step_passed = true
74               if (index == 0) && (indx < @arr_background_step_duration.size) && @bool
75                 step_duration = @arr_background_step_duration[indx]    # take duration from Background for the first scenario
76               else
77                 step_duration = step['result']['duration'].to_i     # take usual duration
78                 @bool = false
79               end
80               scenario_duration += step_duration
81               if step['result']['status'] == "passed"
82                 num_step_pass_count += 1
83               elsif step['result']['status'] == "failed"
84                 num_step_fail_count += 1
85               elsif step['result']['status'] == "skipped"
86                 num_step_skip_count += 1
87               end
88               bool_step_passed = ["passed", "pending"].include?(step['result']['status']) ? true : false
89               bool_scenario_passed &&= bool_step_passed
90                                                                                                                                #puts "\t\t Step : #{step['keyword']+step['name']} - #{step_duration} - #{bool_step_passed}"
91               #reset_step_result(bool_step_passed, step_duration, num_step_result_id)
92               set_step_result_new(num_step_id, str_scenario_id, scenario_result_id, bool_step_passed, step_duration)
93             end
94                                                                                                                                 #puts "Scenario : #{element["tags"][0]["name"]} - #{element['name']} - #{scenario_duration} - #{bool_scenario_passed} - #{step_count}"
95             reset_scenario_result(scenario_result_id, scenario_duration, num_step_pass_count, num_step_fail_count, num_step_skip_count, bool_scenario_passed)
96             feature_duration += scenario_duration
97             bool_feature_passed &&= bool_scenario_passed
98 
99             if bool_scenario_passed
100               num_scenario_pass_count += 1    # scenario pass count
101             else
102               if num_step_pass_count == 0 && num_step_fail_count == 0 && num_step_skip_count > 0
103                 num_scenario_skip_count += 1   # scenario skip count
104               else
105                 num_scenario_fail_count += 1   # scenario fail count
106               end
107             end
108 
109           end
110         end
111                                                                                     #puts "Feature : #{json['name']} - #{feature_duration} - #{bool_feature_passed} - #{scenario_count}"
112         reset_feature_result(feature_duration, num_scenario_pass_count, num_scenario_fail_count, num_scenario_skip_count, bool_feature_passed, num_feature_result_id)
113         @num_build_duration += feature_duration
114         @bool_build_passed &&= bool_feature_passed
115 
116         if bool_feature_passed
117           @num_feature_pass_count += 1
118         else
119           @num_feature_fail_count += 1    # to do feature skip count
120         end
121       end
122     rescue Exception => ex
123       $log.error("Error while parsing JSON : #{ex}")
124       exit
125     end
126 
127     # Description       : sets the build data with default details into Sybase
128     # Author            : Chandra sekaran
129     #
130     def set_build
131       num_host_id = get_host_data
132       build_name = Time.now.strftime("%d_%m_%Y-%H_%M_%S")
133       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
134         str_query = "insert into BuildData(BuildName,HostDataID) values (?,?)"
135         sth = dbh.prepare(str_query)
136         sth.execute(build_name, num_host_id)
137         sth.finish
138         dbh.commit
139         #puts "Added a record to BuildData table successfully"
140 
141         str_query = "select BuildID from BuildData where BuildName='#{build_name}' and HostDataID=#{num_host_id}"
142         sth = dbh.prepare(str_query)
143         sth.execute()
144         @build_id  = sth.fetch[0]
145         dbh.disconnect()
146       end
147     rescue Exception => ex
148       $log.error("Error in setting build data to BuildData table: #{ex}")
149       exit
150     end
151 
152     # Description       : gets the host data from Sybase based on current execution host
153     # Author            : Chandra sekaran
154     # Return Arguments  :
155     #     num_host_id   : primary key the host
156     #
157     def get_host_data
158       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
159         str_query = "select HostDataID from HostData where HostName like '#{ENV['COMPUTERNAME'].downcase}' and OS like '#{ENV['OS'].downcase}' and Browser like '#{BROWSER.downcase}'"
160         sth = dbh.prepare(str_query)
161         sth.execute()
162         num_host_id  = sth.fetch[0]
163         dbh.disconnect()
164         $log.info("------------host id : #{num_host_id.nil?}")
165         num_host_id.nil? ? 5 : num_host_id
166       end
167     rescue Exception => ex
168       $log.error("Error in getting host data from HostData table: #{ex}")
169       exit
170     end
171 
172     # Description       : resets the build data with execution details into Sybase
173     # Author            : Chandra sekaran
174     # Arguments         :
175     #    num_run_length : total execution time in nanoseconds
176     #    num_pass_count : number of features passed
177     #    num_fail_count : number of features failed
178     #    num_skip_count : number of features skipped
179     #    bool_result    : boolean value resembling the state of build result
180     #
181     def reset_build(num_run_length, num_pass_count, num_fail_count, num_skip_count, bool_result)
182       num_result =  bool_result ? 1 : 0
183       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
184         sth = dbh.prepare("update BuildData set RunLength=?, Passes=?, Failures=?, Skips=?, Result=? where BuildID=?")
185         sth.execute(convert_duration(num_run_length), num_pass_count, num_fail_count, num_skip_count, num_result, @build_id)
186         sth.finish
187         dbh.commit
188         #puts "Updated a record (#{@build_id}) in BuildData table successfully"
189         dbh.disconnect()
190       end
191     rescue Exception => ex
192       $log.error("Error in resetting build data to BuildData table: #{ex}")
193       exit
194     end
195 
196     # Description       : sets the feature data with default details into Sybase
197     # Author            : Chandra sekaran
198     # Arguments         :
199     #  str_feature_name : feature name
200     # Return Arguments  :
201     #  num_feature_id   : primary key the feature
202     #  num_feature_result_id  : primary key of the feature result
203     #
204     def set_feature(str_feature_name)
205       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
206         str_query = "select TestFeatureID from TestFeature where FeatureName=?"
207         sth = dbh.prepare(str_query)
208         sth.execute(str_feature_name)
209 
210         if sth.fetch.nil?   # insert only if the data is not present in the table
211           sth = dbh.prepare("insert into TestFeature(FeatureName) values (?)")
212           sth.execute(str_feature_name)
213           dbh.commit
214           #puts "Added a record to TestFeature table successfully"
215         end
216         str_query = "select TestFeatureID from TestFeature where FeatureName='#{str_feature_name}'"
217         sth = dbh.prepare(str_query)
218         sth.execute()
219         num_feature_id = sth.fetch[0]
220         #puts "********** Record found with Primary key '#{num_feature_id}' in TestFeature *************"
221         dbh.disconnect()
222         num_feature_result_id = set_feature_result(num_feature_id)
223         return num_feature_id, num_feature_result_id   # return the feature id and feature result id of the feature
224       end
225     rescue Exception => ex
226       $log.error("Error in setting feature data to TestFeature table : #{ex}")
227       exit
228     end
229 
230     # Description       : sets the feature result data with default details into Sybase
231     # Author            : Chandra sekaran
232     # Arguments         :
233     #  str_feature_name : feature_id of the feature
234     # Return Arguments  :
235     #  num_feature_result_id  : primary key of the feature result
236     #
237     def set_feature_result(num_feature_id)
238       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
239         str_query = "select TestFeatureResultID from TestFeatureResult where TestFeatureID=? and BuildID=?"
240         sth = dbh.prepare(str_query)
241         sth.execute(num_feature_id, @build_id)
242 
243         if sth.fetch.nil?   # insert only if the data is not present in the table
244           sth = dbh.prepare("insert into TestFeatureResult(TestFeatureID,BuildID) values (?,?)")
245           sth.execute(num_feature_id, @build_id)
246           dbh.commit
247           #puts "Added a record to TestFeatureResult table successfully"
248         end
249 
250         str_query = "select TestFeatureResultID from TestFeatureResult where TestFeatureID=#{num_feature_id} and BuildID=#{@build_id}"
251         sth = dbh.prepare(str_query)
252         sth.execute()
253         num_feature_result_id = sth.fetch[0]
254         #puts "********** Record found with Primary key '#{num_feature_result_id}' in TestFeatureResult *************"
255         dbh.disconnect()
256         return num_feature_result_id   # return the feature result id of the feature
257       end
258     rescue Exception => ex
259       $log.error("Error in setting feature result data to TestFeatureResult table : #{ex}")
260       exit
261     end
262 
263     # Description       : resets the feature result data with execution details into Sybase
264     # Author            : Chandra sekaran
265     # Arguments         :
266     #    num_run_length : feature execution time in nanoseconds
267     #    num_pass_count : number of scenarios passed
268     #    num_fail_count : number of scenarios failed
269     #    num_skip_count : number of scenarios skipped
270     #    bool_result    : boolean value resembling the state of build result
271     #    num_feature_result_id : primary key of the TestFeatureResult table
272     #
273     def reset_feature_result(num_run_length, num_pass_count, num_fail_count, num_skip_count, bool_result, num_feature_result_id)
274       num_result =  bool_result ? 1 : 0
275       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
276         sth = dbh.prepare("update TestFeatureResult set RunLength=?, Passes=?, Failures=?, Skips=?, Result=? where TestFeatureResultID=?")
277         sth.execute(convert_duration(num_run_length), num_pass_count, num_fail_count, num_skip_count, num_result, num_feature_result_id)
278         sth.finish
279         dbh.commit
280         #puts "Updated a record (#{num_feature_result_id}) in TestFeatureResult table successfully"
281         dbh.disconnect()
282       end
283     rescue Exception => ex
284       $log.error("Error in resetting feature result data to TestFeatureResult table : #{ex}")
285       exit
286     end
287 
288     # Description           : sets the scenario data with default details into Sybase
289     # Author                : Chandra sekaran
290     # Arguments             :
291     #    str_scenario_name  : scenario name
292     #    str_qa_complete_id : QA Complete ID (Scenario ID) of the scenario
293     #    str_feature_id     : primary key of the feature
294     # Return Arguments      :
295     #    num_scenario_id    : primary key of the scenario
296     #    scenario_result_id : primary key of the scenario result
297     #
298     def set_scenario(str_scenario_name, str_qa_complete_id, str_feature_id)
299       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
300         str_query = "select TestScenarioID from TestScenario where ScenarioName=? and TestFeatureID=?"
301         sth = dbh.prepare(str_query)
302         sth.execute(str_scenario_name, str_feature_id.to_i)
303 
304         if sth.fetch.nil?  # insert only if the data is not present in the table
305           sth = dbh.prepare("insert into TestScenario(ScenarioName,QACompleteID,TestFeatureID) values (?,?,?)")
306           sth.execute(str_scenario_name, str_qa_complete_id, str_feature_id.to_i)
307           sth.finish
308           dbh.commit
309           #puts "Added a record to TestScenario table successfully"
310         end
311 
312         str_query = "select TestScenarioID from TestScenario where ScenarioName='#{str_scenario_name}' and TestFeatureID=#{str_feature_id}"
313         sth = dbh.prepare(str_query)
314         sth.execute()
315         num_scenario_id = sth.fetch[0]
316         #puts "********** Record found with Primary key '#{num_scenario_id}' in TestScenario *************"
317         scenario_result_id = set_scenario_result(num_scenario_id, str_feature_id)
318         dbh.disconnect()
319         return num_scenario_id, scenario_result_id    # return the scenario id and scenario result id of the scenario
320       end
321     rescue Exception => ex
322       $log.error("Error in setting scenario data to TestScenario table : #{ex}")
323       exit
324     end
325 
326     # Description           : sets the scenario result data with default details into Sybase
327     # Author                : Chandra sekaran
328     # Arguments             :
329     #    num_scenario_id    : primary key of the scenario
330     #    num_feature_id     : primary key of the feature
331     # Return Arguments      :
332     #    num_scenario_result_id : primary key of the scenario result
333     #
334     def set_scenario_result(num_scenario_id, num_feature_id)
335       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
336         sth = dbh.prepare("insert into TestScenarioResult(TestFeatureID,TestScenarioID,BuildID) values (?,?,?)")
337         sth.execute(num_feature_id, num_scenario_id, @build_id)
338         sth.finish
339         dbh.commit
340         #puts "Added a record to TestScenarioResult table successfully"
341 
342         str_query = "select TestScenarioResultID from TestScenarioResult where TestFeatureID=#{num_feature_id} and TestScenarioID=#{num_scenario_id} and BuildID=#{@build_id}"
343         sth = dbh.prepare(str_query)
344         sth.execute()
345         num_scenario_result_id = sth.fetch[0]
346         #puts "********** Record found with Primary key '#{num_scenario_result_id}' in TestScenarioResult *************"
347         dbh.disconnect()
348         return num_scenario_result_id    # return the scenario id of the scenario
349       end
350     rescue Exception => ex
351       $log.error("Error in setting scenario data to TestScenarioResult table : #{ex}")
352       exit
353     end
354 
355     # Description              : resets the scenario result data with execution details into Sybase
356     # Author                   : Chandra sekaran
357     # Arguments                :
358     #   num_scenario_result_id : primary key of the scenario result
359     #   num_run_length         : steps execution time in nanoseconds
360     #   num_pass_count         : number of steps passed
361     #   num_fail_count         : number of steps failed
362     #   num_skip_count         : number of steps skipped
363     #   bool_result            : boolean value resembling the state of steps result
364     #
365     def reset_scenario_result(num_scenario_result_id, num_run_length, num_pass_count, num_fail_count, num_skip_count, bool_result)
366       num_result =  bool_result ? 1 : 0
367       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
368         sth = dbh.prepare("update TestScenarioResult set RunLength=?, Passes=?, Failures=?, Skips=?, Result=? where TestScenarioResultID=?")
369         sth.execute(convert_duration(num_run_length), num_pass_count, num_fail_count, num_skip_count, num_result, num_scenario_result_id)
370         sth.finish
371         dbh.commit
372         #puts "Updated a record (#{num_scenario_result_id}) in TestScenarioResult table successfully"
373         dbh.disconnect()
374       end
375     rescue Exception => ex
376       $log.error("Error in resetting scenario data to TestScenarioResult table : #{ex}")
377       exit
378     end
379 
380     # Description               : sets the step data with default details into Sybase
381     # Author                    : Chandra sekaran
382     # Arguments                 :
383     #   str_step_name           : step name
384     #   str_scenario_id         : primary key of scenario
385     #   num_scenario_result_id  : primary key of scenario result
386     # Return Arguments          :
387     #   num_step_id             : primary key of step
388     #   num_step_result_id      : primary key of step result
389     #
390     def set_step(str_step_name, str_scenario_id, num_scenario_result_id)
391       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
392         str_query = "select TestStepID from TestStep where StepName=? and TestScenarioID=?"
393         sth = dbh.prepare(str_query)
394         sth.execute(str_step_name, str_scenario_id)
395 
396         if sth.fetch.nil?   # insert only if the data is not present in the table
397           sth = dbh.prepare("insert into TestStep(StepName,TestScenarioID) values (?,?)")
398           sth.execute(str_step_name, str_scenario_id)
399           dbh.commit
400           #puts "Added a record to TestStep table successfully"
401         end
402 
403         str_query = "select TestStepID from TestStep where StepName='#{str_step_name}' and TestScenarioID=#{str_scenario_id}"
404         sth = dbh.prepare(str_query)
405         sth.execute()
406         num_step_id = sth.fetch[0]
407         #puts "********** Record found with Primary key '#{num_step_id}' in TestStep *************"
408         dbh.disconnect()
409         return num_step_id
410       end
411     rescue Exception => ex
412       $log.error("Error in setting step data to TestStep table : #{ex}")
413       exit
414     end
415 
416     # Description               : sets the step result data with execution details into Sybase
417     # Author                    : Chandra sekaran
418     # Arguments                 :
419     #   num_step_id             : primary key of step
420     #   num_scenario_id         : primary key of scenario
421     #   num_scenario_result_id  : primary key of scenario result
422     #   bool_result             : boolean value resembling the state of step result
423     #   num_run_length          : steps execution time in nanoseconds
424     #
425     def set_step_result_new(num_step_id, num_scenario_id, num_scenario_result_id, bool_result, num_run_length)
426       num_result = bool_result ? 1 : 0
427       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
428         sth = dbh.prepare("insert into TestStepResult(TestScenarioResultID,Result,RunLength,TestStepID,BuildID,TestScenarioID) values (?,?,?,?,?,?)")
429         sth.execute(num_scenario_result_id, num_result, convert_duration(num_run_length), num_step_id, @build_id, num_scenario_id)
430         dbh.commit
431         dbh.disconnect()
432         #puts "Added a record to TestStepResult table successfully"
433       end
434     rescue Exception => ex
435       $log.error("(set_step_result_new)Error in setting step data to TestStepResult table : #{ex}")
436       exit
437     end
438 
439     # Description               : sets the step result data with default details into Sybase
440     # Author                    : Chandra sekaran
441     # Arguments                 :
442     #   num_step_id             : primary key of step
443     #   str_scenario_id         : primary key of scenario
444     #   num_scenario_result_id  : primary key of scenario result
445     # Return Arguments          :
446     #   num_step_result_id      : primary key of step result
447     #
448     def set_step_result(num_step_id, num_scenario_id, num_scenario_result_id)
449       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
450         sth = dbh.prepare("insert into TestStepResult(TestScenarioResultID,TestStepID,BuildID,TestScenarioID) values (?,?,?,?)")
451         sth.execute(num_scenario_result_id, num_step_id, @build_id, num_scenario_id)
452         dbh.commit
453         #puts "Added a record to TestStepResult table successfully"
454 
455         str_query = "select TestStepResultID from TestStepResult where TestScenarioResultID=#{num_scenario_result_id} and TestStepID=#{num_step_id} and BuildID=#{@build_id} and TestScenarioID=#{num_scenario_id}"
456         sth = dbh.prepare(str_query)
457         sth.execute()
458         num_step_result_id = sth.fetch[0]
459         #puts "********** Record found with Primary key '#{num_step_result_id}' in TestStepResult *************"
460         dbh.disconnect()
461         return num_step_result_id     # return the step result id of the step
462       end
463     rescue Exception => ex
464       $log.error("Error in setting step data to TestStep table : #{ex}")
465       exit
466     end
467 
468     # Description           : resets the step result data with execution details into Sybase
469     # Author                : Chandra sekaran
470     # Arguments             :
471     #   bool_result         : boolean value resembling the state of step result
472     #   num_run_length      : step execution time in nanoseconds
473     #   num_step_result_id  : primary key of step result
474     #
475     def reset_step_result(bool_result, num_run_length, num_step_result_id)
476       num_result = bool_result ? 1 : 0
477       DBI.connect(@str_connection_url, DB_USER_NAME, DB_PASSWORD) do |dbh|
478         sth = dbh.prepare("update TestStepResult set Result=?, RunLength=? where TestStepResultID=?")
479         sth.execute(num_result, convert_duration(num_run_length), num_step_result_id)
480         sth.finish
481         dbh.commit
482         #puts "Updated a record (#{num_step_result_id}) in TestStepResult table successfully"
483         dbh.disconnect()
484       end
485     rescue Exception => ex
486       $log.error("Error in resetting step results data in TestStepResult table : #{ex}")
487       exit
488     end
489 
490     # Description           : converts nanoseconds to seconds
491     # Author                : Chandra sekaran
492     # Arguments             :
493     #   num_duration        : time in nanoseconds
494     # Return Argument       : time in seconds
495     #
496     def convert_duration(num_duration)
497       num_duration/(1000*1000*1000) #.to_f    # convert nanosecond to second
498     end
499 
500     # Description           : function that creates performance report data and stores it in Sybase
501     # Author                : Chandra sekaran
502     #
503     def create_performance_report
504       set_build       # set Build data only once for each execution (Single or Parallel)
505       @arr_file_name.each do |path|
506         @arr_background_step_duration = []
507         file = File.read(path)
508         @json = JSON.parse(file)
509         parse_json    # parse each json file and extract report data
510       end
511       #puts "Build duration : #{@num_build_duration} - #{@bool_build_passed} - #{@num_feature_count}"
512       reset_build(@num_build_duration, @num_feature_pass_count, @num_feature_fail_count, @num_feature_skip_count, @bool_build_passed)  # Update the Build data with execution summary
513     rescue Exception => ex
514       $log.error("Error while creating report : #{ex}")
515       exit
516     end
517 
518   end
519 end

Generated on 2015-05-08 10:40:30 +0530 with SimpleCov-RCov 0.2.3