\r\n

51Degrees Device Detection Python  4.4

Device Detection services for 51Degrees Pipeline

onpremise/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 2022 51 Degrees Mobile Experts Limited, Davidson House,
4 # Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 #
6 # This Original Work is licensed under the European Union Public Licence
7 # (EUPL) 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 from fiftyone_devicedetection_examples.example_utils import ExampleUtils
35 
36 data_file = ExampleUtils.find_file("51Degrees-LiteV4.1.hash")
37 
38 pipeline = DeviceDetectionOnPremisePipelineBuilder(
39  data_file_path=data_file,
40  licence_keys="",
41  performance_profile='MaxPerformance',
42  add_javascript_builder = False,
43  restricted_properties = ["ismobile"],
44  usage_sharing=False,
45  auto_update=False).build()
46 
47 # Here we make a function that processes a user agent
48 # And returns if it is a mobile device
49 
50 def process_user_agent(user_agent):
51 
52  # First we create the flowdata using the global pipeline
53  flowdata = pipeline.create_flowdata()
54 
55  # Here we add the user agent as evidence
56  flowdata.evidence.add("header.user-agent", user_agent)
57 
58  # We process the flowdata to get the results
59  flowdata.process()
60 
61  # To check whether the User-Agent is a mobile device we look at the ismobile
62  # property inside the Device Detection Engine
63 
64  # first we check if this has a meaningful result
65 
66  if flowdata.device.ismobile.has_value():
67  return flowdata.device.ismobile.value()
68  else:
69  return None
70 
71 def process_user_agent_list(user_agent_list, list_number, output, skip=False):
72  results = {
73  "mobile": 0,
74  "notmobile": 0,
75  "unknown": 0
76  }
77  for user_agent in user_agent_list:
78  if skip:
79  break
80  result = process_user_agent(user_agent[0])
81  if(result == None):
82  results["unknown"] += 1
83  if(result == True):
84  results["mobile"] += 1
85  if(result == False):
86  results["notmobile"] += 1
87 
88  output.put(results, list_number)
89 
90 # Run the process
91 def run(skip = False):
92 
93  # Make a queue to store the results in
94 
95  output = mp.Queue()
96 
97  # Create processes
98  processes = []
99 
100  for x in range(threads):
101  processes.append(mp.Process(target=process_user_agent_list,
102  args=(split_lists[x], x, output, skip)))
103 
104  # Start timer
105 
106  t0 = time.time()
107 
108  for p in processes:
109  p.start()
110 
111  # Exit the completed processes
112  for p in processes:
113  p.join()
114 
115  # Get process results from the output queue
116  results = [output.get() for p in processes]
117 
118  t1 = time.time()
119 
120  total = t1-t0
121 
122  return {"time": total, "result": results}
123 
124 if __name__ == "__main__":
125 
126  # First we read the contents of the 20000 user agents file as a list
127  with open('fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/20000 User Agents.csv', newline='') as file:
128  reader = csv.reader(file)
129  user_agents = list(reader)
130 
131  number_of_user_agents = len(user_agents)
132 
133  print("Processing " + str(number_of_user_agents) + " user agents")
134 
135  # Now we make a function that returns results of the user agent matching
136 
137  threads = mp.cpu_count()
138 
139  print("Using " + str(threads) + " threads")
140 
141  chunk_size = int(number_of_user_agents / threads)
142 
143  # Split lists by number of threads
144  split_lists = [user_agents[x:x+chunk_size]
145  for x in range(0, len(user_agents), chunk_size)]
146 
147  calibration = run(skip=True)
148 
149  real = run(skip=False)
150 
151  real_time = real["time"] - calibration["time"]
152 
153  print("Total time (seconds): " + str(real_time) + " seconds")
154  print ("Time per user agent (ms): " + str((real_time / number_of_user_agents) * 1000))
155 
156  final_result = {
157  "mobile": 0,
158  "notmobile": 0,
159  "unknown": 0
160  }
161 
162  for result in real["result"]:
163  final_result["unknown"] += result["unknown"]
164  final_result["mobile"] += result["mobile"]
165  final_result["notmobile"] += result["notmobile"]
166 
167  print("Results", final_result)