-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Bl4omArchie/binary
Binary
- Loading branch information
Showing
20 changed files
with
242 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from pubcrypt.cryptosystem.rsa import * | ||
from pubcrypt.number.primality import * | ||
from pubcrypt.number.util import * | ||
from benchmark.plotting import * | ||
from benchmark.profiler import * | ||
|
||
import random, math, os | ||
|
||
|
||
""" | ||
This benchmark intend to evaluate the effiency of Pubcrypt's function | ||
With only two simples class: GraphVisualization and EffiencyProfile, I have access to a good overview of the performance. | ||
- GraphVisualization allow me to generate a graph that plot the times of execution for one or severals function. | ||
- EffiencyProfile generate a profile with the library cProfile which is specialised in examining function in details. It give me information about witch modules are called in the function, how many I have been calling them and the final generation time. | ||
This evaluation is more accurated for huge function that used many external packages. | ||
There is two examples: | ||
""" | ||
|
||
|
||
def plotting_sample(): | ||
obj = GraphVisualization("Function generate") | ||
obj.measure_execution_time(100, generate, 2048) | ||
obj.plot_data(["green"], ["generate()"], show_stats=True) | ||
|
||
def profile_sample(): | ||
obj = EffiencyProfile(generate) | ||
obj.create_profile(2048) | ||
obj.read_profile() | ||
|
||
def gcd_test(): | ||
n = 5000 | ||
obj = GraphVisualization("GCD comparison") | ||
obj.measure_execution_time(n, gcd, random.randint(2**2048, 2**2049), random.randint(2**2048, 2**2049)) | ||
obj.measure_execution_time(n, math.gcd, random.randint(2**2048, 2**2049), random.randint(2**2048, 2**2049)) | ||
obj.plot_data(["green", "red"], ["gcd()", "GCD()"], show_stats=True) | ||
|
||
def pow_test(): | ||
n = 5000 | ||
obj = GraphVisualization("Fast exponentiation comparison") | ||
obj.measure_execution_time(n, pow, random.randint(2**2048, 2**2049), random.randint(2**2048, 2**2049), random.randint(2**2048, 2**2049)) | ||
obj.measure_execution_time(n, pow_fast, random.randint(2**2048, 2**2049), random.randint(2**2048, 2**2049), random.randint(2**2048, 2**2049)) | ||
obj.plot_data(["green", "red"], ["pow()", "pow_fast()"], show_stats=True) | ||
|
||
def rng_test(): | ||
n = 1000 | ||
obj = GraphVisualization("RNG comparison") | ||
obj.measure_execution_time(n, random.getrandbits, 2048) | ||
obj.measure_execution_time(n, random.randint, 2**2048, 2**2049) | ||
obj.measure_execution_time(n, random.randrange, 2**2048, 2**2049) | ||
obj.measure_execution_time(n, os.urandom, 2048) | ||
obj.plot_data(["green", "red", "blue", "purple"], ["getrandbits()", "randint()", "randrange()", "urandom()"], show_stats=False) | ||
|
||
if __name__ == "__main__": | ||
plotting_sample() |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import time | ||
|
||
|
||
class GraphVisualization: | ||
def __init__(self, title, size=(15, 13)) -> None: | ||
self.title = title | ||
self.size = size | ||
self.execution_times = [] | ||
self.path = "benchmark/graph/" | ||
|
||
|
||
def measure_execution_time(self, n, func, *args, **kwargs): | ||
func_times = [] | ||
for _ in range(n): | ||
start = time.time() | ||
func(*args, **kwargs) | ||
func_times.append(time.time() - start) | ||
self.execution_times.append(func_times) | ||
|
||
|
||
def plot_data(self, colors, labels, graph_type='lines', show_stats=False): | ||
if len(self.execution_times) != len(colors) or len(self.execution_times) != len(labels): | ||
raise ValueError("Lengths of execution_times, colors, and labels should be the same.") | ||
|
||
if graph_type not in ['lines', 'scatter']: | ||
raise ValueError("Invalid graph_type. Choose from 'lines' or 'scatter'") | ||
|
||
plt.figure(figsize=self.size) | ||
for i in range(len(self.execution_times)): | ||
data_chunk_size = len(self.execution_times[i]) | ||
if graph_type == 'lines': | ||
plt.plot(np.linspace(0, data_chunk_size, data_chunk_size), self.execution_times[i], color=colors[i], label=labels[i]) | ||
|
||
if show_stats: | ||
min_value = np.min(self.execution_times[i]) | ||
max_value = np.max(self.execution_times[i]) | ||
mean_value = np.mean(self.execution_times[i]) | ||
plt.text(data_chunk_size, min_value, f"Min: {min_value:.10f}", verticalalignment='top', horizontalalignment='left') | ||
plt.text(data_chunk_size, max_value, f"Max: {max_value:.10f}", verticalalignment='top', horizontalalignment='right') | ||
plt.text(data_chunk_size, mean_value, f"Mean: {mean_value:.10f}", verticalalignment='top', horizontalalignment='right') | ||
|
||
plt.title(self.title) | ||
plt.xlabel('Test Iteration') | ||
plt.ylabel('Elapsed Time (seconds)') | ||
plt.legend() | ||
plt.grid(True) | ||
plt.savefig(self.path + self.title) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import cProfile, pstats | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
path = "benchmark/profiles/" | ||
sufix = ".ep" | ||
|
||
class EffiencyProfile: | ||
def __init__(self, func): | ||
self.func = func | ||
self.size = (10, 8) | ||
self.func_name = str(func) | ||
self.profile_file = path + f"{self.func_name}" + sufix | ||
|
||
def create_profile(self, *args, **kwargs): | ||
profiler = cProfile.Profile() | ||
profiler.enable() | ||
self.func(*args, **kwargs) | ||
profiler.disable() | ||
profiler.dump_stats(self.profile_file) | ||
|
||
def read_profile(self): | ||
stats = pstats.Stats(self.profile_file) | ||
stats.print_stats() |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.