Best Python code snippet using autotest_python
plot_fig5_binomial.py
Source:plot_fig5_binomial.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3""" Plot probabilities of entity occurrence.4Usage: python plot_fig5_binomial.py5Input data files: ../data/[app_name]_out/complete_user_[app_name].txt, ../data/[app_name]_out/user_[app_name]_all.txt6Time: ~5M7"""8import sys, os, platform9from collections import defaultdict, Counter10import numpy as np11from scipy.special import comb12from scipy import stats13import matplotlib as mpl14if platform.system() == 'Linux':15 mpl.use('Agg') # no UI backend16import matplotlib.pyplot as plt17from matplotlib.ticker import FuncFormatter18sys.path.append(os.path.join(os.path.dirname(__file__), '../'))19from utils.helper import Timer20from utils.plot_conf import ColorPalette, hide_spines21def binomial(n, k, rho):22 # n: number of trials, k: number of success (being sampled)23 return comb(n, k, exact=True) * (rho ** k) * ((1 - rho) ** (n - k))24def main():25 timer = Timer()26 timer.start()27 cc4 = ColorPalette.CC428 blue = cc4[0]29 app_name = 'cyberbullying'30 rho = 0.527231 entity = 'user'32 fig, axes = plt.subplots(1, 2, figsize=(10, 3.3))33 print('for entity: {0}'.format(entity))34 sample_entity_freq_dict = defaultdict(int)35 with open('../data/{1}_out/{0}_{1}_all.txt'.format(entity, app_name), 'r') as sample_datefile:36 for line in sample_datefile:37 sample_entity_freq_dict[line.rstrip().split(',')[1]] += 138 complete_entity_freq_dict = defaultdict(int)39 with open('../data/{1}_out/complete_{0}_{1}.txt'.format(entity, app_name), 'r') as complete_datefile:40 for line in complete_datefile:41 complete_entity_freq_dict[line.rstrip().split(',')[1]] += 142 complete_to_sample_freq_dict = defaultdict(list)43 sample_to_complete_freq_dict = defaultdict(list)44 for item, complete_vol in complete_entity_freq_dict.items():45 if item in sample_entity_freq_dict:46 complete_to_sample_freq_dict[complete_vol].append(sample_entity_freq_dict[item])47 else:48 complete_to_sample_freq_dict[complete_vol].append(0)49 for item, sample_vol in sample_entity_freq_dict.items():50 sample_to_complete_freq_dict[sample_vol].append(complete_entity_freq_dict[item])51 for item in set(complete_entity_freq_dict.keys()) - set(sample_entity_freq_dict.keys()):52 sample_to_complete_freq_dict[0].append(complete_entity_freq_dict[item])53 ax1_x_axis = range(1, 101)54 ax1_y_axis = []55 empirical_mean_list = []56 expected_mean_list = []57 for num_complete in ax1_x_axis:58 # compute complete to sample59 empirical_cnt_dist = complete_to_sample_freq_dict[num_complete]60 binomial_cnt_dist = []61 for x in range(num_complete + 1):62 binomial_cnt_dist.extend([x] * int(binomial(num_complete, x, rho) * len(empirical_cnt_dist)))63 ks_test = stats.ks_2samp(empirical_cnt_dist, binomial_cnt_dist)64 empirical_mean = sum(empirical_cnt_dist) / len(empirical_cnt_dist)65 empirical_mean_list.append(empirical_mean)66 expected_mean = sum(binomial_cnt_dist) / len(binomial_cnt_dist)67 expected_mean_list.append(expected_mean)68 print('num_complete: {0}, number of Bernoulli trials: {1}, d_statistic: {2:.4f}, p: {3:.4f}, expected mean: {4:.2f}, empirical mean: {5:.2f}'69 .format(num_complete, len(empirical_cnt_dist), ks_test[0], ks_test[1], expected_mean, empirical_mean))70 ax1_y_axis.append(ks_test[0])71 axes[0].plot(ax1_x_axis, ax1_y_axis, c='k', lw=1.5, ls='-')72 axes[0].set_xlabel(r'complete frequency $n_c$', fontsize=16)73 axes[0].set_ylabel('D-statistic', fontsize=16)74 axes[0].set_xlim([-2, 102])75 axes[0].set_xticks([0, 25, 50, 75, 100])76 axes[0].set_ylim([0, 0.1])77 axes[0].yaxis.set_major_formatter(FuncFormatter(lambda x, _: '{0:.2f}'.format(x)))78 axes[0].tick_params(axis='both', which='major', labelsize=16)79 axes[0].set_title('(a)', fontsize=18, pad=-3*72, y=1.0001)80 # show an example81 num_complete = 2082 axes[0].scatter(num_complete, ax1_y_axis[num_complete - 1], s=40, c=blue, zorder=30)83 axes[0].set_yticks([0, ax1_y_axis[num_complete - 1], 0.05, 0.1])84 axes[0].plot([axes[0].get_xlim()[0], num_complete], [ax1_y_axis[num_complete - 1], ax1_y_axis[num_complete - 1]], color=blue, ls='--', lw=1)85 axes[0].plot([num_complete, num_complete], [axes[0].get_ylim()[0], ax1_y_axis[num_complete - 1]], color=blue, ls='--', lw=1)86 # plot complete to sample87 ax2_x_axis = range(num_complete + 1)88 num_items = len(complete_to_sample_freq_dict[num_complete])89 complete_to_sample_cnt = Counter(complete_to_sample_freq_dict[num_complete])90 ax2_y_axis = [complete_to_sample_cnt[x] / num_items for x in ax2_x_axis]91 ax2_binomial_axis = [binomial(num_complete, x, rho) for x in ax2_x_axis]92 axes[1].plot(ax2_x_axis, ax2_y_axis, c=blue, lw=1.5, ls='-', marker='o', zorder=20, label='empirical')93 axes[1].plot(ax2_x_axis, ax2_binomial_axis, c='k', lw=1.5, ls='-', marker='x', zorder=10, label='binomial')94 axes[1].set_xlabel(r'sample frequency $n_s$', fontsize=16)95 axes[1].set_ylabel(r'Pr($n_s$|$n_c$={0})'.format(num_complete), fontsize=16)96 axes[1].set_xticks([0, 5, 10, 15, 20])97 axes[1].set_ylim([-0.005, 0.27])98 axes[1].set_yticks([0, 0.1, 0.2])99 axes[1].tick_params(axis='both', which='major', labelsize=16)100 axes[1].legend(frameon=False, fontsize=16, ncol=1, fancybox=False, shadow=True, loc='upper left')101 axes[1].set_title('(b)', fontsize=18, pad=-3*72, y=1.0001)102 axes[1].plot([empirical_mean_list[num_complete - 1], empirical_mean_list[num_complete - 1]], [axes[1].get_ylim()[0], 0.21], color=blue, ls='--', lw=1)103 axes[1].plot([expected_mean_list[num_complete - 1], expected_mean_list[num_complete - 1]], [axes[1].get_ylim()[0], 0.21], color='k', ls='--', lw=1)104 hide_spines(axes)105 timer.stop()106 plt.tight_layout(rect=[0, 0.05, 1, 1])107 plt.savefig('../images/entity_binomial.pdf', bbox_inches='tight')108 if not platform.system() == 'Linux':109 plt.show()110if __name__ == '__main__':...
bench_auth.py
Source:bench_auth.py
1#!/usr/bin/python32import argparse3import copy4import json5import rados6import time7import multiprocessing8caps_base = ["mon", "profile rbd", "osd", "profile rbd pool=rbd namespace=test"]9def create_users(conn, num_namespaces, num_users):10 cmd = {'prefix': 'auth get-or-create'}11 for i in range(num_namespaces):12 caps_base[-1] += ", profile rbd pool=rbd namespace=namespace{}".format(i)13 cmd['caps'] = caps_base14 for i in range(num_users):15 cmd['entity'] = "client.{}".format(i)16 conn.mon_command(json.dumps(cmd), b'')17class Worker(multiprocessing.Process):18 def __init__(self, conn, num, queue, duration):19 super().__init__()20 self.conn = conn21 self.num = num22 self.queue = queue23 self.duration = duration24 def run(self):25 client = "client.{}".format(self.num)26 cmd = {'prefix': 'auth caps', 'entity': client}27 start_time = time.time()28 num_complete = 029 with rados.Rados(conffile='') as conn:30 while True:31 now = time.time()32 diff = now - start_time33 if diff > self.duration:34 self.queue.put((num_complete, diff))35 return36 caps = copy.deepcopy(caps_base)37 caps[-1] += ", profile rbd pool=rbd namespace=namespace{}".format(self.num * 10000 + num_complete)38 cmd['caps'] = caps39 cmd_start = time.time()40 ret, buf, out = conn.mon_command(json.dumps(cmd), b'')41 cmd_end = time.time()42 if ret != 0:43 self.queue.put((Exception("{0}: {1}".format(ret, out)), 0))44 return45 num_complete += 146 print("Process {} finished op {} - latency: {}".format(self.num, num_complete, cmd_end - cmd_start))47def main():48 parser = argparse.ArgumentParser(description="""49Benchmark updates to ceph users' capabilities. Run one update at a time in each thread.50""")51 parser.add_argument(52 '-n', '--num-namespaces',53 type=int,54 default=300,55 help='number of namespaces per user',56 )57 parser.add_argument(58 '-t', '--threads',59 type=int,60 default=10,61 help='number of threads (and thus parallel operations) to use',62 )63 parser.add_argument(64 '-d', '--duration',65 type=int,66 default=30,67 help='how long to run, in seconds',68 )69 args = parser.parse_args()70 num_namespaces = args.num_namespaces71 num_threads = args.threads72 duration = args.duration73 workers = []74 results = []75 q = multiprocessing.Queue()76 with rados.Rados(conffile=rados.Rados.DEFAULT_CONF_FILES) as conn:77 create_users(conn, num_namespaces, num_threads)78 for i in range(num_threads):79 workers.append(Worker(conn, i, q, duration))80 workers[-1].start()81 for i in range(num_threads):82 num_complete, seconds = q.get()83 if isinstance(num_complete, Exception):84 raise num_complete85 results.append((num_complete, seconds))86 total = 087 total_rate = 088 for num, sec in results:89 print("Completed {} in {} ({} / s)".format(num, sec, num / sec))90 total += num91 total_rate += num / sec92 print("Total: ", total)93 print("Avg rate: ", total_rate / len(results))94if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!