File: stress.pl

package info (click to toggle)
libgraph-easy-perl 0.71-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,284 kB
  • sloc: perl: 24,909; makefile: 2
file content (137 lines) | stat: -rw-r--r-- 2,508 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/perl -w

BEGIN
  {
  use lib 'lib';
  $|++;
  }

use Scalar::Util qw/weaken/;
use Time::HiRes qw/time/;
use Data::Dumper;
use Graph::Easy;

my $N1 = shift || 5000;
my $N2 = shift || 40000;
my $STEP = shift || 2;

# results
my $RC = [];

print "Using Graph::Easy v$Graph::Easy::VERSION\n";

for (my $N = $N1; $N < $N2; $N *= $STEP)
  {
  my @R = ($N);
  my $start = time();

  print scalar localtime(), " start\n";
  normal($N);
  print scalar localtime(), " done, took ", sprintf("%.2f", time() - $start)," seconds\n";
  push @R, sprintf("%.2f",time() - $start);

  $start = time();

  print scalar localtime(), " start\n";

  my $graph = graph($N);	# return the graph to show that creation sep.

  print scalar localtime(), " done creation, took ", sprintf("%.2f", time() - $start)," seconds\n";
  push @R, sprintf("%.2f",time() - $start);

  $start = time();
  $graph = undef;

  print scalar localtime(), " done destroy, took ", sprintf("%.2f", time() - $start)," seconds\n";
  push @R, sprintf("%.2f",time() - $start);

  $start = time();

  push @$RC, [ @R ];

  }

print "\n";
print "\n", join("\t\t", 'N', 'Normal', 'Graph-Easy'), "\tGraph-Easy\n";
print join("\t\t", '', '', 'Create','Destroy'), "\n";
print '-' x 70,"\n";

# print results
for my $R (@$RC)
  {
  print join("\t\t", @$R), "\n";
  }

sub graph
  {
  my $N = shift;

  my $graph = Graph::Easy->new();

  # create N objects, and "link" them together
  for my $i (1..$N)
    {
    my $b = $i; $b++;
    $graph->add_edge($i,$b);
    }
  print Dumper($graph),"\n" if $N < 10;
  $graph;
  }

sub normal
  {
  my $N = shift;

  my $container = {};

  my $old_object;

  # create N objects, and "link" them together
  for my $i (1..$N)
    {
    my $o = new_object($i);
    $container->{nodes}->{$i} = $o;

    $o->{graph} = $container;
    weaken($o->{graph});
  
    if ($old_object)
      {
      my $link = new_link($old_object, $o, $i);
      $container->{edges}->{$i} = $link;
   
      $link->{graph} = $container;
	{
	no warnings;
	
        weaken($link->{graph});
        weaken($link->{to}->{graph});
        weaken($link->{from}->{graph});
	}
      }

    $old_object = $o;
    }
  print Dumper($container),"\n" if $N < 10;
  }

sub new_object
  {
  my $id = shift;

  my $o = bless { id => $id, att => {}, }, 'main';

  $o;
  }

sub new_link
  {
  my ($a,$b,$id) = @_;

  my $link = bless { id => $id, from => $a, to => $b, att => {} }, 'main';

  $a->{edges}->{$id} = $link;
  $b->{edges}->{$id} = $link;

  $link;
  }