\r\n

51Degrees Device Detection Python  4.2Newer Version 4.3

Device Detection services for 51Degrees Pipeline

hash/performance.py

This example shows how to change the performance profile when creating a 51Degrees device detection engine. It also includes a simple method of benchmarking the engine that can illustrate the performance differences. Note that benchmarking is a complex area and this is not a sophisticated solution. It is simply intended to demonstrate the approximate, relative performance of the pre-configured profiles.

1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4 # Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5 #
6 # This Original Work is licensed under the European Union Public Licence (EUPL)
7 # v.1.2 and is subject to its terms as set out below.
8 #
9 # If a copy of the EUPL was not distributed with this file, You can obtain
10 # one at https://opensource.org/licenses/EUPL-1.2.
11 #
12 # The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 # amended by the European Commission) shall be deemed incompatible for
14 # the purposes of the Work and the provisions of the compatibility
15 # clause in Article 5 of the EUPL shall not apply.
16 #
17 # If using the Work as, or as part of, a network application, by
18 # including the attribution notice(s) required under Article 5 of the EUPL
19 # in the end user terms of the application under an appropriate heading,
20 # such notice(s) shall fulfill the requirements of that article.
21 # ********************************************************************
22 
23 
27 
28 import csv
29 import time
30 import multiprocessing as mp
31 
32 # This example goes through a CSV of 20000 user agents and processes them, returning the time and information about the matches
33 from fiftyone_devicedetection_onpremise.devicedetection_onpremise_pipelinebuilder import DeviceDetectionOnPremisePipelineBuilder
34 
35 data_file = "fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash"
36 
37 pipeline = DeviceDetectionOnPremisePipelineBuilder(
38  data_file_path=data_file,
39  licence_keys="",
40  performance_profile='MaxPerformance',
41  add_javascript_builder = False,
42  restricted_properties = ["ismobile"],
43  usage_sharing=False,
44  auto_update=False).build()
45 
46 # Here we make a function that processes a user agent
47 # And returns if it is a mobile device
48 
49 def process_user_agent(user_agent):
50 
51  # First we create the flowdata using the global pipeline
52  flowdata = pipeline.create_flowdata()
53 
54  # Here we add the user agent as evidence
55  flowdata.evidence.add("header.user-agent", user_agent)
56 
57  # We process the flowdata to get the results
58  flowdata.process()
59 
60  # To check whether the User-Agent is a mobile device we look at the ismobile
61  # property inside the Device Detection Engine
62 
63  # first we check if this has a meaningful result
64 
65  if flowdata.device.ismobile.has_value():
66  return flowdata.device.ismobile.value()
67  else:
68  return None
69 
70 def process_user_agent_list(user_agent_list, list_number, output, skip=False):
71  results = {
72  "mobile": 0,
73  "notmobile": 0,
74  "unknown": 0
75  }
76  for user_agent in user_agent_list:
77  if skip:
78  break
79  result = process_user_agent(user_agent[0])
80  if(result == None):
81  results["unknown"] += 1
82  if(result == True):
83  results["mobile"] += 1
84  if(result == False):
85  results["notmobile"] += 1
86 
87  output.put(results, list_number)
88 
89 # Run the process
90 def run(skip = False):
91 
92  # Make a queue to store the results in
93 
94  output = mp.Queue()
95 
96  # Create processes
97  processes = []
98 
99  for x in range(threads):
100  processes.append(mp.Process(target=process_user_agent_list,
101  args=(split_lists[x], x, output, skip)))
102 
103  # Start timer
104 
105  t0 = time.time()
106 
107  for p in processes:
108  p.start()
109 
110  # Exit the completed processes
111  for p in processes:
112  p.join()
113 
114  # Get process results from the output queue
115  results = [output.get() for p in processes]
116 
117  t1 = time.time()
118 
119  total = t1-t0
120 
121  return {"time": total, "result": results}
122 
123 if __name__ == "__main__":
124 
125  # First we read the contents of the 20000 user agents file as a list
126  with open('fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/20000 User Agents.csv', newline='') as file:
127  reader = csv.reader(file)
128  user_agents = list(reader)
129 
130  number_of_user_agents = len(user_agents)
131 
132  print("Processing " + str(number_of_user_agents) + " user agents")
133 
134  # Now we make a function that returns results of the user agent matching
135 
136  threads = mp.cpu_count()
137 
138  print("Using " + str(threads) + " threads")
139 
140  chunk_size = int(number_of_user_agents / threads)
141 
142  # Split lists by number of threads
143  split_lists = [user_agents[x:x+chunk_size]
144  for x in range(0, len(user_agents), chunk_size)]
145 
146  calibration = run(skip=True)
147 
148  real = run(skip=False)
149 
150  real_time = real["time"] - calibration["time"]
151 
152  print("Total time (seconds): " + str(real_time) + " seconds")
153  print ("Time per user agent (ms): " + str((real_time / number_of_user_agents) * 1000))
154 
155  final_result = {
156  "mobile": 0,
157  "notmobile": 0,
158  "unknown": 0
159  }
160 
161  for result in real["result"]:
162  final_result["unknown"] += result["unknown"]
163  final_result["mobile"] += result["mobile"]
164  final_result["notmobile"] += result["notmobile"]
165 
166  print("Results", final_result)